· 7 years ago · Nov 11, 2018, 02:14 PM
1import java.math.BigInteger;
2import java.nio.charset.StandardCharsets;
3
4import javax.crypto.Cipher;
5import javax.crypto.KeyGenerator;
6import javax.crypto.SecretKey;
7import javax.crypto.spec.GCMParameterSpec;
8
9public class Scribble {
10
11 public static void main(String[] args) throws Exception {
12
13 SecretKey secret = KeyGenerator.getInstance("AES").generateKey();
14 byte[] iv = new byte[16];
15
16 Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
17 cipher.init(Cipher.ENCRYPT_MODE, secret, new GCMParameterSpec(128, iv));
18
19 byte[] ctFromEmpty = cipher.doFinal(new byte[16]);
20
21 cipher = Cipher.getInstance("AES/GCM/NoPadding");
22 cipher.init(Cipher.ENCRYPT_MODE, secret, new GCMParameterSpec(128, iv));
23
24 byte[] plain = "--Hello World!--".getBytes(StandardCharsets.US_ASCII);
25
26 byte[] ct = cipher.doFinal(plain);
27
28 byte[] zeroCiphertext = new byte[16];
29 System.arraycopy(ctFromEmpty, 0, zeroCiphertext, 0, 16);
30
31 byte[] targetCiphertext = new byte[16];
32 System.arraycopy(ct, 0, targetCiphertext, 0, 16);
33
34 byte[] result = new BigInteger(1, zeroCiphertext).xor(new BigInteger(1,targetCiphertext)).toByteArray();
35
36 System.out.println(new String(result, StandardCharsets.US_ASCII));
37 }
38}