Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Cryptography
3.
Affine Cipher
4.
Formula Used 
4.1.
Example
4.2.
Encryption 
4.3.
Decryption
5.
Implementation
5.1.
Code
5.2.
Output
6.
Weakness
7.
Frequently Asked Questions
7.1.
What is cryptography?
7.2.
What is an affine cipher?
7.3.
What are encryption and decryption?
7.4.
How much is affine cipher secure?
7.5.
What are plain text and ciphertext?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Special thy Affine Cipher

Author Ayushi Goyal
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Cryptography is the technique used for securing communication. It uses encryption and decryption algorithms to protect data. Encryption is the process of converting plain text (human-readable) text into cipher text. Decryption is the process of converting non-human readable text back into plain text. These algorithms are called Cipher. 

introduction

This article will discuss one cryptographic method, the affine cipher. Then we will see the formula by which encryption and decryption are performed. We will also discuss the implementation of the affine cipher. Lastly, we will discuss the weaknesses of the affine cipher. But before that, let's see what cryptography is.

Cryptography

It is the study of encrypting and decrypting data using mathematics. It allows you to store sensitive information or send it through insecure networks. Cryptography can be powerful or weak. The time and resources required to determine its strength. Strong cryptography produces ciphertext that is extremely difficult to decode. 

cryptography

Cryptography is a word used in computing to describe secured information systems that transform messages in difficult hard-to-decipher ways using mathematical concepts and a sequence of rule-based computations known as algorithms. These proposed approaches are used for encryption keys generation, digital signature, data privacy verification, online surfing, and private communications, including banking transactions and email.

types

There are mainly three types of cryptography techniques.

  1. Public key or symmetric key cryptography
  2. Secret key or asymmetric key cryptography
  3. Hash functions
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Affine Cipher

The combination of shifting and multiplication operations makes an affine cipher. It is a monoalphabetic substitution cipher. Affine cipher maps each alphabet with its numerical equivalent. Moreover, it uses a mathematical function to encrypt and decrypt. Ceasar/shift cipher is also an example of the affine cipher. 

affine cipher

This cipher is named after the function used i.e., the affine function. Let’s now see what formulas are used by the affine cipher. 

Formula Used 

The formula used for encryption is - 

E(m) = (am + b) mod 26.

 

The formula of decryption is

D(m) = a^-1 (m - b) mod 26

 

Where 

  • E depicts encryption 
  • D depicts decryption
  • m denotes the letter-value
  • a and b denotes the key-value
  • a^-1 is the modular multiplicative inverse of a mod 26. It can be calculated using the below formula by hit and trial- a*a^-1 mod 26 = 1
  • a should be chosen as co-prime with 26. 
  • The possible values of a are - 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.

Let’s see an example for a better understanding. 

Example

Let’s say we want to encrypt the word ‘NINJA’ and the keys 5 and 8. The table of letters used will be as follows - 

A

B

C

D

E

F

G

H

I

J

K

L

M

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

Encryption 

The encryption formula will be - 

E = (5m + 8) mod 26

If we apply the encryption formula on the word ‘NINJA’, then the ciphertext will be -

Plaintext

Encryption - (5m + 8)%26

Ciphertext

N - 13

(5 * 13 + 8)%26

21 - V

I - 08

(5 * 08 + 8)%26

22 - W

N - 13

(5 * 13 + 8)%26

21 - V

J - 09

(5 * 09 + 8)%26

01 - B

A - 00

(5 * 00 + 8)%26

08 - I

So, the encrypted message is ‘VWVBI’

Decryption

The value of a^-1 will be 21.  So the decryption formula will be - 

D = 21(m - 8) %26

Now use this decryption formula on the ciphertext. The plaintext received will be as follows -

Ciphertext

Decryption = 21(m - 8) %26

Plaintext

V - 21

21(21 - 8)%26

13 - N 

W - 22

21(22 - 8)%26

08 - I

V - 21

21(21 - 8)%26

