-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAuthenticatedEncryptionTest.java
39 lines (31 loc) · 1.44 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
package com.nordea.oss.authenticatedencryption;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
/**
* Copyright (c) 2017. Niels Bo <[email protected]>
* Copyright (c) 2017. Nordea Bank AB
* Licensed under the MIT license (LICENSE.txt)
*/
public class AuthenticatedEncryptionTest {
public static void main(String[] args) throws Exception {
String encryption = makeKey();
String auth = makeKey();
String payload = "{\"email\":\"[email protected]\"}";
System.out.println("Encryption key: " + encryption);
System.out.println("Authorisation key: " + auth);
System.out.println("Payload: " + payload);
AuthenticatedEncryption authenticatedEncryption = new AuthenticatedEncryption(encryption, auth);
String encrypted = authenticatedEncryption.encrypt(payload);
System.out.println("Authenticated and encrypted payload: " + encrypted);
String decrypted = authenticatedEncryption.decrypt(encrypted);
System.out.println("Verified and decrypted: " + decrypted);
}
private static String makeKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();
byte[] secretKeyEncoded = secretKey.getEncoded();
return Base64.getEncoder().encodeToString(secretKeyEncoded);
}
}