1

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!

1
  • 1
    It seems like it would be difficult to decrypt the cipher text without a copy of the original a array used in the encryption process. Commented Dec 3, 2012 at 16:50

1 Answer 1

4

This is a simple substitution, such as is used in a cryptogram puzzle. It can be solved with letter frequency analysis.

There exist automated solvers. The original text for the example is:

twas the week before christmas, and all through the house you could hear me beating the computer with the mouse. the disk was hung up in the disk drive with care, and, needless to say, i nearly pulled out my hair. i gave up for that night, got ready for bed, and wondered, 'should i just buy cards instead?' mama and daddy had just hit the hay, so i told them good night, and went on my way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.