-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
62 lines (52 loc) · 2.67 KB
/
CaesarCipher.java
File metadata and controls
62 lines (52 loc) · 2.67 KB
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
import java.math.BigInteger;
public class CaesarCipher {
public static void main(String[] args) {
String message = "This is a secret message";
BigInteger shift = new BigInteger("123");
BigInteger[] private_key ={new BigInteger("17"),new BigInteger("3233")};
BigInteger [] public_key = {new BigInteger("2753"),new BigInteger("3233")};
int s = shift.intValue();
// Convert the message to a char array
char[] chars = message.toCharArray();
// Encrypt each character in the message
for (int i = 0; i < chars.length; i++) {
// Shift the character by the specified amount
char c = chars[i];
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
c = (char) (((c - base + s) % 26) + base);
}
chars[i] = c;
}
// Convert the encrypted char array back to a string and print it to the console
String encryptedMessage = new String(chars);
System.out.println("Encrypted message: " + encryptedMessage);
shift = createSecretShift(private_key, shift);
System.out.println("hello "+shift);
String encryptedMessage2 = encryptedMessage;
BigInteger shift1 = getshiftkey(public_key, shift);
int f = shift1.intValue();
System.out.println("hello "+shift1);
// Convert the encrypted message to a char array
char[] chars1 = encryptedMessage2.toCharArray();
// Decrypt each character in the encrypted message
for (int i = 0; i < chars1.length; i++) {
// Shift1shift1 the character back by the specified amount
char c = chars1[i];
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
c = (char) (((c - base - f) % 26 + 26) % 26 + base);
}
chars1[i] = c;
}
// Convert the decrypted char array back to a string and print it to the console
String decryptedMessage = new String(chars1);
System.out.println("Decrypted message: " + decryptedMessage);
}
public static BigInteger getshiftkey(BigInteger[] public_key,BigInteger secret_shift){
return (secret_shift.pow(2753)).mod(public_key[1]);
}
public static BigInteger createSecretShift (BigInteger[] private_key,BigInteger shift){
return (shift.pow(17)).mod( private_key[1]);
}
}