Master Solution in C:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char encryption_key[30] = "muyalrdfnzbewvsgxtichokqpj";
char decryption_key[30] = "dktglhpuszweaivyxforbnmqcj";
int shift(int k,int i)
{
if ( i+k > 25 ) return i+k-26;
if ( i+k < 0 ) return 26+i+k;
return i+k;
}
void build_decryption_key()
{
int i;
for ( i=0; i<26; i++ ) decryption_key[encryption_key[i]-97] = i+97;
}
void shift_encryption_key(int k)
{
int i,j=0;
char temp[26];
for (i=0; i<26; i++)
temp[i]=encryption_key[shift(k,i)];
strcpy (encryption_key, temp );
}
char decrypt(char p) { return decryption_key[tolower(p)-97]; }
void decrypt_file()
{
char p;
int x;
while ( (p=getchar()) != '$' )
{
if ( p=='<')
{
scanf("%d",&x);
getchar();
shift_encryption_key(x);
build_decryption_key();
continue;
}
isalpha(p)?putchar(decrypt(p)):putchar(p);
}
}
int main()
{
decrypt_file();
return 0;
}
This was a simple question on Encryption and Decryption.
There were just 2 tricky things about this question. 1st thing is that in the given encrypted message the keys of 'q' and 'x' were not to be found. But as the question speaks it clearly , " in the initial encryption no english letter is encrypted to itself ", the solution is clear. That 'q' is encrypted to 'x' and 'x' is encrypted to 'q'.
Now, here is another point to notice, the initial encryption key is not in a linear manner. So when the encryption keys are shifted like an Caeser Cipher, just shifting the Decryption key won't do the trick. You will have to shift the encryption keys and then build the decryption keys from it.
Rest of the problem is simple... Read -> Decrypt -> Print. :)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char encryption_key[30] = "muyalrdfnzbewvsgxtichokqpj";
char decryption_key[30] = "dktglhpuszweaivyxforbnmqcj";
int shift(int k,int i)
{
if ( i+k > 25 ) return i+k-26;
if ( i+k < 0 ) return 26+i+k;
return i+k;
}
void build_decryption_key()
{
int i;
for ( i=0; i<26; i++ ) decryption_key[encryption_key[i]-97] = i+97;
}
void shift_encryption_key(int k)
{
int i,j=0;
char temp[26];
for (i=0; i<26; i++)
temp[i]=encryption_key[shift(k,i)];
strcpy (encryption_key, temp );
}
char decrypt(char p) { return decryption_key[tolower(p)-97]; }
void decrypt_file()
{
char p;
int x;
while ( (p=getchar()) != '$' )
{
if ( p=='<')
{
scanf("%d",&x);
getchar();
shift_encryption_key(x);
build_decryption_key();
continue;
}
isalpha(p)?putchar(decrypt(p)):putchar(p);
}
}
int main()
{
decrypt_file();
return 0;
}
This was a simple question on Encryption and Decryption.
There were just 2 tricky things about this question. 1st thing is that in the given encrypted message the keys of 'q' and 'x' were not to be found. But as the question speaks it clearly , " in the initial encryption no english letter is encrypted to itself ", the solution is clear. That 'q' is encrypted to 'x' and 'x' is encrypted to 'q'.
Now, here is another point to notice, the initial encryption key is not in a linear manner. So when the encryption keys are shifted like an Caeser Cipher, just shifting the Decryption key won't do the trick. You will have to shift the encryption keys and then build the decryption keys from it.
Rest of the problem is simple... Read -> Decrypt -> Print. :)
No comments:
Post a Comment