I'm writing a simple program in which I take a .txt file read it and encrypt or decrypt it with the Playfair algorithm using a 16x16 ASCII cipher. When I decypher a file I get it mostly correct the only issue is that the char 'o' is replaced with '_' and random sections of the text are not deciphered right.
Example:
"as she swam l z}"
d~o*dd/y``6� {;/~v_(tb_)uesp4�prxnc(z
�icehr,P`(yc'eezm"-ickin_)"
This is the section of the code that I use to decipher it. At this point I already generated the matrix and read the input file. In the encryption code if a and b are equal b is set to NULL, the last if statement checks that. The variable besedilo is the whole text of the file and M is the 16x16 matrix.
for(int t = 0; t < besedilo.size()-2;t+=2){
char a = besedilo[t];
char b = besedilo[t+1];
int it, jt, k = 0,f = 0, i=0,j=0;
for(it = 0; it < 16; it++){
for(jt = 0; jt < 16; jt++){
if(a == M[it][jt]){
i = it;
j = jt;
}
if(b == M[it][jt]){
k = it;
f = jt;
}
}
}
if((i != k)&&(j != f)){
a = M[i][f];
b = M[k][j];
}else if(i == k){
a = M[i][(j-1)%16];
b = M[k][(f-1)%16];
}else if(j == f){
a = M[(i-1)%16][j];
b = M[(k-1)%16][f];
}
if(b == NULL){
b = a;
}