-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathSm4CtrExample.java
More file actions
50 lines (41 loc) · 1.42 KB
/
Sm4CtrExample.java
File metadata and controls
50 lines (41 loc) · 1.42 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
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
import org.gmssl.Sm4Ctr;
import org.gmssl.Random;
public class Sm4CtrExample {
public static void main(String[] args) {
Random rng = new Random();
byte[] key = rng.randBytes(Sm4Ctr.KEY_SIZE);
byte[] iv = rng.randBytes(Sm4Ctr.IV_SIZE);
byte[] ciphertext = new byte[64];
byte[] plaintext = new byte[64];
int cipherlen;
int plainlen;
int i;
Sm4Ctr sm4ctr = new Sm4Ctr();
sm4ctr.init(key, iv);
cipherlen = sm4ctr.update("abc".getBytes(), 0, "abc".length(), ciphertext, 0);
cipherlen += sm4ctr.update("12345678".getBytes(), 0, "12345678".length(), ciphertext, cipherlen);
cipherlen += sm4ctr.update("xxyyyzzz".getBytes(), 0, "xxyyyzzz".length(), ciphertext, cipherlen);
cipherlen += sm4ctr.doFinal(ciphertext, cipherlen);
System.out.print("ciphertext : ");
for (i = 0; i < cipherlen; i++) {
System.out.printf("%02x", ciphertext[i]);
}
System.out.print("\n");
sm4ctr.init(key, iv);
plainlen = sm4ctr.update(ciphertext, 0, cipherlen, plaintext, 0);
plainlen += sm4ctr.doFinal(plaintext, plainlen);
System.out.print("plaintext : ");
for (i = 0; i < plainlen; i++) {
System.out.printf("%02x", plaintext[i]);
}
System.out.print("\n");
}
}