1
1
package com .thealgorithms .ciphers ;
2
2
3
+ /**
4
+ * The AffineCipher class implements the Affine cipher, a type of monoalphabetic substitution cipher.
5
+ * It encrypts and decrypts messages using a linear transformation defined by the formula:
6
+ *
7
+ * E(x) = (a * x + b) mod m
8
+ * D(y) = a^-1 * (y - b) mod m
9
+ *
10
+ * where:
11
+ * - E(x) is the encrypted character,
12
+ * - D(y) is the decrypted character,
13
+ * - a is the multiplicative key (must be coprime to m),
14
+ * - b is the additive key,
15
+ * - x is the index of the plaintext character,
16
+ * - y is the index of the ciphertext character,
17
+ * - m is the size of the alphabet (26 for the English alphabet).
18
+ *
19
+ * The class provides methods for encrypting and decrypting messages, as well as a main method to demonstrate its usage.
20
+ */
3
21
final class AffineCipher {
4
22
private AffineCipher () {
5
23
}
@@ -8,45 +26,56 @@ private AffineCipher() {
8
26
static int a = 17 ;
9
27
static int b = 20 ;
10
28
29
+ /**
30
+ * Encrypts a message using the Affine cipher.
31
+ *
32
+ * @param msg the plaintext message as a character array
33
+ * @return the encrypted ciphertext
34
+ */
11
35
static String encryptMessage (char [] msg ) {
12
- /// Cipher Text initially empty
36
+ // Cipher Text initially empty
13
37
String cipher = "" ;
14
38
for (int i = 0 ; i < msg .length ; i ++) {
15
39
// Avoid space to be encrypted
16
- /* applying encryption formula ( a x + b ) mod m
40
+ /* applying encryption formula ( a * x + b ) mod m
17
41
{here x is msg[i] and m is 26} and added 'A' to
18
- bring it in range of ascii alphabet[ 65-90 | A-Z ] */
42
+ bring it in the range of ASCII alphabet [ 65-90 | A-Z] */
19
43
if (msg [i ] != ' ' ) {
20
- cipher = cipher + (char ) ((((a * (msg [i ] - 'A' )) + b ) % 26 ) + 'A' );
44
+ cipher += (char ) ((((a * (msg [i ] - 'A' )) + b ) % 26 ) + 'A' );
21
45
} else { // else simply append space character
22
46
cipher += msg [i ];
23
47
}
24
48
}
25
49
return cipher ;
26
50
}
27
51
52
+ /**
53
+ * Decrypts a ciphertext using the Affine cipher.
54
+ *
55
+ * @param cipher the ciphertext to decrypt
56
+ * @return the decrypted plaintext message
57
+ */
28
58
static String decryptCipher (String cipher ) {
29
59
String msg = "" ;
30
60
int aInv = 0 ;
31
- int flag = 0 ;
61
+ int flag ;
32
62
33
- // Find a^-1 (the multiplicative inverse of a
34
- // in the group of integers modulo m.)
63
+ // Find a^-1 (the multiplicative inverse of a in the group of integers modulo m.)
35
64
for (int i = 0 ; i < 26 ; i ++) {
36
65
flag = (a * i ) % 26 ;
37
66
38
- // Check if (a*i)% 26 == 1,
67
+ // Check if (a * i) % 26 == 1,
39
68
// then i will be the multiplicative inverse of a
40
69
if (flag == 1 ) {
41
70
aInv = i ;
42
71
}
43
72
}
44
73
for (int i = 0 ; i < cipher .length (); i ++) {
45
- /*Applying decryption formula a^-1 ( x - b ) mod m
74
+ /* Applying decryption formula a^-1 * ( x - b) mod m
46
75
{here x is cipher[i] and m is 26} and added 'A'
47
- to bring it in range of ASCII alphabet[ 65-90 | A-Z ] */
76
+ to bring it in the range of ASCII alphabet [ 65-90 | A-Z] */
48
77
if (cipher .charAt (i ) != ' ' ) {
49
- msg = msg + (char ) (((aInv * ((cipher .charAt (i ) + 'A' - b )) % 26 ) ) + 'A' );
78
+ msg += (char ) (((aInv * ((cipher .charAt (i ) - 'A' ) - b + 26 )) % 26 ) + 'A' );
50
79
} else { // else simply append space character
51
80
msg += cipher .charAt (i );
52
81
}
0 commit comments