2021-10-18 13:43:38 +08:00
|
|
|
package Ciphers;
|
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
class AffineCipher {
|
2021-10-18 13:43:38 +08:00
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
// Key values of a and b
|
|
|
|
static int a = 17;
|
|
|
|
static int b = 20;
|
2021-10-18 13:43:38 +08:00
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
static String encryptMessage(char[] msg) {
|
|
|
|
/// Cipher Text initially empty
|
2021-10-18 13:43:38 +08:00
|
|
|
String cipher = "";
|
2021-10-29 13:19:42 +08:00
|
|
|
for (int i = 0; i < msg.length; i++) {
|
2021-10-18 13:43:38 +08:00
|
|
|
// Avoid space to be encrypted
|
|
|
|
/* applying encryption formula ( a x + b ) mod m
|
|
|
|
{here x is msg[i] and m is 26} and added 'A' to
|
|
|
|
bring it in range of ascii alphabet[ 65-90 | A-Z ] */
|
2021-10-29 13:19:42 +08:00
|
|
|
if (msg[i] != ' ') {
|
2021-10-18 13:43:38 +08:00
|
|
|
cipher = cipher
|
|
|
|
+ (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
|
2021-10-29 13:19:42 +08:00
|
|
|
} else // else simply append space character
|
2021-10-18 13:43:38 +08:00
|
|
|
{
|
|
|
|
cipher += msg[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cipher;
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
static String decryptCipher(String cipher) {
|
2021-10-18 13:43:38 +08:00
|
|
|
String msg = "";
|
|
|
|
int a_inv = 0;
|
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
//Find a^-1 (the multiplicative inverse of a
|
|
|
|
//in the group of integers modulo m.)
|
2021-10-29 13:19:42 +08:00
|
|
|
for (int i = 0; i < 26; i++) {
|
2021-10-18 13:43:38 +08:00
|
|
|
flag = (a * i) % 26;
|
|
|
|
|
|
|
|
// Check if (a*i)%26 == 1,
|
2021-10-29 13:19:42 +08:00
|
|
|
// then i will be the multiplicative inverse of a
|
|
|
|
if (flag == 1) {
|
2021-10-18 13:43:38 +08:00
|
|
|
a_inv = i;
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 13:19:42 +08:00
|
|
|
for (int i = 0; i < cipher.length(); i++) {
|
2021-10-18 13:43:38 +08:00
|
|
|
/*Applying decryption formula a^-1 ( x - b ) mod m
|
|
|
|
{here x is cipher[i] and m is 26} and added 'A'
|
|
|
|
to bring it in range of ASCII alphabet[ 65-90 | A-Z ] */
|
2021-10-29 13:19:42 +08:00
|
|
|
if (cipher.charAt(i) != ' ') {
|
2021-10-18 13:43:38 +08:00
|
|
|
msg = msg + (char) (((a_inv *
|
|
|
|
((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
|
2021-10-29 13:19:42 +08:00
|
|
|
} else //else simply append space character
|
2021-10-18 13:43:38 +08:00
|
|
|
{
|
|
|
|
msg += cipher.charAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
// Driver code
|
|
|
|
public static void main(String[] args) {
|
2021-10-18 13:43:38 +08:00
|
|
|
String msg = "AFFINE CIPHER";
|
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
// Calling encryption function
|
2021-10-18 13:43:38 +08:00
|
|
|
String cipherText = encryptMessage(msg.toCharArray());
|
|
|
|
System.out.println("Encrypted Message is : " + cipherText);
|
|
|
|
|
2021-10-29 13:19:42 +08:00
|
|
|
// Calling Decryption function
|
2021-10-18 13:43:38 +08:00
|
|
|
System.out.println("Decrypted Message is: " + decryptCipher(cipherText));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 13:19:42 +08:00
|
|
|
|