-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm4EcbTest.java
More file actions
122 lines (108 loc) · 4.26 KB
/
Sm4EcbTest.java
File metadata and controls
122 lines (108 loc) · 4.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* 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/25
* @description Sm4Ecb unit test
*/
public class Sm4EcbTest {
byte[] key;
@Before
public void beforeTest(){
key=new byte[]{74, 97, -73, 5, -31, 1, -88, -21, -7, -2, -65, 98, 70, 5, -54, 15};
}
@Test
public void encryptTest(){
String test_plaintext="gmssl";
byte[] paddingPlaintext=pkcs7padding(test_plaintext.getBytes(),Sm4.BLOCK_SIZE);
byte[] encrypted = encrypt(paddingPlaintext,key);
//System.out.println("encrypted data:"+HexUtil.byteToHex(encrypted));
Assert.assertNotNull("data is empty exception!",encrypted);
}
@Test
public void decryptTest(){
String test_hex_chipertext="31acce3f0317026c30accba2be9d326f";
String test_plaintext="gmssl";
byte[] encrypted =HexUtil.hexToByte(test_hex_chipertext);
byte[] plaintextArray = decrypt(encrypted,key);
byte[] unpaddingPlaintextArray = pkcs7UnPadding(plaintextArray);
String plaintext=new String(unpaddingPlaintextArray);
//System.out.println("chipertext:"+plaintext);
Assert.assertEquals("original value is not equal to the expected value after decryption!",plaintext,test_plaintext);
}
/**
* The purpose of PKCS7Padding is to pad the data to the block size required by the encryption algorithm, ensuring that the data length meets the requirements of the encryption algorithm.
* In special cases where the data length is already a multiple of the block size, according to the PKCS7 rule, padding is still added at the end.
* This is done to ensure consistent handling of padding during encryption and decryption processes.
* @param byteArray
* @param blockSize
* @return byte[] padding array
*/
private static byte[] pkcs7padding(byte[] byteArray, int blockSize) {
int paddingLength = blockSize - (byteArray.length % blockSize);
byte[] padding = new byte[paddingLength];
Arrays.fill(padding, (byte) paddingLength);
byte[] result = new byte[byteArray.length + padding.length];
System.arraycopy(byteArray, 0, result, 0, byteArray.length);
System.arraycopy(padding, 0, result, byteArray.length, padding.length);
return result;
}
/**
* unPadding the byteArray
* @param byteArray
* @return byte[] unPadding byteArray
* @throws IllegalArgumentException
*/
private static byte[] pkcs7UnPadding(byte[] byteArray) throws IllegalArgumentException {
int paddingSize = byteArray[byteArray.length - 1];
if (paddingSize <= 0 || paddingSize > byteArray.length) {
throw new IllegalArgumentException("Invalid pkcs#7 padding!");
}
for (int i = byteArray.length - paddingSize; i < byteArray.length; i++) {
if (byteArray[i] != paddingSize) {
throw new IllegalArgumentException("Invalid pkcs#7 padding!");
}
}
return Arrays.copyOfRange(byteArray, 0, byteArray.length - paddingSize);
}
/**
* Encrypt data by block
* @param data data to be encrypted
* @param key
* @return byte[] encrypted data
*/
private static byte[] encrypt(byte[] data, byte[] key) {
byte[] ciphertext = new byte[data.length];
Sm4 sm4 = new Sm4(key, true);
for (int i = 0; i < data.length; i += Sm4.BLOCK_SIZE) {
sm4.encrypt(data, i, ciphertext, i);
}
return ciphertext;
}
/**
* Decrypt data by block
* @param data data to be decrypted
* @param key
* @return byte[] decrypted data
*/
private static byte[] decrypt(byte[] data, byte[] key) {
byte[] plaintext=new byte[data.length];
Sm4 sm4 = new Sm4(key, false);
for (int i = 0; i < data.length; i += Sm4.BLOCK_SIZE) {
sm4.encrypt(data, i, plaintext, i);
}
return plaintext;
}
}