The following code produces a text file with an encoded message derived from a different, readable textfile.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
main()
{
FILE *f, *f_enc;
char c, c_enc;
int ind, a[26], r_val;
srandom(time(NULL));
for (ind=0; ind<26; ind++) a[ind]=-1;
for (ind=0; ind<26; ind++){
do
r_val=random()%26;
while (a[r_val]!=-1);
a[r_val]=ind;
}
f=fopen("plain.txt","r");
f_enc=fopen("cipher.txt","w");
while (!feof(f)) {
c=fgetc(f);
if ((c>='A' && c<='Z')||(c>='a' && c<='z')) {
if (c>='A' && c<='Z') c = c + 'a' - 'A';
c=a[c-'a']+'a';
}
fputc(c,f_enc);
}
fclose(f);
fclose(f_enc);
}
What I am unclear of is the overal algorithm the code is implementing to encode a message.
- Is it subtracting an ASCII value ('a - 'A) from the character to produce an encoded character?
An example output is given below:
xohq xpu ouuf yubliu spieqxkhq, hzj hcc xpilnap xpu plnqu
tln slncj puhi ku yuhxeza xpu slkmnxui oexp xpu klnqu.
xpu jeqf ohq pnza nm ez xpu jeqf jieru oexp shiu,
hzj, zuujcuqq xl qht, e zuhict mnccuj lnx kt phei.
e ahru nm bli xphx zeapx, alx iuhjt bli yuj,
hzj olzjuiuj, "qplncj e gnqx ynt shijq ezqxuhj?"
khkh hzj jhjjt phj gnqx pex xpu pht,
ql e xlcj xpuk allj zeapx, hzj ouzx lz kt oht.
I'm going to try to reverse engineer this to see if I can get ahold of what's going on. Any speculation is welcomed!
aarray used in the encryption process.