-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm4CtrTest.java
More file actions
64 lines (55 loc) · 2.26 KB
/
Sm4CtrTest.java
File metadata and controls
64 lines (55 loc) · 2.26 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* 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
*/
package org.gmssl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
/**
* @author yongfeili
* @email 290836576@qq.com
* @date 2023/09/07
* @description Sm4Ctr unit test
*/
public class Sm4CtrTest {
private Sm4Ctr sm4Ctr;
byte[] key ,iv ;
@Before
public void beforeTest(){
sm4Ctr = new Sm4Ctr();
key=new byte[]{99, -49, -44, -61, 104, 76, -65, 88, 55, 54, 48, -81, 99, -10, 50, 22};
iv=new byte[]{-127, 39, -104, -97, 61, -119, 85, -18, -14, -79, 47, -92, -113, 92, 28, -34};
}
@Test
public void encryptTest(){
String ciphertext_1="gmssl",ciphertext_2="_",ciphertext_3="v3";
byte[] ciphertext = new byte[64];
sm4Ctr.init(key, iv);
int cipherlen = sm4Ctr.update(ciphertext_1.getBytes(), 0, ciphertext_1.length(), ciphertext, 0);
cipherlen += sm4Ctr.update(ciphertext_2.getBytes(), 0, ciphertext_2.length(), ciphertext, cipherlen);
cipherlen += sm4Ctr.update(ciphertext_3.getBytes(), 0, ciphertext_3.length(), ciphertext, cipherlen);
cipherlen += sm4Ctr.doFinal(ciphertext, cipherlen);
byte[] ciphertextEnd = Arrays.copyOfRange(ciphertext,0,cipherlen);
//System.out.println(HexUtil.byteToHex(ciphertextEnd));
Assert.assertNotNull("data is empty exception!",HexUtil.byteToHex(ciphertextEnd));
}
@Test
public void decryptTest(){
String plainText="gmssl_v3";
String ciphertext="912c3317275d8e5f";
byte[] ciphertextByte=HexUtil.hexToByte(ciphertext);
byte[] plaintext = new byte[64];
sm4Ctr.init(key, iv);
int plainlen = sm4Ctr.update(ciphertextByte, 0, ciphertext.length()/2, plaintext, 0);
plainlen += sm4Ctr.doFinal(plaintext, plainlen);
plaintext=Arrays.copyOfRange(plaintext,0,plainlen);
//System.out.println(new String(plaintext));
Assert.assertEquals("original value is not equal to the expected value after decryption!",plainText,new String(plaintext));
}
}