101121MewContenty6627223/View The Affine cipher is a cryptographic mcthod for en
ID: 3752184 • Letter: 1
Question
101121MewContenty6627223/View The Affine cipher is a cryptographic mcthod for encrypting text such that it becomes unreadable to a party without access to the cryptographic key. The encryption and deeryption opcrations are simple shifts of the alphabet letters in a cyclic fashion. Mathematically, for a key k-(, ), the encryption and decryption are defined as Encryption: c-(a-z + ) mod 26 , 1 26 and a 26 Decryption: z = -1 . (e-) mod 26. Hore, i s a multiplicative mverse of a n the group of integen modulo 26. To find the multiplicative inverse of a one needs to find z such that az 1 mod 26. A simple way of finding the inverse of a is to consider all numbers from 1 to 25 and see which one satisfies the equation (1) To illustrate the use of the Affine cipher, consider the eneryption of "defend the east wall of the castle" with key k-(a,B)(6,7. The first letter "" is mapped to mumber 3 (alphabet letters are mumbered from 0 to 25). Inserting 3 to the encryption functions yiclds c- 5 3+7 mod 26 22 mod 26 22 which corresponds to letter "w". Applying the same process for ever letter, the plaintext sentence is translated to "wbgbuw yqb bhty nhkk zg yqb rhtykb" . Now to decode, we first need to find the inverse of 5 modulo 26. Scanning every number from 1 to 25, we observe that 5-21 mod 26 1, so the inverse of 5 is 21. Using 21 for decrypting the first letter "w" (or 22) becomes 21-(22-7) mod 26 3, which reverses back to letter "d". NOTE: The modulo operation for negative numbers is different from the % arithmetic operator in C 112Explanation / Answer
Create 2 files encrypted.txt and decrypted.txt in the same folder before running the program.
#include<stdio.h>
#include<stdlib.h>
int mod(int x, int y) {
while(x<0) {
x+=y;
}
return x%y;
}
int inverse(int x) {
int i;
for(i=1;i<=25;i++) {
if(x*i%26==1) break;
}
return i;
}
char encryptFun(int a, int b, char letter) {
return (char)(mod((a*(letter-'a')+b), 26)+'a');
}
char decryptFun(int a, int b, char letter) {
int ai = inverse(a);
int norm = letter-'a';
return (char) (mod(ai*(norm-b),26)+'a');
}
void decryptFile(int a, int b) {
FILE *fp, *fq;
fp = fopen("encrypted.txt", "r");
fq = fopen("decrypted.txt", "w");
char ch;
while((ch=fgetc(fp))!=EOF) {
char dec = ch;
if((ch>='a'&&ch<='z') || (ch>='A'&& ch<='Z')) dec = decryptFun(a, b, ch);
fprintf(fq, "%c", dec);
}
fclose(fp);
fclose(fq);
}
void encryptFile(int a, int b) {
FILE *fp, *fq;
fp = fopen("encrypted.txt", "w");
fq = fopen("decrypted.txt", "r");
char ch;
while((ch=fgetc(fq))!=EOF) {
char en = ch;
if((ch>='a'&&ch<='z') || (ch>='A'&& ch<='Z')) en = encryptFun(a, b, ch);
fprintf(fp, "%c", en);
}
fclose(fp);
fclose(fq);
}
int main() {
do {
printf("Enter the decryption key: ");
int a, b;
scanf("%d %d", &a, &b);
if(a!=5 || b!=7) {
printf("Wrong key ");
continue;
}
decryptFile(a,b);
printf("File decrypted! Do you want to reencrypt the file (y/n)?");
char op;
scanf(" %c", &op);
if(op==(int)'n') break;
else {
printf("Enter an encryption key: ");
scanf("%d %d", &a, &b);
encryptFile(a,b);
printf("File encrypted Goodbye ");
break;
}
}while(1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.