Skip to content

Commit 8836993

Browse files
authored
Merge pull request codec-akash#38 from canigethellomoto/affine
Added AffineCipher.java
2 parents 96e7879 + 28eb41c commit 8836993

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

AffineCipher.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.io.*;
2+
3+
class AffineCipher {
4+
public static void main(String[] args) throws Exception {
5+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+
File input = new File("FILE PATH FOR INPUT");
7+
File output = new File("FILE PATH FOR OUTPUT");
8+
BufferedReader br1 = new BufferedReader(new FileReader(input));
9+
BufferedWriter or = new BufferedWriter(new FileWriter(output));
10+
System.out.println("Select 1 for Encryption and 2 for decryption: ");
11+
int choice = Integer.parseInt(br.readLine());
12+
if (choice == 1) {
13+
String pt = br1.readLine();
14+
15+
System.out.print("Enter the value of a in Encryption formula (must be coprime to 26): ");
16+
int a = Integer.parseInt(br.readLine());
17+
if (!isCoprime(a, 26)) {
18+
System.out.println("!!Enter a value CoPrime to 26!!\nRun Again with proper value of A");
19+
} else {
20+
System.out.print("Enter the value of b in Encryption formula: ");
21+
int b = Integer.parseInt(br.readLine());
22+
23+
or.write("Encrypted Ciphertext is: " + encryptMessage(pt.toCharArray(), a, b));
24+
25+
}
26+
} else {
27+
String pt = br1.readLine();
28+
System.out.print("Enter the value of a in Encryption formula (must be coprime to 26): ");
29+
int a = Integer.parseInt(br.readLine());
30+
if (!isCoprime(a, 26)) {
31+
System.out.println("!!Enter a value CoPrime to 26!!\nRun Again with proper value of A");
32+
} else {
33+
System.out.print("Enter the value of b in Encryption formula: ");
34+
int b = Integer.parseInt(br.readLine());
35+
36+
or.write("Decrypted Plaintext is: " + decryptCipher(pt, a, b));
37+
38+
}
39+
}
40+
or.close();
41+
}
42+
43+
static String encryptMessage(char[] msg, int a, int b) {
44+
45+
String cipher = "";
46+
for (int i = 0; i < msg.length; i++) {
47+
if (msg[i] != ' ') {
48+
cipher = cipher + (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
49+
} else {
50+
cipher += msg[i];
51+
}
52+
}
53+
return cipher;
54+
}
55+
56+
static String decryptCipher(String cipher, int a, int b) {
57+
String msg = "";
58+
int a_inv = 0;
59+
int flag = 0;
60+
61+
for (int i = 0; i < 26; i++) {
62+
flag = (a * i) % 26;
63+
if (flag == 1) {
64+
a_inv = i;
65+
}
66+
}
67+
for (int i = 0; i < cipher.length(); i++) {
68+
if (cipher.charAt(i) != ' ') {
69+
msg = msg + (char) (((a_inv * ((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
70+
} else {
71+
msg += cipher.charAt(i);
72+
}
73+
}
74+
75+
return msg;
76+
}
77+
78+
static int gcd(int a, int b) {
79+
if (b == 0)
80+
return a;
81+
return gcd(b, a % b);
82+
}
83+
84+
static boolean isCoprime(int a , int b){
85+
if (gcd(a,b) == 1)
86+
return true;
87+
return false;
88+
}
89+
}

0 commit comments

Comments
 (0)