13 - N

B - 01

21(01 - 8)%26

09 - J

I - 08

21(08 - 8)%26

00 - A

Yes, we got our original message, i.e., ‘NINJA’, back.

Implementation

Let’s see the implementation of affine cipher - 

Code

// Program Of Affine Cipher
#include<bits/stdc++.h>
using namespace std;

// Define Key values of a and b
const int a = 5;
const int b = 8;

// Define encryption function
string encryption(string m)
{
	string c = "";
	int n = m.length();
	for (int i=0;i<n;i++)
	{
		// Avoid space
		if(m[i]==' ')
		c += m[i];
		else
		c += ((char) ((( (a * (m[i]-'A')) + b) % 26) + 'A'));
	}

	// Return cipherText
	return c;
}

// Define decryption function
string decrypion(string c)
{
	string m = "";
	int aInverse = 0;
	int temp = 0;

	// Find value of a inverse
	for (int i=0;i<26;i++)
	{
		temp = (a*i)%26;
		// Check the condition (a*i)%26 == 1
		if (temp == 1)
		aInverse = i;
	}

	int n = c.length();
	for (int i = 0;i<n;i++)
	{

    	// Avoid space
		if(c[i]==' ')
		m += c[i];
		else
		m += ((char) (((aInverse * ((c[i]+'A' - b)) % 26)) + 'A'));
	}

	// Return Message
	return m;
}

int main()
{
	string message = "CODING NINJAS";

	// Call encryption function
	string c = encryption(message);
	cout << "Encrypted Message is : " << c<<endl;

	//Calling Decryption function
	message = decrypion(c);
	cout << "Decrypted Message is: " <<message;

	return 0;
}

Output

output

Weakness

Affine cipher is a substitution cipher, so it inherits all its weaknesses. The caesar cipher is a type of affine cipher in which the value of ‘a’ is 1. 

weaknesses

The number of possible key combinations in this cipher is very small. This allows attacker to use brute force to hack it. The total number of encryption letters is 26[alphabets]. There are only 12 numbers that are co-prime with 26. So possible values for key ‘a’ = 12, and for ‘b’ = 26. The total number of possible keys = 12*26 = 312. Such less numbers of key possibilities make the system insure. 
 

The attacker can detect the message using frequency analysis. It is the study of the frequency of a group of letters in a ciphertext. 
 

Affine cipher is similar to linear congruential generators. These are cryptographically secure. Due to the same reason, the affine cipher is also not secure. 

Frequently Asked Questions

What is cryptography?

Cryptography is a method of encrypting and decrypting messages and information to establish a secure information exchange between the sender and the receiver.

What is an affine cipher?

Affine cipher is a monoalphabetic substitution cipher. This cipher maps each alphabet with its numerical equivalent. And it uses a mathematical function to encrypt and decrypt. 

What are encryption and decryption?

Encryption is the conversion of plain text into ciphertext. In contrast, decryption is the process that converts ciphertext into plaintext.

How much is affine cipher secure?

The affine cipher is less secure than the substitution cipher. It includes all the weaknesses of substitution cipher, along with other attacks. 

What are plain text and ciphertext?

The plaintext can refer to anything that humans can understand. In contrast, the ciphertext is a series of randomized letters that don't make any sense to humans. 

Conclusion

In this article, we have discussed the affine cipher. We also discussed the formula used for encryption and decryption by this Cipher. Using this formula, we implemented an affine cipher. And at last, we discussed the weaknesses of this shift cipher. 

Do not stop learning! We recommend you read these articles -. 

  1. What is Cryptography?
  2. Stream ciphers
  3. Keyword Cipher
  4. Hill Cipher
  5. Difference between Public Key and Private Key
  6. Monte Carlo Simulation
     

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Keep learning, and Keep Growing!

Do upvote our blogs if you find them engaging and knowledgeable.

Previous article
Substitution in Cipher
Next article
Non-monoalphabetic Vigenere Cipher
Live masterclass