-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtea.java
73 lines (62 loc) · 1.65 KB
/
tea.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
import java.io.*;
public class tea {
int delta = 0x9e3779b9; // (2^32 golden ratio, key scheduling constant)
int sum = 0;
int k[] = new int[4];
int p[] = new int[2];
int L, R;
public void encrypt() {
//split 32 bits
L = p[0];
R = p[1];
for (int i=1;i<=32;i++) {
sum += delta;
// L += ((R<<4)+K[0]) XOR (R+sum) XOR ((R>>5)+K[1])
L += ( ((R << 4)+(k[0])) ^ (R + sum) ^ ((R >> 5)+(k[1])) );
// R += ((L<<4)+K[2]) XOR (L+sum) XOR ((L>>5)+K[3])
R += ( ((L << 4)+(k[2])) ^ (L + sum) ^ ((L >> 5)+(k[3])) );
}
System.out.println("Ciphertext is "+L,R);
}
public void decrypt() {
}
public void getKey() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = 0, idx = 0;
try {
String str = br.readLine();
while (count <= 3) {
k[count++] = Integer.parseInt(str.substring(idx, idx + 2));
idx += 2;
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
public void getPlainText() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = 0, idx = 0;
try {
String number = br.readLine();
while (count <= 1) {
p[count++] = Integer.parseInt(number.substring(idx, idx + 2));
idx += 2;
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
public static void main(String args[]) {
System.out.println("TEA ENCRYPTION: ");
tea t = new tea();
System.out.println("Enter the key: ");
t.getKey();
System.out.println("Enter the number: ");
t.getPlainText();
t.encrypt();
}
}