PROJECT
EULER PROBLEM # 59
Each
character on a computer is assigned a unique code and the preferred standard is
ASCII (American Standard Code for Information Interchange). For example,
uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.
For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.
Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.
Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher1.txt (right click and 'Save Link/Target As...'), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.
PROJECT
EULER SOLUTION # 59
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
char keyArray[3],answerArray[3];
int maxSpaces=0;
int isAValidEnglishCharacter(char temp)
{
if(temp<32 || temp>126)
return 0;
return 1;
}
int isKeyPossible(char a,char b,char c,char*arr,int n)
{
int i,flag,spaces;
keyArray[0]=a;
keyArray[1]=b;
keyArray[2]=c;
flag=0;
spaces=0;
for(i=0;i<n;i++)
{
if(((char)keyArray[i%3]^(char)arr[i])==' ')
spaces++;
if(!isAValidEnglishCharacter((char)keyArray[i%3]^(char)arr[i]))
return 0;
}
if(maxSpaces<spaces) // as
the most common english character is space (' ')
{
maxSpaces=spaces;
answerArray[0]=a;
answerArray[1]=b;
answerArray[2]=c;
}
return 1;
}
void findKey(char*arr,int n)
{
char i,j,k;
int l,answer[3];
/*
the encryption key consists of three
lower case characters
*/
for(i='a';i<='z';i++)
for(j='a';j<='z';j++)
for(k='a';k<='z';k++)
if(isKeyPossible(i,j,k,arr,n))
printf("Key found key is %c%c%c\n",i,j,k);
}
int main()
{
char temp;
int answer;
int arr[5000],numberOfElements;
char byteArray[5000];
int i,key;
FILE*fptr;
fptr=fopen("cypher1.txt","r");
if(fptr)
{
for(i=0;!feof(fptr);i++)
{
fscanf(fptr,"%d",&arr[i]);
byteArray[i]=(char)arr[i];
fscanf(fptr," %c",&temp);
}
numberOfElements=i;
/*
eliminating the duplicates from the array */
findKey(byteArray,numberOfElements);
/*
for(i=0;i<numberOfElements;i++)
printf("%c",(key^arr[i]));
*/
answer=0;
/*
keyArray[0]='g';
keyArray[1]='o';
keyArray[2]='d';
*/
for(i=0;i<numberOfElements;i++)
{
printf("%c",(byteArray[i]^answerArray[i%3]));
answer+=(byteArray[i]^answerArray[i%3]);
}
printf("\nanswer
= %d\n",answer);
}
else
printf("There
was some problem in opening the file\n");
return 0;
}
No comments:
Post a Comment