![]() |
|||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||
|
File Encryption Using Catalan Numbers and Stack Permutation
What are the Catalan Numbers? A number can be denoted as a Catalan number if its binary form consists of equal number of "1"s and "0"s and start with "1". If “1” represents 'open parenthesis and “0” 'close parenthesis', we can say every open parenthesis closes.
The parenthesis representation is “ (((((()((()))(((()))))())()))()((((()))()((())()()())())))() “
n = array size. For example array size is n=30. So array can be written in Below there is an example on Stack Permutation. For
3 characters there are Number = Character array = “ abc
”.
So array “ abc “ changed to “ acb ”.
Algorithm can be summarized like this:
Finding Catalan Numbers: 1. Binary array is constructed from number “ s “. 2. In binary array for “ 1 “ usage is “ counter + 1 “ . For “ 0 ” usage is “ counter - 1 “ 3.
In WHILE
cycle stay in while counter 4. After WHILE cycle, if counter = 0, number " s " is Catalan.
Encryption Process: 1. Create a binary array from number “ m “. 2. Using binary array in stack permutation original data is mixed.
Decrypt Process: 1. Create a binary array from number “ m “. 2. In binary array every element is changed: “ 1 ” with “ 0 ” and “ 0 ” with “ 1 ”. 3. Using binary array in stack permutation original data is reconstructed.
Note:
For 60-bit numbers there are 3.814.986.502.092.304 Catalan numbers.
If we try to find all Catalan numbers and write to disk, required space is: for binary file 30.519.892.016.738.432 byte = 29.804.582.047.596 KB = 29.106.037.155 MB = 28.423.864 GB = 27.757 TB
Suppose all 60-bit Catalan numbers were calculated. If for every number average control time is 1 ms, test time will be 120972 years. Average time will be
Here are some 60-bit Catalan numbers for you.
The Algorithm
Win32 Source Code (Visual C++)
Files are used for test purposes. File name entrance should be changed.
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #define chr_num 30 long filesize(FILE* stream) { long curpos, length; curpos = ftell(stream); fseek(stream, 0L, SEEK_END); length = ftell(stream); fseek(stream, curpos, SEEK_SET); return length; } int find_catalan() { short A[67]; __int64 i, k, m, n, s, count, cstart, cend ; FILE *fl; cstart = 576460752303423488; cend = 1152921503533105152; k = 0; s = cstart; // 60-bit = 30 character. while ( s < cend ) { m = s; n = 0; while(m > 1) { if ( (m&1) > 0) A[n] = 1; else A[n] = 0; n++; m = m >> 1; } A[n] = 1; n++; count = 0; i = n - 1; while ( (i>=0) && (count>=0) ) { if (A[i] == 1) count++; else count--; i--; } if( count == 0 ) // number is catalan { fprintf(fl,"%I64d\n",s); } s++; } fclose(fl); return 0; } int encrypt_catalan() { short A[67]; char buf1[chr_num], buf2[chr_num], buf3[chr_num], ch; __int64 i, j, k, m, n, s, L1, L2, L3; long int count = 0; FILE *fl1, *fl2; fl1 = fopen("z.txt","rb"); fl2 = fopen("z2.txt","wb"); m = 1142920503053891760; // used catalan number. n = 0; while(m > 1) { if ( (m&1) > 0) A[n] = 1; else A[n] = 0; n++; m = m >> 1; } A[n] = 1; count = filesize(fl1) / chr_num; for (j=0;j<count;j++) { fread(&buf1, chr_num, 1, fl1); L1 = chr_num-1; L2 = L3 = 0; for(i=n;i>=0;i--) if (A[i] == 1) // PUSH { buf2[L2] = buf1[L1]; L2++; L1--; } else // POP { buf3[L3] = buf2[L2-1]; L2--; L3++; buf2[L2] = 0; } fwrite(&buf3, chr_num, 1, fl2); } count = filesize(fl1) - ftell(fl1); fread(&buf1, count, 1, fl1); fwrite(&buf1, count, 1, fl2); fclose(fl1); fclose(fl2); return 0; } int decrypt_catalan() { short A[64]; char buf1[chr_num], buf2[chr_num], buf3[chr_num], tmp; __int64 i, j, k, m, n, s, L1, L2, L3; long int count = 0; FILE *fl1, *fl2; fl1 = fopen("z2.txt","rb"); fl2 = fopen("z3.txt","wb"); m = 1142920503053891760; // used catalan number. n = 0; while(m > 1) { if ( (m&1) > 0) A[n] = 1; else A[n] = 0; n++; m = m >> 1; } A[n] = 1; for(i=0;i<=n;i++) A[i] = A[i] ^ 1; count = filesize(fl1) / chr_num; for (j=0;j<count;j++) { fread(&buf1, chr_num, 1, fl1); L1 = chr_num - 1; L2 = L3 = 0; for(i=0;i<=n;i++) if (A[i] == 1) { buf2[L2] = buf1[L1]; L2++; L1--; } else { buf3[L3] = buf2[L2-1]; L2--; L3++; } fwrite(&buf3, chr_num, 1, fl2); } count = filesize(fl1) - ftell(fl1); fread(&buf1, count, 1, fl1); fwrite(&buf1, count, 1, fl2); fclose(fl1); fclose(fl2); return 0; } void main(void) { find_catalan(); encrypt_catalan(); decrypt_catalan(); }
Sample Execution
For 30 characters a 60-bit catalan number is required. Used
number is:
Original Text File
TASM now supports aliasing. This means that TASM allows the association of an alias name with another name ( called the substitute name ) in a program. Any time that alias name is encountered it will really refer to the substitute name. Aliasing is primarily a linker issue. The alias statement will generate an alias record setting the alias name equal to the substitute name, e.g.: 000056 ALIAS '_Set_Coords' = '_SetCoords' When the linker tries to resolve a reference to a name and it finds an alias record for that name, it will continue trying to resolve the reference using the substitute name.
Encrypted Text File
nasi aligs.t rsupp ooM nSTAwTwlloSM asA Tth thasta meshinesliaan a nfamoion t isocs aaelcale ( emda tnthero ath i wnh n a ) i e mpraute tntubsse ioaalihat st naey tinm m. agrAm illit wr edaleunteorns eie clutitsubst ee nh to trefery alariprimy sa isinga l A.meilsas aliteahteTue. s ier kinsmi al anaest ragene rl witenleas naliam ee ehing tted srcotqm natuteei,t se suhb toluate_ ' SSeAt_I56 A0L0: 0..g0Ch Wrds'eono tC'_Se t ds'roo=heolv res oat r trie seinkle re it andfeimndao a tneencrfe shr td foarto nc rsei aln aaaitrynue nigt tnll cio it,mewo e rencuesfinethe rvsole reg the substitute name.
Decrypted Text File
TASM now supports aliasing. This means that TASM allows the association of an alias name with another name ( called the substitute name ) in a program. Any time that alias name is encountered it will really refer to the substitute name. Aliasing is primarily a linker issue. The alias statement will generate an alias record setting the alias name equal to the substitute name, e.g.: 000056 ALIAS '_Set_Coords' = '_SetCoords' When the linker tries to resolve a reference to a name and it finds an alias record for that name, it will continue trying to resolve the reference using the substitute name.
Some 60-bit Catalan Numbers
|
|||||||||||||||||||||||||||||||||||||||
| Copyright by Chasan
Chouse |