Skip to content

Commit e04ba75

Browse files
authored
Add Affine Cipher (TheAlgorithms#2668)
1 parent fa3c1f5 commit e04ba75

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

Ciphers/AffineCipher.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//The ‘key’ for the Affine cipher consists of 2 numbers, we’ll call them a and b.
2+
// The following discussion assumes the use of a 26 character alphabet (m = 26).
3+
// a should be chosen to be relatively prime to m (i.e. a should have no factors in common with m).
4+
5+
package Ciphers;
6+
7+
import java.util.Scanner;
8+
9+
class AffineCipher
10+
{
11+
static Scanner in = new Scanner(System.in);
12+
13+
static String encryptMessage(char[] msg)
14+
{
15+
System.out.println("Enter key value a for encryption : ");
16+
int a = in.nextInt();
17+
18+
System.out.println("Enter key value b for encryption : ");
19+
int b = in.nextInt();
20+
21+
/// Initially empty cipher String
22+
String cipher = "";
23+
for (int i = 0; i < msg.length; i++)
24+
{
25+
// Avoid space to be encrypted
26+
/* applying encryption formula ( a x + b ) mod m
27+
{here x is msg[i] and m is 26} and added 'A' to
28+
bring it in range of ascii alphabet[ 65-90 | A-Z ] */
29+
if (msg[i] != ' ')
30+
{
31+
cipher = cipher
32+
+ (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
33+
} else // append space character
34+
{
35+
cipher += msg[i];
36+
}
37+
}
38+
return cipher;
39+
}
40+
41+
static String decryptCipher(String cipher)
42+
{
43+
System.out.println("Enter key value a for decryption : ");
44+
int a = in.nextInt();
45+
46+
System.out.println("Enter key value b for decryption : ");
47+
int b = in.nextInt();
48+
49+
String msg = "";
50+
int a_inv = 0;
51+
int flag = 0;
52+
53+
//Find a^-1 (the multiplicative inverse of a
54+
//in the group of integers modulo m.)
55+
for (int i = 0; i < 26; i++)
56+
{
57+
flag = (a * i) % 26;
58+
59+
// Check if (a*i)%26 == 1,
60+
// if so, then i will be the multiplicative inverse of a
61+
if (flag == 1)
62+
{
63+
a_inv = i;
64+
}
65+
}
66+
for (int i = 0; i < cipher.length(); i++)
67+
{
68+
/*Applying decryption formula a^-1 ( x - b ) mod m
69+
{here x is cipher[i] and m is 26} and added 'A'
70+
to bring it in range of ASCII alphabet[ 65-90 | A-Z ] */
71+
if (cipher.charAt(i) != ' ')
72+
{
73+
msg = msg + (char) (((a_inv *
74+
((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
75+
}
76+
else // append space character
77+
{
78+
msg += cipher.charAt(i);
79+
}
80+
}
81+
82+
return msg;
83+
}
84+
85+
// Main method
86+
public static void main(String[] args)
87+
{
88+
String msg = "AFFINE CIPHER";
89+
90+
// Encrypting message
91+
String cipherText = encryptMessage(msg.toCharArray());
92+
System.out.println("Encrypted Message is : " + cipherText);
93+
94+
// Decrypting message
95+
System.out.println("Decrypted Message is: " + decryptCipher(cipherText));
96+
97+
}
98+
}

0 commit comments

Comments
 (0)