-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffineCipher.java
101 lines (82 loc) · 2.45 KB
/
AffineCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.math.BigInteger;
import java.util.Scanner;
/**
*
* @author Manoo
*/
public class AffineCipher{
public static void encryption(String msg,int a,int b)
{
StringBuilder c=new StringBuilder();
msg=msg.toLowerCase();
for (int i = 0; i < msg.length(); i++)
{
char character = msg.charAt(i);
if(character == ' ')
{
c.append(" ");
}
else
{
if (Character.isLetter(character))
{
character = (char) ((a * (character - 'a') + b) % 26 + 'a');
}
}
c.append(character);
}
System.out.println(c.toString().toUpperCase());
}
public static void decryption(String msg,int a,int b)
{
msg=msg.toLowerCase();
StringBuilder p = new StringBuilder();
BigInteger inverse = BigInteger.valueOf(a).modInverse(BigInteger.valueOf(26));
for (int in = 0; in < msg.length(); in++)
{
char character = input.charAt(in);
if(character == ' ')
{
c.append(" ");
}
else (Character.isLetter(character))
{
int decoded = inverse.intValue() * (character - 'a' - b + 26);
character = (char) (decoded % 26 + 'a');
}
p.append(character);
}
System.out.println(p.toString().toUpperCase());
}
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("if you want encryption press 1,if you want decryption press 2");
int n=in.nextInt();
in.nextLine();
if(n==1)
{
System.out.println("Please enter your message");
String msg=in.nextLine();
System.out.println("Please enter a and b values");
System.out.print("a = ");
int a=in.nextInt();
in.nextLine();
System.out.print("b = ");
int b=in.nextInt();
encryption(msg,a,b);
}
if(n==2)
{
System.out.println("Please enter your message");
String msg=in.nextLine();
System.out.println("Please enter a and b values");
System.out.print("a = ");
int a=in.nextInt();
in.nextLine();
System.out.print("b = ");
int b=in.nextInt();
decryption(msg,a,b);
}
}
}