-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJaCl.java
More file actions
143 lines (111 loc) · 3.72 KB
/
JaCl.java
File metadata and controls
143 lines (111 loc) · 3.72 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class JaCl {
static {
System.loadLibrary("JaCl");
}
protected long ptr;
public JaCl() {
this.ptr = initCppSide();
}
public String nonce() {
return nonce(this.ptr);
}
public void setRemoteNonce(String rn) {
setRemoteNonce(this.ptr, rn);
}
public void genNewNonce() {
genNewNonce(this.ptr);
}
public String publicKey() {
return publicKey(this.ptr);
}
public String secretKey() {
return secretKey(this.ptr);
}
public void setRemoteKey(String rk) {
setRemoteKey(this.ptr, rk);
}
public void genNewKeys() {
genNewKeys(this.ptr);
}
public String secret() {
return secret(this.ptr);
}
public void setSecret(String ns) {
setSecret(this.ptr, ns);
}
public void genNewSecret() {
genNewSecret(this.ptr);
}
public String publicDecrypt(String enc_msg) {
return publicDecrypt(this.ptr, enc_msg);
}
public String publicEncrypt(String msg) {
return publicEncrypt(this.ptr, msg);
}
public String secretDecrypt(String enc_msg) {
return secretDecrypt(this.ptr, enc_msg);
}
public String secretEncrypt(String msg) {
return secretEncrypt(this.ptr, msg);
}
public void printTest(String pk) {
printTest(this.ptr, pk);
}
private final native long initCppSide();
private native String nonce(long ptr);
private native void setRemoteNonce(long ptr, String rn);
private native void genNewNonce(long ptr);
private native String publicKey(long ptr);
private native String secretKey(long ptr);
private native void setRemoteKey(long ptr, String rk);
private native void genNewKeys(long ptr);
private native String secret(long ptr);
private native void setSecret(long ptr, String ns);
private native void genNewSecret(long ptr);
private native String publicDecrypt(long ptr, String enc_msg);
private native String publicEncrypt(long ptr, String msg);
private native String secretDecrypt(long ptr, String enc_msg);
private native String secretEncrypt(long ptr, String msg);
private native void printTest(long ptr, String pk);
private static int runTests() {
JaCl sender = new JaCl();
JaCl receiver = new JaCl();
sender.setRemoteNonce(receiver.nonce());
receiver.setRemoteNonce(sender.nonce());
sender.setRemoteKey(receiver.publicKey());
receiver.setRemoteKey(sender.publicKey());
receiver.setSecret(sender.secret());
System.out.println("Testing Public Encryption");
String message = "This is a public encryption test message.";
System.out.println("Test Message: " + message);
String encrypted_message = sender.publicEncrypt(message);
System.out.println("Encrypted Message: " + encrypted_message);
String decrypted_message = receiver.publicDecrypt(encrypted_message);
System.out.println("Decrypted Message: " + decrypted_message);
if (decrypted_message.equals(message)) {
System.out.println("Public encryption test successful!");
}
else {
System.out.println("Public encryption test failed!");
return 1;
}
System.out.println("\nTesting Secret Encryption");
message = "This is a secret encryption test message.";
System.out.println("Test Message: " + message);
encrypted_message = receiver.publicEncrypt(message);
System.out.println("Encrypted Message: " + encrypted_message);
decrypted_message = sender.publicDecrypt(encrypted_message);
System.out.println("Decrypted Message: " + decrypted_message);
if (decrypted_message.equals(message)) {
System.out.println("Secret encryption test successful!");
}
else {
System.out.println("Secret encryption test failed!");
return 1;
}
return 0;
}
public static void main(String[] args) {
System.exit(runTests());
}
}