42 lines
1.2 KiB
Markdown
42 lines
1.2 KiB
Markdown
|
|
Test Java codes:
|
|
```java
|
|
public class AesCtrTest {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
final byte[] key = new byte[]{
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
|
|
};
|
|
final byte[] iv = new byte[]{
|
|
1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0
|
|
};
|
|
final SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
|
|
|
|
final Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
|
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
|
|
System.out.println(Bytes.from(cipher.doFinal("hello world".getBytes())).asHex());
|
|
System.out.println(Bytes.from(cipher.doFinal(
|
|
"hello world.hello world.hello world.hello world.hello world.hello world.".getBytes())).asHex());
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
Outputs:
|
|
```
|
|
84ad8d80732490c061177a
|
|
84ad8d80732490c061177a58bd26d032d6fcff2e66f9afe3cf95717485d3a4485d4a2a7bd835df3d0756b8192e3bf5a287ad8dd81942c43bc812c82d666ebbb34df4e2a5069467d9
|
|
```
|
|
|
|
<br>
|
|
|
|
Java `nonce` counter:
|
|
|
|
`com.sun.crypto.provider.CounterMode`
|
|
|
|
```java
|
|
private static void increment(byte[] nonce) {
|
|
for(int index = nonce.length - 1; index >= 0 && ++nonce[index] == 0; --index) {
|
|
}
|
|
}
|
|
``` |