Implementation of Affine Cipher in C++
Affine ciphers are monoalphabetic substitution ciphers in which every letter in the alphabet is mapped to its corresponding numeric value, encrypted using a basic mathematical function, and then converted back to its original letter form.
With a rule dictating which letter goes to which, the cipher is a conventional substitution cipher.
This is because the formula used means that each letter encrypts to another letter and decrypts it back again. Each letter is encrypted with the function used as (ax + b) mod m, where m is the length of the alphabet used, and a and b are two keys of the cipher, where b denotes the magnitude of the shift and a and m should be co-prime, that is gcd(a,m) = 1. Each letter of the alphabet is mapped to the integers in the range of [0, m-1].
In this tutorial, we’ll be assuming that we use the normal English alphabet (both uppercase and lowercase) for the cipher. That means the length of the alphabet we use for the cipher, m is 52. The mapped value of each character is mentioned in the picture below.
Encryption
It is the process in which we use modular arithmetic to transform the mapped integer value of a plaintext character to another mapped integer of another character. That will be our cipher text character. Mentioned below is how the modular algorithm of encryption works:
F(x) = (ax + b) mod m a and b are 2 keys of the cipher m is the length of the alphabet used gcd(a,m) = 1, i.e. a and m must be co-prime The function F(x) returns an integer value corresponding to the mapped value of the ciphertext character.
Decryption
To obtain the plaintext, we apply the inverse functions to the ciphertext obtained after encryption throughout the decryption or deciphering process. The main step is to translate each letter in the ciphertext into its corresponding integer value. Mentioned below is how the modular algorithm of decryption works:
G(x) = a^-1 (x - b) mod m a^-1: Modular Multiplicative Inverse of a modulo m. ( 1 = a * a^-1 mod m ) The function G(x) returns an integer value corresponding to the mapped value of plaintext character.
Algorithm to find Multiplicative Inverse
In modular arithmetic, we say an integer y is the multiplicative inverse of another integer x, if and only if ( x * y ) mod m = 1, and vice-versa. Considering the alphabet we used, we will look for an integer between 0 and 51 and satisfy the inverse equation.
C++ Implementation: Affine Cipher
#include "bits/stdc++.h" using namespace std; const int a = 7; // 1st key used in the encryption function const int b = 12; // 2nd key denoting magnitude of shift const int m = 52; // Magnitude of the alphabet length map<int, char> mp; // Map to store the mapped value of each character of the alphabet void mapping() { for(int i=0; i<52; i++) { if(i<26) { mp[i]=(char)(65+i); //ASCII value of 'A' = 65 } else { mp[i]= (char)(97+i-26); ////ASCII value of 'a' = 97 } } } string encryption(string txt){ //Initialize an empty string string result = ""; for(int i=0; i < txt.length(); i++) { if(txt[i]>='A' && txt[i]<='Z') // Ensure that the character is Uppercase { char c = mp[(((a * (txt[i]-'A'))+b)%26)%m]; result.push_back(c); } else if(txt[i]>='a' && txt[i]<='z') // Ensure that the character is Lowercase { char c = mp[((((a * (txt[i]-'a'))+b)%26)+26)%m]; result.push_back(c); } else result.push_back(txt[i]); // in case it contains any character other than in our alphabet } return result; } string decryption(string txt){ //Initialize an empty string string result = ""; //Find the inverse using our inverse equation int inv = 0; for(int i=0; i<m/2; i++) { int k = (a * i) % (m/2); if(k==1) { inv = i; break; } } for(int i=0; i<txt.length(); i++) { if(txt[i]>='A' && txt[i]<='Z') // If encrypted character is Uppercase { char c = mp[((inv *(txt[i]+'A' - b) % 26))%m]; result.push_back(c); } else if(txt[i]>='a' && txt[i]<='z') // If encrypted character is Lowercase { char c = mp[(((inv *(txt[i]+'a'-'A'+1-b) % 26))+26)%m]; //Ensuring ASCII calculation to find the decrypted character result.push_back(c); } else result.push_back(txt[i]); // in case it contains any character other than in our alphabet } return result; } int main(){ mapping(); string txt = "HeLlO WoRlD"; string res = encryption(txt); string pes = decryption(res); cout<<"The encrypted message is: "<< res << endl; cout<<"The decrypted message is: "<< pes << endl; }
Output
The encrypted message is: JoLlG KgBlH The decrypted message is: HeLlO WoRlD
We got back the decrypted message from the encrypted cipher. Hence, the function works fine.
Leave a Reply