This project is designed for encrypting and decrypting the text using Java Programming Language.
This project is designed for encrypting and decrypting the text using Java Programming Language.
The Affine cipher is a type of monoalphabetic substitution cipher, wherein each letter is an alphabet is mapped to its numeric equivalent and encrypted by using a simple mathematical function and converted back to a letter. It uses two keys for encryption and decryption.
Formulas
1.) For Encryption
E(x) = ( a.pt + b) mod m
2.) For Decryption
D(x) = a-1( ct - b) mod m
where
a and b denote the keys.
m denotes the size of the alphabets. By default, it is 26.
pt denotes plaintext.
ct denotes ciphertext.
a-1 denotes the modular multiplicative inverse of a mod 26.
E and D denote Encryption and Decryption.
NOTE: a and m should be coprime means a and m has only one common factor i.e. 1
How to find a-1?
1 = a.a-1 mod 26
For Example Encrypt CodersPacket using Affine Cipher by using 5 and 2 as keys.
Step 1: Check for coprime:- 5 and 26 are coprime because 1 is the only common factor between them.
Step 2: Write the respective key values of the plain text and then apply the encryption formula and get the ciphertext.
Plaintext |
Input Key-Value |
Formula : ( ax + b) mod 26 |
Output Key-Value |
Ciphertext |
C |
02 |
( 5*2 + 2 ) mod 26 |
12 |
M |
O |
14 |
( 5*14 + 2 ) mod 26 |
20 |
U |
D |
03 |
( 5*03 + 2 ) mod 26 |
17 |
R |
E |
04 |
( 5*04 + 2 ) mod 26 |
22 |
W |
R |
17 |
( 5*17 + 2 ) mod 26 |
09 |
J |
S |
18 |
( 5*18 + 2 ) mod 26 |
14 |
O |
P |
15 |
( 5*15 + 2 ) mod 26 |
25 |
Z |
A |
00 |
( 5*00 + 2 ) mod 26 |
02 |
C |
C |
02 |
( 5*02 + 2 ) mod 26 |
12 |
M |
K |
10 |
( 5*10 + 2 ) mod 26 |
00 |
A |
E |
04 |
( 5*04 + 2 ) mod 26 |
22 |
W |
T |
19 |
( 5*19 + 2 ) mod 26 |
19 |
T |
So the encrypted message is: MURWJOZCMAWT
For Example Decrypt MURWJOZCMAWT using Affine Cipher by using 5 and 2 as keys.
Step 1: Check for coprime:- 5 and 26 are coprime because 1 is the only common factor between them.
Step 2: Find a-1 : 1=a.a-1 mod 26
1=5.a-1 mod 26 => put a-1 = 1 then 5 mod 26 == 1 => 5!=1
1=5.a-1 mod 26 => put a-1 = 2 then 10 mod 26 == 1 => 10!=1
1=5.a-1 mod 26 => put a-1 = 3 then 15 mod 26 == 1 => 15!=1
1=5.a-1 mod 26 => put a-1 = 4 then 20 mod 26 == 1 => 20!=1
1=5.a-1 mod 26 => put a-1 = 5 then 25 mod 26 == 1 => 25!=1
1=5.a-1 mod 26 => put a-1 = 21 then 105 mod 26 == 1 => 1==1
So a-1 = 21
Step 2: Write the respective key values of the ciphertext and then apply the decryption formula and get the plaintext.
Ciphertext |
Input Key-Value |
Formula : a-1( x - b) mod 26 |
Output Key-Value |
Plaintext |
M |
12 |
21( 12 - 2) mod 26 |
02 |
C |
U |
20 |
21( 20 - 2) mod 26 |
14 |
O |
R |
17 |
21( 17 - 2) mod 26 |
03 |
D |
W |
22 |
21( 22 - 2) mod 26 |
04 |
E |
J |
09 |
21( 09 - 2) mod 26 |
17 |
R |
O |
14 |
21( 14 - 2) mod 26 |
18 |
S |
Z |
25 |
21( 25 - 2) mod 26 |
15 |
P |
C |
02 |
21( 02 - 2) mod 26 |
00 |
A |
M |
12 |
21( 12 - 2) mod 26 |
02 |
C |
A |
00 |
21( 00 - 2) mod 26 |
10 |
K |
W |
22 |
21( 22 - 2) mod 26 |
04 |
E |
T |
19 |
21( 19 - 2) mod 26 |
19 |
T |
So the decrypted message is: CODERSPACKET
Possible Outputs :
Submitted by Shiddhant Gupta (Shiddhant123)
Download packets of source code on Coders Packet
Comments