This commit is contained in:
Looly
2024-01-08 21:57:53 +08:00
parent 6fba25ad7d
commit 46febc3d05
8 changed files with 252 additions and 73 deletions

View File

@@ -0,0 +1,28 @@
package org.dromara.hutool.crypto.bc;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.DefaultBufferedBlockCipher;
import org.bouncycastle.crypto.engines.SM4Engine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.dromara.hutool.core.util.ByteUtil;
import org.dromara.hutool.crypto.CipherMode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class BCCipherTest {
@Test
void sm4Test() {
final byte[] data = ByteUtil.toUtf8Bytes("我是测试Hutool的字符串00");
final BufferedBlockCipher blockCipher = new DefaultBufferedBlockCipher(CBCBlockCipher.newInstance(new SM4Engine()));
final BCCipher bcCipher = new BCCipher(blockCipher);
bcCipher.init(CipherMode.ENCRYPT, new BCCipher.BCParameters(new KeyParameter(ByteUtil.toUtf8Bytes("1234567890000000"))));
final byte[] encryptData = bcCipher.processFinal(data);
bcCipher.init(CipherMode.DECRYPT, new BCCipher.BCParameters(new KeyParameter(ByteUtil.toUtf8Bytes("1234567890000000"))));
final byte[] decryptData = bcCipher.processFinal(encryptData);
Assertions.assertArrayEquals(data, decryptData);
}
}

View File

@@ -135,9 +135,9 @@ public class SymmetricTest {
final AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
// 加密为16进制表示
aes.setMode(CipherMode.encrypt);
aes.setMode(CipherMode.ENCRYPT);
final String randomData = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
aes.setMode(CipherMode.encrypt);
aes.setMode(CipherMode.ENCRYPT);
final String randomData2 = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
Assertions.assertEquals(randomData2, randomData);
Assertions.assertEquals(randomData, "cd0e3a249eaf0ed80c330338508898c4");