forked from GmSSL/GmSSL-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSm4Test.java
More file actions
54 lines (47 loc) · 1.62 KB
/
Sm4Test.java
File metadata and controls
54 lines (47 loc) · 1.62 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
/*
* 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;
/**
* @author yongfeili
* @email 290836576@qq.com
* @date 2023/10/20
* @description Sm4 unit test
*/
public class Sm4Test {
byte[] key;
@Before
public void beforeTest(){
key=new byte[]{49, 50, 51, 52, 53, 54, 55, 56, 56, 55, 54, 53, 52, 51, 50, 49};
}
@Test
public void encryptTest(){
String plaintextStr="1234567887654321";
byte[] plaintext=plaintextStr.getBytes();
byte[] ciphertext=new byte[Sm4.BLOCK_SIZE];
Sm4 sm4enc = new Sm4(key, true);
sm4enc.encrypt(plaintext, 0, ciphertext, 0);
String ciphertextHex = HexUtil.byteToHex(ciphertext);
//System.out.println(ciphertextHex);
Assert.assertNotNull("data is empty exception!",ciphertextHex);
}
@Test
public void decryptTest(){
String ciphertextHex="4a7dc8fc6f7fb9bac989bbf8a5f194a7";
byte[] ciphertext = HexUtil.hexToByte(ciphertextHex);
byte[] plaintext = new byte[ciphertext.length];
Sm4 sm4dec = new Sm4(key, false);
sm4dec.encrypt(ciphertext, 0, plaintext, 0);
String plaintext1=new String(plaintext);
//System.out.println(plaintext1);
Assert.assertEquals("original value is not equal to the expected value after decryption!","1234567887654321",plaintext1);
}
}