-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathZucTest.java
More file actions
83 lines (66 loc) · 2.42 KB
/
ZucTest.java
File metadata and controls
83 lines (66 loc) · 2.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
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
/*
* 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.BeforeClass;
import org.junit.Test;
import java.util.Arrays;
/**
* @author yongfei.li
* @email 290836576@qq.com
* @date 2023/09/12
* @description zuc unit test
*/
public class ZucTest {
byte[] key , iv;
Zuc zuc;
@Before
public void beforeTest(){
zuc = new Zuc();
key=new byte[]{-58, -106, -55, 98, -75, 49, -74, -101, -50, 1, -79, 43, -33, -86, -57, -106};
iv=new byte[]{-119, 19, 24, 45, 83, 17, -89, 102, -72, -104, 91, -31, -25, -109, -28, 30};
}
/**
* encryption unit test
*/
@Test
public void encryptTest(){
String plaintextStr = "gmss";
byte[] plaintext = plaintextStr.getBytes();
int ciphertextLen = 2 * Zuc.BLOCK_SIZE * (int)Math.ceil((plaintext.length)/(double)Zuc.BLOCK_SIZE);
byte[] ciphertext = new byte[Math.max(16,ciphertextLen)];
int cipherlen;
zuc.init(key, iv);
cipherlen = zuc.update(plaintext, 0, plaintext.length, ciphertext, 0);
cipherlen += zuc.doFinal(ciphertext, cipherlen);
ciphertext = Arrays.copyOfRange(ciphertext,0,cipherlen);
String ciphertextHex= HexUtil.byteToHex(ciphertext);
//System.out.println(ciphertextHex);
Assert.assertNotNull("data is empty exception!",ciphertextHex);
}
/**
* decryption unit test
*/
@Test
public void decryptTest(){
String ciphertextHex = "91a99db164";
int plainlen;
byte[] ciphertext=HexUtil.hexToByte(ciphertextHex);
int plaintextLen = 2 * Zuc.BLOCK_SIZE * (int)Math.ceil((ciphertext.length)/(double)Zuc.BLOCK_SIZE);
byte[] plaintext = new byte[Math.max(16,plaintextLen)];
zuc.init(key, iv);
plainlen = zuc.update(ciphertext, 0, ciphertext.length, plaintext, 0);
plainlen += zuc.doFinal(plaintext, plainlen);
plaintext=Arrays.copyOfRange(plaintext,0,plainlen);
String plaintextStr = new String(plaintext);
//System.out.println(plaintextStr);
Assert.assertEquals("original value is not equal to the expected value after decryption!","gmssl",plaintextStr);
}
}