I am having problem wrapping my head around how to reverse the process of my transposition encryption. Which is basically trying to decrypt it. In my head i believe that in order to decrypt it i would need to take in the encrypted text message and then replace it the position in the transposition array.
For example if i used the transposition table, {2 4 0 1 3} i would first scan in the encrypted text into the input array assign the encrypted text as 0-4. So for example, "Jacks" based on the transposition table of {2 4 0 1 3} would be encrypted into "csJak". But then if i scan in "csJak" into an array where c is input[0], s is input[1] and so on.
That way first letter scanned in from the encrypted message would correspond to the third slot in the transposition slot which would be where it suppose to be printed out. However im not sure how to put my idea down into code.
Current Transposition encryption Cipher Code:
#include <stdio.h>
int main(int argc, char *argv[]){
char input3[256];
char ch;
int i, j, k, npos;
FILE *file1=fopen(argv[1], "r");
FILE *file2=fopen(argv[2], "w");
sscanf(argv[3], "%d", &npos);
char transposition[npos];
for(i=0;i<npos;++i){
sscanf(argv[4+i], "%d", &k);
transposition[i] = k;
}
int len= sizeof(transposition);
char temp[len];
while(fgets(input3,sizeof(input3),file1)!=NULL){
i=0;
do {
for(j=0;j<len;j++){
ch = input3[i];
if(ch != '\n' && ch != '\0'){
temp[j] = ch;
++i;
} else {
temp[j] = ' ';
}
}
if(temp[0] != '.')
for(k=0;k<len;k++){
fprintf(file2,"%c", temp[transposition[k]]);
}
}
while(ch != '\n' && ch != '\0');
fprintf(file2,"\n");
}
return 0;
}
Very basic transposition decryption program
Assume the file "decrypttrans1.txt" only has the word "Jacks" in it:
#include <stdio.h>
int main(int argc, char *argv[]){
char input[5], decrypted[256];
int i, j, k, ii;
int transposition[5]={'2','4','0','1','3'};
char plainText[5];
FILE *file1=fopen("decrypttrans1.txt", "r");
for(ii=0; ii<5;ii++) {
decrypt[encrypt[ii]]=ii;
}
for(ii=0;ii<5;ii++){
plainText[ii]=input[decrypt[ii]];
printf("%c", plainText[ii]);
}
}
return 0;
}