Reputation: 41
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;
}
Upvotes: 0
Views: 699
Reputation: 46435
Your transposition table tells you where the characters ended up. So when you look at the scrambled code and your encryption array, you can decrypt as follows (using your example).
transposition "cypher" = { 2, 4, 0, 1, 3 }'
original string = J a c k s
encrypted string = c k J s a
Now to decrypt, you just look at the encryption string:
for(ii = 0; ii < 5; ii++) {
plainText[ii] = encrypted[cypher[ii]];
}
See that this works:
ii = 0: cypher[0] = 2, put 'J' into position '0' J....
ii = 1: cypher[1] = 4, put 'a' into position '1' Ja...
ii = 2: cypher[2] = 0, put 'c' into position '2' Jac..
and you can finish if from here.
Note that this presumes that you have the entire encrypted string read in when you start the decoding as the above code "jumps around" in the encoded string. If you cannot do that, you need to find the "transpose" of the encryption string. This means you have to find the index in the original string corresponding to the first, second etc. location in the encrypted string. You generate this with
int encrypt[]= {2,4,0,1,3};
int decrypt[5];
for(ii=0; ii<5, ii++) {
decrypts[encrypt[ii]]=ii;
}
This will put the following values in the decryption array:
decrypt[2]=0;
decrypt[4]=1;
decrypt[0]=2;
decrypt[1]=3;
decrypt[3]=4;
And new we decrypt the string as it comes in, by putting it back in the right place:
decrypt[] = {2, 3, 0, 4, 1}; // after the above loop we have the "inverse transposition cypher"
encrypted = "ckJsa";
converting to plaintext:
c -> 2 ..c..
k -> 3 ..ck.
J -> 0 J.ck.
s -> 4 J.cks
a -> 1 Jacks
I hope this clears up the confusion.
Upvotes: 2