-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm4CbcTest.java
More file actions
68 lines (58 loc) · 2.33 KB
/
Sm4CbcTest.java
File metadata and controls
68 lines (58 loc) · 2.33 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
65
66
67
68
/*
* 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 Sm4Cbc unit test
*/
public class Sm4CbcTest {
Sm4Cbc sm4Cbc;
byte[] key , iv ;
@Before
public void beforeTest(){
sm4Cbc = new Sm4Cbc();
key = new byte[]{-73, -55, -122, -95, 0, -4, 51, -38, 125, -31, 38, 12, 112, 8, -50, -92};
iv = new byte[]{88, 121, -51, 88, 32, -85, 98, 56, 108, 18, 102, -73, -122, -59, -97, -25};
}
@Test
public void encryptTest(){
String testStr="gmssl";
byte[] plaintext = testStr.getBytes();
byte[] ciphertext = new byte[plaintext.length+Sm4Cbc.BLOCK_SIZE];
sm4Cbc.init(key, iv, true);
int cipherlen = sm4Cbc.update(plaintext, 0, plaintext.length, ciphertext, 0);
cipherlen += sm4Cbc.doFinal(ciphertext, cipherlen);
byte[] ciphertext1 =Arrays.copyOfRange(ciphertext,0,cipherlen);
//System.out.println("cipher:"+HexUtil.byteToHex(ciphertext1));
Assert.assertNotNull("data is empty exception!",HexUtil.byteToHex(ciphertext1));
}
@Test
public void decryptTest(){
String cipherHex="ccedec05b742098b33e0fc8c5c006365";
byte[] ciphertext=HexUtil.hexToByte(cipherHex);
sm4Cbc.init(key, iv, false);
byte[] decrypted = new byte[ciphertext.length + Sm4Cbc.BLOCK_SIZE]; // prepare large enough plaintext buffer
int decryptedOffset = 0;
int decryptedLen;
int ciphertextOffset = 0;
decryptedLen = sm4Cbc.update(ciphertext, ciphertextOffset, ciphertext.length, decrypted, decryptedOffset);
decryptedOffset += decryptedLen;
decryptedLen += sm4Cbc.doFinal(decrypted, decryptedOffset);
byte[] plaintext =Arrays.copyOfRange(decrypted,0,decryptedLen);
String plaintextStr=new String(plaintext);
//System.out.println(plaintextStr);
Assert.assertEquals("original value is not equal to the expected value after decryption!","gmssl",plaintextStr);
}
}