-
Notifications
You must be signed in to change notification settings - Fork 82
/
AuthenticatedEncryptionTest.java
54 lines (45 loc) · 2.12 KB
/
AuthenticatedEncryptionTest.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
package org.libsodium.jni.publickey;
import org.libsodium.jni.NaCl;
import org.libsodium.jni.Sodium;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by joshjdevl on 1/24/16.
*/
public class AuthenticatedEncryptionTest {
@Test
public void testAuthenticatedEncryption() {
Sodium sodium= NaCl.sodium();
long alice_publickeylen=Sodium.crypto_box_publickeybytes();
long alice_privatekeylen=Sodium.crypto_box_secretkeybytes();
byte[] alice_public_key=new byte[(int)alice_publickeylen];
byte[] alice_private_key=new byte[(int)alice_privatekeylen];
System.out.println("Generating keypair");
int ret=Sodium.crypto_box_keypair(alice_public_key,alice_private_key);
Assert.assertEquals(0,ret);
System.out.println(ret);
System.out.println("Generated keyapir");
long bob_publickeylen=Sodium.crypto_box_publickeybytes();
long bob_privatekeylen=Sodium.crypto_box_secretkeybytes();
byte[] bob_public_key=new byte[(int)bob_publickeylen];
byte[] bob_private_key=new byte[(int)bob_privatekeylen];
System.out.println("Generating keypair");
ret=Sodium.crypto_box_keypair(bob_public_key,bob_private_key);
Assert.assertEquals(0,ret);
System.out.println(ret);
System.out.println("Generated keyapir");
byte[] message="test".getBytes();
long noncelen=Sodium.crypto_box_noncebytes();
byte[] nonce=new byte[(int)noncelen];
long ciphertextlen=Sodium.crypto_box_macbytes()+message.length;
byte[] ciphertext=new byte[(int)ciphertextlen];
Sodium.randombytes_buf(nonce,(int)noncelen);
ret=Sodium.crypto_box_easy(ciphertext,message,message.length,nonce,bob_public_key,alice_private_key);
Assert.assertEquals(0,ret);
byte[] decrypted=new byte[ciphertext.length - Sodium.crypto_box_macbytes()];
ret=Sodium.crypto_box_open_easy(decrypted, ciphertext, (int)ciphertextlen, nonce,
alice_public_key, bob_private_key);
Assert.assertEquals(0,ret);
System.out.println("Recovered message="+new String(decrypted));
}
}