C++ Program to Implement Affine Cipher


In the Affine cipher, each letter in an alphabet is mapped to its numeric equivalent, is a type of monoalphabetic substitution cipher. Encryption is done using a simple mathematical function and converted back to a letter.

The letters of an alphabet of size m are first mapped to the integers in the range 0 … m-1, in the Affine cipher,

The ‘key’ for the Affine cipher consists of 2 numbers, a and b. a should be chosen to be relatively prime to m.

Encryption

To transform the integer, it uses modular arithmetic that each plaintext letter corresponds to into another integer that correspond to a cipher text letter. The encryption function for a single letter is

E ( x ) = ( a x + b ) mod m
modulus m: size of the alphabet
a and b: key of the cipher.

Decryption

In decryption, convert each of the cipher text letters into their integer values. The decryption function is

D ( x ) = a^-1 ( x - b ) mod m
a^-1 : modular multiplicative inverse of a modulo m. i.e., it satisfies the equation
1 = a^-1 mod m.

Here is a C++ program to implement this process.

Algorithms

Begin
Function encryption(string m)
   for i = 0 to m.length()-1
      if(m[i]!=' ')
         c = c + (char) ((((a * (m[i]-'A') ) + b) % 26) + 'A')
      else
         c += m[i]
   return c
End
Begin
Function decryption(string c)
   Initialize a_inverse = 0
   Initialize flag = 0
   For i = 0 to 25
      flag = (a * i) % 26
      if (flag == 1)
         a_inverse = i
      done
   done
   For i = 0 to c.length() - 1
      if(c[i]!=' ')
         m = m + (char) (((a_inverse * ((c[i]+'A' - b)) % 26)) + 'A')
      else
         m = m+ c[i]
   done
End

Example

#include<bits/stdc++.h>
using namespace std;
static int a = 7;
static int b = 6;
string encryption(string m) {
   //Cipher Text initially empty
   string c = "";
   for (int i = 0; i < m.length(); i++) {
      // Avoid space to be encrypted
      if(m[i]!=' ')
         // added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
         c = c + (char) ((((a * (m[i]-'A') ) + b) % 26) + 'A');
      else
         //else append space character
         c += m[i];
   }
   return c;
}
string decryption(string c) {
   string m = "";
   int a_inverse = 0;
   int flag = 0;
   //Find a^-1 (the multiplicative inverse of a
   //in the group of integers modulo m.)
   for (int i = 0; i < 26; i++) {
      flag = (a * i) % 26;
      //Check if (a * i) % 26 == 1,
      //then i will be the multiplicative inverse of a
      if (flag == 1) {
         a_inverse = i;
      }
   }
   for (int i = 0; i < c.length(); i++) {
      if(c[i] != ' ')
         // added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
         m = m + (char) (((a_inverse * ((c[i]+'A' - b)) % 26)) + 'A');
      else
         //else append space character
         m += c[i];
   }
   return m;
}
int main(void) {
   string msg = "TUTORIALSPOINT";
   string c = encryption(msg);
   cout << "Encrypted Message is : " << c<<endl;
   cout << "Decrypted Message is: " << decryption(c);
   return 0;
}

Output

Encrypted Message is : JQJAVKGFCHAKTJ
Decrypted Message is: TUTORIALSPOINT

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements