mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -13,8 +13,8 @@ public class BCUtilTest {
|
||||
*/
|
||||
@Test
|
||||
public void createECPublicKeyParametersTest() {
|
||||
String x = "706AD9DAA3E5CEAC3DA59F583429E8043BAFC576BE10092C4EA4D8E19846CA62";
|
||||
String y = "F7E938B02EED7280277493B8556E5B01CB436E018A562DFDC53342BF41FDF728";
|
||||
final String x = "706AD9DAA3E5CEAC3DA59F583429E8043BAFC576BE10092C4EA4D8E19846CA62";
|
||||
final String y = "F7E938B02EED7280277493B8556E5B01CB436E018A562DFDC53342BF41FDF728";
|
||||
|
||||
final ECPublicKeyParameters keyParameters = BCUtil.toSm2Params(x, y);
|
||||
Assert.assertNotNull(keyParameters);
|
||||
@@ -22,7 +22,7 @@ public class BCUtilTest {
|
||||
|
||||
@Test
|
||||
public void createECPrivateKeyParametersTest() {
|
||||
String privateKeyHex = "5F6CA5BB044C40ED2355F0372BF72A5B3AE6943712F9FDB7C1FFBAECC06F3829";
|
||||
final String privateKeyHex = "5F6CA5BB044C40ED2355F0372BF72A5B3AE6943712F9FDB7C1FFBAECC06F3829";
|
||||
|
||||
final ECPrivateKeyParameters keyParameters = BCUtil.toSm2Params(privateKeyHex);
|
||||
Assert.assertNotNull(keyParameters);
|
||||
|
@@ -20,7 +20,7 @@ public class KeyUtilTest {
|
||||
@Ignore
|
||||
public void generateKeyPairTest() {
|
||||
GlobalBouncyCastleProvider.setUseBouncyCastle(false);
|
||||
KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
Assert.assertNotNull(pair);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class KeyUtilTest {
|
||||
Assert.assertNotNull(ecies.getPrivate());
|
||||
Assert.assertNotNull(ecies.getPublic());
|
||||
|
||||
byte[] privateKeyBytes = ecies.getPrivate().getEncoded();
|
||||
final byte[] privateKeyBytes = ecies.getPrivate().getEncoded();
|
||||
|
||||
final PrivateKey privateKey = KeyUtil.generatePrivateKey("EC", privateKeyBytes);
|
||||
Assert.assertEquals(ecies.getPrivate(), privateKey);
|
||||
@@ -53,7 +53,7 @@ public class KeyUtilTest {
|
||||
Assert.assertNotNull(dh.getPrivate());
|
||||
Assert.assertNotNull(dh.getPublic());
|
||||
|
||||
byte[] privateKeyBytes = dh.getPrivate().getEncoded();
|
||||
final byte[] privateKeyBytes = dh.getPrivate().getEncoded();
|
||||
|
||||
final PrivateKey privateKey = KeyUtil.generatePrivateKey("DH", privateKeyBytes);
|
||||
Assert.assertEquals(dh.getPrivate(), privateKey);
|
||||
|
@@ -19,45 +19,45 @@ public class PemUtilTest {
|
||||
|
||||
@Test
|
||||
public void readPrivateKeyTest() {
|
||||
PrivateKey privateKey = PemUtil.readPemPrivateKey(ResourceUtil.getStream("test_private_key.pem"));
|
||||
final PrivateKey privateKey = PemUtil.readPemPrivateKey(ResourceUtil.getStream("test_private_key.pem"));
|
||||
Assert.assertNotNull(privateKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readPublicKeyTest() {
|
||||
PublicKey publicKey = PemUtil.readPemPublicKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
final PublicKey publicKey = PemUtil.readPemPublicKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
Assert.assertNotNull(publicKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readPemKeyTest() {
|
||||
PublicKey publicKey = (PublicKey) PemUtil.readPemKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
final PublicKey publicKey = (PublicKey) PemUtil.readPemKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
Assert.assertNotNull(publicKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateKey() {
|
||||
PrivateKey privateKey = PemUtil.readPemPrivateKey(ResourceUtil.getStream("test_private_key.pem"));
|
||||
PublicKey publicKey = PemUtil.readPemPublicKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
final PrivateKey privateKey = PemUtil.readPemPrivateKey(ResourceUtil.getStream("test_private_key.pem"));
|
||||
final PublicKey publicKey = PemUtil.readPemPublicKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
|
||||
RSA rsa = new RSA(privateKey, publicKey);
|
||||
String str = "你好,Hutool";//测试字符串
|
||||
final RSA rsa = new RSA(privateKey, publicKey);
|
||||
final String str = "你好,Hutool";//测试字符串
|
||||
|
||||
String encryptStr = rsa.encryptBase64(str, KeyType.PublicKey);
|
||||
String decryptStr = rsa.decryptStr(encryptStr, KeyType.PrivateKey);
|
||||
final String encryptStr = rsa.encryptBase64(str, KeyType.PublicKey);
|
||||
final String decryptStr = rsa.decryptStr(encryptStr, KeyType.PrivateKey);
|
||||
Assert.assertEquals(str, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readECPrivateKeyTest() {
|
||||
PrivateKey privateKey = PemUtil.readSm2PemPrivateKey(ResourceUtil.getStream("test_ec_private_key.pem"));
|
||||
SM2 sm2 = new SM2(privateKey, null);
|
||||
final PrivateKey privateKey = PemUtil.readSm2PemPrivateKey(ResourceUtil.getStream("test_ec_private_key.pem"));
|
||||
final SM2 sm2 = new SM2(privateKey, null);
|
||||
sm2.usePlainEncoding();
|
||||
|
||||
//需要签名的明文,得到明文对应的字节数组
|
||||
byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
byte[] sign = sm2.sign(dataBytes, null);
|
||||
final byte[] sign = sm2.sign(dataBytes, null);
|
||||
// 64位签名
|
||||
Assert.assertEquals(64, sign.length);
|
||||
}
|
||||
@@ -66,15 +66,15 @@ public class PemUtilTest {
|
||||
@Ignore
|
||||
public void readECPrivateKeyTest2() {
|
||||
// https://gitee.com/dromara/hutool/issues/I37Z75
|
||||
byte[] d = PemUtil.readPem(FileUtil.getInputStream("d:/test/keys/priv.key"));
|
||||
byte[] publicKey = PemUtil.readPem(FileUtil.getInputStream("d:/test/keys/pub.key"));
|
||||
final byte[] d = PemUtil.readPem(FileUtil.getInputStream("d:/test/keys/priv.key"));
|
||||
final byte[] publicKey = PemUtil.readPem(FileUtil.getInputStream("d:/test/keys/pub.key"));
|
||||
|
||||
SM2 sm2 = new SM2(d, publicKey);
|
||||
final SM2 sm2 = new SM2(d, publicKey);
|
||||
sm2.usePlainEncoding();
|
||||
|
||||
String content = "我是Hanley.";
|
||||
byte[] sign = sm2.sign(StrUtil.utf8Bytes(content));
|
||||
boolean verify = sm2.verify(StrUtil.utf8Bytes(content), sign);
|
||||
final String content = "我是Hanley.";
|
||||
final byte[] sign = sm2.sign(StrUtil.utf8Bytes(content));
|
||||
final boolean verify = sm2.verify(StrUtil.utf8Bytes(content), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
}
|
||||
|
@@ -22,77 +22,77 @@ public class SmTest {
|
||||
|
||||
@Test
|
||||
public void sm3Test() {
|
||||
String digestHex = SmUtil.sm3("aaaaa");
|
||||
final String digestHex = SmUtil.sm3("aaaaa");
|
||||
Assert.assertEquals("136ce3c86e4ed909b76082055a61586af20b4dab674732ebd4b599eef080c9be", digestHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4Test() {
|
||||
String content = "test中文";
|
||||
SM4 sm4 = SmUtil.sm4();
|
||||
final String content = "test中文";
|
||||
final SM4 sm4 = SmUtil.sm4();
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String encryptHex = sm4.encryptHex(content);
|
||||
final String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4Test2() {
|
||||
String content = "test中文";
|
||||
SM4 sm4 = new SM4(Mode.CTR, Padding.PKCS5Padding);
|
||||
final String content = "test中文";
|
||||
final SM4 sm4 = new SM4(Mode.CTR, Padding.PKCS5Padding);
|
||||
sm4.setIv("aaaabbbb".getBytes());
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String encryptHex = sm4.encryptHex(content);
|
||||
final String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4ECBPKCS5PaddingTest2() {
|
||||
String content = "test中文";
|
||||
SM4 sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding);
|
||||
final String content = "test中文";
|
||||
final SM4 sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding);
|
||||
Assert.assertEquals("SM4/ECB/PKCS5Padding", sm4.getCipher().getAlgorithm());
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String encryptHex = sm4.encryptHex(content);
|
||||
final String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4TestWithCustomKeyTest() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
SecretKey key = KeyUtil.generateKey(SM4.ALGORITHM_NAME);
|
||||
final SecretKey key = KeyUtil.generateKey(SM4.ALGORITHM_NAME);
|
||||
|
||||
SM4 sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding, key);
|
||||
final SM4 sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding, key);
|
||||
Assert.assertEquals("SM4/ECB/PKCS5Padding", sm4.getCipher().getAlgorithm());
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String encryptHex = sm4.encryptHex(content);
|
||||
final String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4TestWithCustomKeyTest2() {
|
||||
String content = "test中文frfewrewrwerwer---------------------------------------------------";
|
||||
final String content = "test中文frfewrewrwerwer---------------------------------------------------";
|
||||
|
||||
byte[] key = KeyUtil.generateKey(SM4.ALGORITHM_NAME, 128).getEncoded();
|
||||
final byte[] key = KeyUtil.generateKey(SM4.ALGORITHM_NAME, 128).getEncoded();
|
||||
|
||||
SM4 sm4 = SmUtil.sm4(key);
|
||||
final SM4 sm4 = SmUtil.sm4(key);
|
||||
Assert.assertEquals("SM4", sm4.getCipher().getAlgorithm());
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String encryptHex = sm4.encryptHex(content);
|
||||
final String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacSm3Test() {
|
||||
String content = "test中文";
|
||||
HMac hMac = SmUtil.hmacSm3("password".getBytes());
|
||||
String digest = hMac.digestHex(content);
|
||||
final String content = "test中文";
|
||||
final HMac hMac = SmUtil.hmacSm3("password".getBytes());
|
||||
final String digest = hMac.digestHex(content);
|
||||
Assert.assertEquals("493e3f9a1896b43075fbe54658076727960d69632ac6b6ed932195857a6840c6", digest);
|
||||
}
|
||||
|
||||
|
@@ -31,17 +31,17 @@ public class ECIESTest {
|
||||
* @param cryptoForEncrypt 加密的Crypto
|
||||
* @param cryptoForDecrypt 解密的Crypto
|
||||
*/
|
||||
private void doTest(AsymmetricCrypto cryptoForEncrypt, AsymmetricCrypto cryptoForDecrypt){
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
private void doTest(final AsymmetricCrypto cryptoForEncrypt, final AsymmetricCrypto cryptoForDecrypt){
|
||||
final String textBase = "我是一段特别长的测试";
|
||||
final StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = cryptoForEncrypt.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
final String encryptStr = cryptoForEncrypt.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
|
||||
String decryptStr = StrUtil.utf8Str(cryptoForDecrypt.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
final String decryptStr = StrUtil.utf8Str(cryptoForDecrypt.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
}
|
||||
}
|
||||
|
@@ -28,27 +28,27 @@ public class RSATest {
|
||||
|
||||
@Test
|
||||
public void generateKeyPairTest() {
|
||||
KeyPair pair = KeyUtil.generateKeyPair("RSA");
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("RSA");
|
||||
Assert.assertNotNull(pair.getPrivate());
|
||||
Assert.assertNotNull(pair.getPublic());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaCustomKeyTest() {
|
||||
KeyPair pair = KeyUtil.generateKeyPair("RSA");
|
||||
byte[] privateKey = pair.getPrivate().getEncoded();
|
||||
byte[] publicKey = pair.getPublic().getEncoded();
|
||||
final KeyPair pair = KeyUtil.generateKeyPair("RSA");
|
||||
final byte[] privateKey = pair.getPrivate().getEncoded();
|
||||
final byte[] publicKey = pair.getPublic().getEncoded();
|
||||
|
||||
RSA rsa = SecureUtil.rsa(privateKey, publicKey);
|
||||
final RSA rsa = SecureUtil.rsa(privateKey, publicKey);
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
final byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
final byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
final byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
final byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@@ -63,14 +63,14 @@ public class RSATest {
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
final byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
final byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
final byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
final byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@@ -85,14 +85,14 @@ public class RSATest {
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
final byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
final byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
final byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
final byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@@ -107,14 +107,14 @@ public class RSATest {
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
final byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
final byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
final byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
final byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@@ -130,37 +130,37 @@ public class RSATest {
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
final byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
final byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
final byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PrivateKey);
|
||||
final byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaBcdTest() {
|
||||
String text = "我是一段测试aaaa";
|
||||
final String text = "我是一段测试aaaa";
|
||||
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = rsa.encryptBcd(text, KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(rsa.decryptFromBcd(encryptStr, KeyType.PrivateKey));
|
||||
final String encryptStr = rsa.encryptBcd(text, KeyType.PublicKey);
|
||||
final String decryptStr = StrUtil.utf8Str(rsa.decryptFromBcd(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text, decryptStr);
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
String encrypt2 = rsa.encryptBcd(text, KeyType.PrivateKey);
|
||||
String decrypt2 = StrUtil.utf8Str(rsa.decryptFromBcd(encrypt2, KeyType.PublicKey));
|
||||
final String encrypt2 = rsa.encryptBcd(text, KeyType.PrivateKey);
|
||||
final String decrypt2 = StrUtil.utf8Str(rsa.decryptFromBcd(encrypt2, KeyType.PublicKey));
|
||||
Assert.assertEquals(text, decrypt2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaBase64Test() {
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
final String textBase = "我是一段特别长的测试";
|
||||
final StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
@@ -168,19 +168,19 @@ public class RSATest {
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = rsa.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(rsa.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
final String encryptStr = rsa.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
final String decryptStr = StrUtil.utf8Str(rsa.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
String encrypt2 = rsa.encryptBase64(text.toString(), KeyType.PrivateKey);
|
||||
String decrypt2 = StrUtil.utf8Str(rsa.decrypt(encrypt2, KeyType.PublicKey));
|
||||
final String encrypt2 = rsa.encryptBase64(text.toString(), KeyType.PrivateKey);
|
||||
final String decrypt2 = StrUtil.utf8Str(rsa.decrypt(encrypt2, KeyType.PublicKey));
|
||||
Assert.assertEquals(text.toString(), decrypt2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaDecodeTest() {
|
||||
String PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIL7pbQ+5KKGYRhw7jE31hmA" //
|
||||
final String PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIL7pbQ+5KKGYRhw7jE31hmA" //
|
||||
+ "f8Q60ybd+xZuRmuO5kOFBRqXGxKTQ9TfQI+aMW+0lw/kibKzaD/EKV91107xE384qOy6IcuBfaR5lv39OcoqNZ"//
|
||||
+ "5l+Dah5ABGnVkBP9fKOFhPgghBknTRo0/rZFGI6Q1UHXb+4atP++LNFlDymJcPAgMBAAECgYBammGb1alndta" //
|
||||
+ "xBmTtLLdveoBmp14p04D8mhkiC33iFKBcLUvvxGg2Vpuc+cbagyu/NZG+R/WDrlgEDUp6861M5BeFN0L9O4hz"//
|
||||
@@ -191,52 +191,52 @@ public class RSATest {
|
||||
+ "wfMhrJksfeKbIaMjNLS9b8JynIaXg9iCiyOHmgkMl5gAbPoH/ULXqSKwzBw5mJ2GW1gBlyaSfV3AkA/RJC+adIjsRGg"//
|
||||
+ "JOkiRjSmPpGv3FOhl9fsBPjupZBEIuoMWOC8GXK/73DHxwmfNmN7C9+sIi4RBcjEeQ5F5FHZ";
|
||||
|
||||
RSA rsa = new RSA(PRIVATE_KEY, null);
|
||||
final RSA rsa = new RSA(PRIVATE_KEY, null);
|
||||
|
||||
String a = "2707F9FD4288CEF302C972058712F24A5F3EC62C5A14AD2FC59DAB93503AA0FA17113A020EE4EA35EB53F" //
|
||||
final String a = "2707F9FD4288CEF302C972058712F24A5F3EC62C5A14AD2FC59DAB93503AA0FA17113A020EE4EA35EB53F" //
|
||||
+ "75F36564BA1DABAA20F3B90FD39315C30E68FE8A1803B36C29029B23EB612C06ACF3A34BE815074F5EB5AA3A"//
|
||||
+ "C0C8832EC42DA725B4E1C38EF4EA1B85904F8B10B2D62EA782B813229F9090E6F7394E42E6F44494BB8";
|
||||
|
||||
byte[] aByte = HexUtil.decodeHex(a);
|
||||
byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
|
||||
final byte[] aByte = HexUtil.decodeHex(a);
|
||||
final byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
|
||||
|
||||
Assert.assertEquals("虎头闯杭州,多抬头看天,切勿只管种地", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaTest2() throws Exception {
|
||||
String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6s" +
|
||||
final String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6s" +
|
||||
"XqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9S" +
|
||||
"dB1Ua44oncaTWz7OBGLbCiK45wIDAQAB";
|
||||
|
||||
byte[] keyBytes = Base64.decode(publicKeyStr);
|
||||
PublicKey publicKey = KeyUtil.generateRSAPublicKey(keyBytes);
|
||||
final byte[] keyBytes = Base64.decode(publicKeyStr);
|
||||
final PublicKey publicKey = KeyUtil.generateRSAPublicKey(keyBytes);
|
||||
|
||||
byte[] data = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).getBytes();
|
||||
final byte[] data = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).getBytes();
|
||||
//长度不满足128补0
|
||||
byte[] finalData = ArrayUtil.resize(data, 128);
|
||||
final byte[] finalData = ArrayUtil.resize(data, 128);
|
||||
|
||||
//jdk原生加密
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
||||
final Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
String result1 = HexUtil.encodeHexStr(cipher.doFinal(finalData));
|
||||
final String result1 = HexUtil.encodeHexStr(cipher.doFinal(finalData));
|
||||
|
||||
//hutool加密
|
||||
RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);
|
||||
final RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);
|
||||
rsa.setEncryptBlockSize(128);
|
||||
String result2 = rsa.encryptHex(finalData, KeyType.PublicKey);
|
||||
final String result2 = rsa.encryptHex(finalData, KeyType.PublicKey);
|
||||
|
||||
Assert.assertEquals(result1, result2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exponentTest(){
|
||||
String modulus = "BD99BAAB9E56B7FD85FB8BCF53CAD2913C1ACEF9063E7C913CD6FC4FEE040DA44D8" +
|
||||
final String modulus = "BD99BAAB9E56B7FD85FB8BCF53CAD2913C1ACEF9063E7C913CD6FC4FEE040DA44D8" +
|
||||
"ADAA35A9DCABD6E936C402D47278049638407135BAB22BB091396CB6873195C8AC8B0B7AB123" +
|
||||
"C3BF7A6341A4419BDBC0EFB85DBCD9A3AD12C99E2265BDCC1197913749E2AFA568EB7623DA3A" +
|
||||
"361335AA1F9FFA6E1801DDC8228AA86306B87";
|
||||
String publicExponent = "65537";
|
||||
RSA rsa = new RSA(new BigInteger(modulus, 16), null, new BigInteger(publicExponent));
|
||||
final String publicExponent = "65537";
|
||||
final RSA rsa = new RSA(new BigInteger(modulus, 16), null, new BigInteger(publicExponent));
|
||||
|
||||
final String encryptBase64 = rsa.encryptBase64("测试内容", KeyType.PublicKey);
|
||||
Assert.assertNotNull(encryptBase64);
|
||||
|
@@ -30,7 +30,7 @@ public class SM2Test {
|
||||
|
||||
@Test
|
||||
public void generateKeyPairTest() {
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
Assert.assertNotNull(pair.getPrivate());
|
||||
Assert.assertNotNull(pair.getPublic());
|
||||
}
|
||||
@@ -38,24 +38,24 @@ public class SM2Test {
|
||||
@Test
|
||||
public void KeyPairOIDTest() {
|
||||
// OBJECT IDENTIFIER 1.2.156.10197.1.301
|
||||
String OID = "06082A811CCF5501822D";
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final String OID = "06082A811CCF5501822D";
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
Assert.assertTrue(HexUtil.encodeHexStr(pair.getPrivate().getEncoded()).toUpperCase().contains(OID));
|
||||
Assert.assertTrue(HexUtil.encodeHexStr(pair.getPublic().getEncoded()).toUpperCase().contains(OID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2CustomKeyTest() {
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
byte[] privateKey = pair.getPrivate().getEncoded();
|
||||
byte[] publicKey = pair.getPublic().getEncoded();
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final byte[] privateKey = pair.getPrivate().getEncoded();
|
||||
final byte[] publicKey = pair.getPublic().getEncoded();
|
||||
|
||||
SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
|
||||
final SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
|
||||
sm2.setMode(SM2Engine.Mode.C1C2C3);
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
final byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
final byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@@ -70,27 +70,27 @@ public class SM2Test {
|
||||
Assert.assertNotNull(sm2.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
final byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.UTF_8), KeyType.PublicKey);
|
||||
final byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2BcdTest() {
|
||||
String text = "我是一段测试aaaa";
|
||||
final String text = "我是一段测试aaaa";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
|
||||
final String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
|
||||
final String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2Base64Test() {
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
final String textBase = "我是一段特别长的测试";
|
||||
final StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
@@ -98,31 +98,31 @@ public class SM2Test {
|
||||
SM2 sm2 = new SM2();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = sm2.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
final String encryptStr = sm2.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
final String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
|
||||
// 测试自定义密钥后是否生效
|
||||
PrivateKey privateKey = sm2.getPrivateKey();
|
||||
PublicKey publicKey = sm2.getPublicKey();
|
||||
final PrivateKey privateKey = sm2.getPrivateKey();
|
||||
final PublicKey publicKey = sm2.getPublicKey();
|
||||
|
||||
sm2 = SmUtil.sm2();
|
||||
sm2.setPrivateKey(privateKey);
|
||||
sm2.setPublicKey(publicKey);
|
||||
String decryptStr2 = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
final String decryptStr2 = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignTest(){
|
||||
//需要签名的明文,得到明文对应的字节数组
|
||||
byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
//指定的私钥
|
||||
String privateKeyHex = "1ebf8b341c695ee456fd1a41b82645724bc25d79935437d30e7e4b0a554baa5e";
|
||||
final String privateKeyHex = "1ebf8b341c695ee456fd1a41b82645724bc25d79935437d30e7e4b0a554baa5e";
|
||||
final SM2 sm2 = new SM2(privateKeyHex, null, null);
|
||||
sm2.usePlainEncoding();
|
||||
byte[] sign = sm2.sign(dataBytes, null);
|
||||
final byte[] sign = sm2.sign(dataBytes, null);
|
||||
// 64位签名
|
||||
Assert.assertEquals(64, sign.length);
|
||||
}
|
||||
@@ -130,91 +130,91 @@ public class SM2Test {
|
||||
@Test
|
||||
public void sm2VerifyTest(){
|
||||
//指定的公钥
|
||||
String publicKeyHex = "04db9629dd33ba568e9507add5df6587a0998361a03d3321948b448c653c2c1b7056434884ab6f3d1c529501f166a336e86f045cea10dffe58aa82ea13d7253763";
|
||||
final String publicKeyHex = "04db9629dd33ba568e9507add5df6587a0998361a03d3321948b448c653c2c1b7056434884ab6f3d1c529501f166a336e86f045cea10dffe58aa82ea13d7253763";
|
||||
//需要加密的明文,得到明文对应的字节数组
|
||||
byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
|
||||
//签名值
|
||||
String signHex = "2881346e038d2ed706ccdd025f2b1dafa7377d5cf090134b98756fafe084dddbcdba0ab00b5348ed48025195af3f1dda29e819bb66aa9d4d088050ff148482a1";
|
||||
final String signHex = "2881346e038d2ed706ccdd025f2b1dafa7377d5cf090134b98756fafe084dddbcdba0ab00b5348ed48025195af3f1dda29e819bb66aa9d4d088050ff148482a1";
|
||||
|
||||
final SM2 sm2 = new SM2(null, publicKeyHex);
|
||||
sm2.usePlainEncoding();
|
||||
|
||||
boolean verify = sm2.verify(dataBytes, HexUtil.decodeHex(signHex));
|
||||
final boolean verify = sm2.verify(dataBytes, HexUtil.decodeHex(signHex));
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyTest() {
|
||||
String content = "我是Hanley.";
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
byte[] sign = sm2.sign(StrUtil.utf8Bytes(content));
|
||||
boolean verify = sm2.verify(StrUtil.utf8Bytes(content), sign);
|
||||
final byte[] sign = sm2.sign(StrUtil.utf8Bytes(content));
|
||||
final boolean verify = sm2.verify(StrUtil.utf8Bytes(content), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyHexTest() {
|
||||
String content = "我是Hanley.";
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
String sign = sm2.signHex(HexUtil.encodeHexStr(content));
|
||||
boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
|
||||
final String sign = sm2.signHex(HexUtil.encodeHexStr(content));
|
||||
final boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyUseKeyTest() {
|
||||
String content = "我是Hanley.";
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
|
||||
final SM2 sm2 = new SM2(pair.getPrivate(), pair.getPublic());
|
||||
|
||||
byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
|
||||
boolean verify = sm2.verify(content.getBytes(StandardCharsets.UTF_8), sign);
|
||||
final byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
|
||||
final boolean verify = sm2.verify(content.getBytes(StandardCharsets.UTF_8), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyUseKeyTest2() {
|
||||
String content = "我是Hanley.";
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
|
||||
final SM2 sm2 = new SM2(//
|
||||
HexUtil.encodeHexStr(pair.getPrivate().getEncoded()), //
|
||||
HexUtil.encodeHexStr(pair.getPublic().getEncoded())//
|
||||
);
|
||||
|
||||
byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
|
||||
boolean verify = sm2.verify(content.getBytes(StandardCharsets.UTF_8), sign);
|
||||
final byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
|
||||
final boolean verify = sm2.verify(content.getBytes(StandardCharsets.UTF_8), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2PublicKeyEncodeDecodeTest() {
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
PublicKey publicKey = pair.getPublic();
|
||||
byte[] data = KeyUtil.encodeECPublicKey(publicKey);
|
||||
String encodeHex = HexUtil.encodeHexStr(data);
|
||||
String encodeB64 = Base64.encode(data);
|
||||
PublicKey Hexdecode = KeyUtil.decodeECPoint(encodeHex, KeyUtil.SM2_DEFAULT_CURVE);
|
||||
PublicKey B64decode = KeyUtil.decodeECPoint(encodeB64, KeyUtil.SM2_DEFAULT_CURVE);
|
||||
final KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
final PublicKey publicKey = pair.getPublic();
|
||||
final byte[] data = KeyUtil.encodeECPublicKey(publicKey);
|
||||
final String encodeHex = HexUtil.encodeHexStr(data);
|
||||
final String encodeB64 = Base64.encode(data);
|
||||
final PublicKey Hexdecode = KeyUtil.decodeECPoint(encodeHex, KeyUtil.SM2_DEFAULT_CURVE);
|
||||
final PublicKey B64decode = KeyUtil.decodeECPoint(encodeB64, KeyUtil.SM2_DEFAULT_CURVE);
|
||||
Assert.assertEquals(HexUtil.encodeHexStr(publicKey.getEncoded()), HexUtil.encodeHexStr(Hexdecode.getEncoded()));
|
||||
Assert.assertEquals(HexUtil.encodeHexStr(publicKey.getEncoded()), HexUtil.encodeHexStr(B64decode.getEncoded()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2WithPointTest() {
|
||||
String d = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
|
||||
String x = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
|
||||
String y = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
|
||||
final String d = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
|
||||
final String x = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
|
||||
final String y = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
|
||||
|
||||
String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
String id = "31323334353637383132333435363738";
|
||||
final String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
final String id = "31323334353637383132333435363738";
|
||||
|
||||
final SM2 sm2 = new SM2(d, x, y);
|
||||
final String sign = sm2.signHex(data, id);
|
||||
@@ -225,31 +225,31 @@ public class SM2Test {
|
||||
public void sm2PlainWithPointTest() {
|
||||
// 测试地址:https://i.goto327.top/CryptTools/SM2.aspx?tdsourcetag=s_pctim_aiomsg
|
||||
|
||||
String d = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
|
||||
String x = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
|
||||
String y = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
|
||||
final String d = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
|
||||
final String x = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
|
||||
final String y = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
|
||||
|
||||
String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
String id = "31323334353637383132333435363738";
|
||||
final String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
final String id = "31323334353637383132333435363738";
|
||||
|
||||
final SM2 sm2 = new SM2(d, x, y);
|
||||
// 生成的签名是64位
|
||||
sm2.usePlainEncoding();
|
||||
|
||||
|
||||
String sign = "DCA0E80A7F46C93714B51C3EFC55A922BCEF7ECF0FE9E62B53BA6A7438B543A76C145A452CA9036F3CB70D7E6C67D4D9D7FE114E5367A2F6F5A4D39F2B10F3D6";
|
||||
final String sign = "DCA0E80A7F46C93714B51C3EFC55A922BCEF7ECF0FE9E62B53BA6A7438B543A76C145A452CA9036F3CB70D7E6C67D4D9D7FE114E5367A2F6F5A4D39F2B10F3D6";
|
||||
Assert.assertTrue(sm2.verifyHex(data, sign));
|
||||
|
||||
String sign2 = sm2.signHex(data, id);
|
||||
final String sign2 = sm2.signHex(data, id);
|
||||
Assert.assertTrue(sm2.verifyHex(data, sign2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2PlainWithPointTest2() {
|
||||
String d = "4BD9A450D7E68A5D7E08EB7A0BFA468FD3EB32B71126246E66249A73A9E4D44A";
|
||||
String q = "04970AB36C3B870FBC04041087DB1BC36FB4C6E125B5EA406DB0EC3E2F80F0A55D8AFF28357A0BB215ADC2928BE76F1AFF869BF4C0A3852A78F3B827812C650AD3";
|
||||
final String d = "4BD9A450D7E68A5D7E08EB7A0BFA468FD3EB32B71126246E66249A73A9E4D44A";
|
||||
final String q = "04970AB36C3B870FBC04041087DB1BC36FB4C6E125B5EA406DB0EC3E2F80F0A55D8AFF28357A0BB215ADC2928BE76F1AFF869BF4C0A3852A78F3B827812C650AD3";
|
||||
|
||||
String data = "123456";
|
||||
final String data = "123456";
|
||||
|
||||
final SM2 sm2 = new SM2(d, q);
|
||||
sm2.setMode(SM2Engine.Mode.C1C2C3);
|
||||
@@ -261,54 +261,54 @@ public class SM2Test {
|
||||
|
||||
@Test
|
||||
public void encryptAndSignTest(){
|
||||
SM2 sm2 = SmUtil.sm2();
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
String src = "Sm2Test";
|
||||
byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
byte[] sign = sm2.sign(src.getBytes(StandardCharsets.UTF_8));
|
||||
final String src = "Sm2Test";
|
||||
final byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
final byte[] sign = sm2.sign(src.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Assert.assertTrue(sm2.verify( src.getBytes(StandardCharsets.UTF_8), sign));
|
||||
|
||||
byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
final byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
Assert.assertArrayEquals(dec, src.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPublicKeyByPrivateKeyTest(){
|
||||
// issue#I38SDP,openSSL生成的PKCS#1格式私钥
|
||||
String priKey = "MHcCAQEEIE29XqAFV/rkJbnJzCoQRJLTeAHG2TR0h9ZCWag0+ZMEoAoGCCqBHM9VAYItoUQDQgAESkOzNigIsH5ehFvr9y" +
|
||||
final String priKey = "MHcCAQEEIE29XqAFV/rkJbnJzCoQRJLTeAHG2TR0h9ZCWag0+ZMEoAoGCCqBHM9VAYItoUQDQgAESkOzNigIsH5ehFvr9y" +
|
||||
"QNQ66genyOrm+Q4umCA4aWXPeRzmcTAWSlTineiReTFN2lqor2xaulT8u3a4w3AM/F6A==";
|
||||
|
||||
PrivateKey privateKey = KeyUtil.generatePrivateKey("sm2", new OpenSSHPrivateKeySpec(SecureUtil.decode(priKey)));
|
||||
final PrivateKey privateKey = KeyUtil.generatePrivateKey("sm2", new OpenSSHPrivateKeySpec(SecureUtil.decode(priKey)));
|
||||
final ECPrivateKeyParameters privateKeyParameters = ECKeyUtil.toPrivateParams(privateKey);
|
||||
|
||||
final SM2 sm2 = new SM2(privateKeyParameters, ECKeyUtil.getPublicParams(privateKeyParameters));
|
||||
|
||||
String src = "Sm2Test";
|
||||
byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
byte[] sign = sm2.sign(src.getBytes(StandardCharsets.UTF_8));
|
||||
final String src = "Sm2Test";
|
||||
final byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
final byte[] sign = sm2.sign(src.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Assert.assertTrue(sm2.verify( src.getBytes(StandardCharsets.UTF_8), sign));
|
||||
|
||||
byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
final byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
Assert.assertArrayEquals(dec, src.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readPublicKeyTest(){
|
||||
String priKey = "MHcCAQEEIE29XqAFV/rkJbnJzCoQRJLTeAHG2TR0h9ZCWag0+ZMEoAoGCCqBHM9VAYItoUQDQgAESkOzNigIsH5ehFvr9y" +
|
||||
final String priKey = "MHcCAQEEIE29XqAFV/rkJbnJzCoQRJLTeAHG2TR0h9ZCWag0+ZMEoAoGCCqBHM9VAYItoUQDQgAESkOzNigIsH5ehFvr9y" +
|
||||
"QNQ66genyOrm+Q4umCA4aWXPeRzmcTAWSlTineiReTFN2lqor2xaulT8u3a4w3AM/F6A==";
|
||||
String pubKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAESkOzNigIsH5ehFvr9yQNQ66genyOrm+Q4umCA4aWXPeRzmcTAWSlTineiReTFN2lqor2xaulT8u3a4w3AM/F6A==";
|
||||
final String pubKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAESkOzNigIsH5ehFvr9yQNQ66genyOrm+Q4umCA4aWXPeRzmcTAWSlTineiReTFN2lqor2xaulT8u3a4w3AM/F6A==";
|
||||
|
||||
SM2 sm2 = SmUtil.sm2(priKey, pubKey);
|
||||
final SM2 sm2 = SmUtil.sm2(priKey, pubKey);
|
||||
|
||||
String src = "Sm2Test中文";
|
||||
byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
byte[] sign = sm2.sign(src.getBytes(StandardCharsets.UTF_8));
|
||||
final String src = "Sm2Test中文";
|
||||
final byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
final byte[] sign = sm2.sign(src.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Assert.assertTrue(sm2.verify( src.getBytes(StandardCharsets.UTF_8), sign));
|
||||
|
||||
byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
final byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
Assert.assertArrayEquals(dec, src.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
|
@@ -21,18 +21,18 @@ public class SignTest {
|
||||
|
||||
@Test
|
||||
public void signAndVerifyUseKeyTest() {
|
||||
String content = "我是Hanley.";
|
||||
final String content = "我是Hanley.";
|
||||
|
||||
String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJ4fG8vJ0tzu7tjXMSJhyNjlE5B7GkTKMKEQlR6LY3IhIhMFVjuA6W+DqH1VMxl9h3GIM4yCKG2VRZEYEPazgVxa5/ifO8W0pfmrzWCPrddUq4t0Slz5u2lLKymLpPjCzboHoDb8VlF+1HOxjKQckAXq9q7U7dV5VxOzJDuZXlz3AgMBAAECgYABo2LfVqT3owYYewpIR+kTzjPIsG3SPqIIWSqiWWFbYlp/BfQhw7EndZ6+Ra602ecYVwfpscOHdx90ZGJwm+WAMkKT4HiWYwyb0ZqQzRBGYDHFjPpfCBxrzSIJ3QL+B8c8YHq4HaLKRKmq7VUF1gtyWaek87rETWAmQoGjt8DyAQJBAOG4OxsT901zjfxrgKwCv6fV8wGXrNfDSViP1t9r3u6tRPsE6Gli0dfMyzxwENDTI75sOEAfyu6xBlemQGmNsfcCQQCzVWQkl9YUoVDWEitvI5MpkvVKYsFLRXKvLfyxLcY3LxpLKBcEeJ/n5wLxjH0GorhJMmM2Rw3hkjUTJCoqqe0BAkATt8FKC0N2O5ryqv1xiUfuxGzW/cX2jzOwDdiqacTuuqok93fKBPzpyhUS8YM2iss7jj6Xs29JzKMOMxK7ZcpfAkAf21lwzrAu9gEgJhYlJhKsXfjJAAYKUwnuaKLs7o65mtp242ZDWxI85eK1+hjzptBJ4HOTXsfufESFY/VBovIBAkAltO886qQRoNSc0OsVlCi4X1DGo6x2RqQ9EsWPrxWEZGYuyEdODrc54b8L+zaUJLfMJdsCIHEUbM7WXxvFVXNv";
|
||||
final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJ4fG8vJ0tzu7tjXMSJhyNjlE5B7GkTKMKEQlR6LY3IhIhMFVjuA6W+DqH1VMxl9h3GIM4yCKG2VRZEYEPazgVxa5/ifO8W0pfmrzWCPrddUq4t0Slz5u2lLKymLpPjCzboHoDb8VlF+1HOxjKQckAXq9q7U7dV5VxOzJDuZXlz3AgMBAAECgYABo2LfVqT3owYYewpIR+kTzjPIsG3SPqIIWSqiWWFbYlp/BfQhw7EndZ6+Ra602ecYVwfpscOHdx90ZGJwm+WAMkKT4HiWYwyb0ZqQzRBGYDHFjPpfCBxrzSIJ3QL+B8c8YHq4HaLKRKmq7VUF1gtyWaek87rETWAmQoGjt8DyAQJBAOG4OxsT901zjfxrgKwCv6fV8wGXrNfDSViP1t9r3u6tRPsE6Gli0dfMyzxwENDTI75sOEAfyu6xBlemQGmNsfcCQQCzVWQkl9YUoVDWEitvI5MpkvVKYsFLRXKvLfyxLcY3LxpLKBcEeJ/n5wLxjH0GorhJMmM2Rw3hkjUTJCoqqe0BAkATt8FKC0N2O5ryqv1xiUfuxGzW/cX2jzOwDdiqacTuuqok93fKBPzpyhUS8YM2iss7jj6Xs29JzKMOMxK7ZcpfAkAf21lwzrAu9gEgJhYlJhKsXfjJAAYKUwnuaKLs7o65mtp242ZDWxI85eK1+hjzptBJ4HOTXsfufESFY/VBovIBAkAltO886qQRoNSc0OsVlCi4X1DGo6x2RqQ9EsWPrxWEZGYuyEdODrc54b8L+zaUJLfMJdsCIHEUbM7WXxvFVXNv";
|
||||
Sign sign = SecureUtil.sign(SignAlgorithm.SHA1withRSA, privateKey, null);
|
||||
Assert.assertNull(sign.getPublicKeyBase64());
|
||||
// 签名
|
||||
byte[] signed = sign.sign(content.getBytes());
|
||||
final byte[] signed = sign.sign(content.getBytes());
|
||||
|
||||
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeHxvLydLc7u7Y1zEiYcjY5ROQexpEyjChEJUei2NyISITBVY7gOlvg6h9VTMZfYdxiDOMgihtlUWRGBD2s4FcWuf4nzvFtKX5q81gj63XVKuLdEpc+btpSyspi6T4ws26B6A2/FZRftRzsYykHJAF6vau1O3VeVcTsyQ7mV5c9wIDAQAB";
|
||||
final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeHxvLydLc7u7Y1zEiYcjY5ROQexpEyjChEJUei2NyISITBVY7gOlvg6h9VTMZfYdxiDOMgihtlUWRGBD2s4FcWuf4nzvFtKX5q81gj63XVKuLdEpc+btpSyspi6T4ws26B6A2/FZRftRzsYykHJAF6vau1O3VeVcTsyQ7mV5c9wIDAQAB";
|
||||
sign = SecureUtil.sign(SignAlgorithm.SHA1withRSA, null, publicKey);
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(content.getBytes(), signed);
|
||||
final boolean verify = sign.verify(content.getBytes(), signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@@ -63,15 +63,15 @@ public class SignTest {
|
||||
*
|
||||
* @param signAlgorithm 算法
|
||||
*/
|
||||
private void signAndVerify(SignAlgorithm signAlgorithm) {
|
||||
byte[] data = StrUtil.utf8Bytes("我是一段测试ab");
|
||||
Sign sign = SecureUtil.sign(signAlgorithm);
|
||||
private void signAndVerify(final SignAlgorithm signAlgorithm) {
|
||||
final byte[] data = StrUtil.utf8Bytes("我是一段测试ab");
|
||||
final Sign sign = SecureUtil.sign(signAlgorithm);
|
||||
|
||||
// 签名
|
||||
byte[] signed = sign.sign(data);
|
||||
final byte[] signed = sign.sign(data);
|
||||
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(data, signed);
|
||||
final boolean verify = sign.verify(data, signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@@ -80,29 +80,29 @@ public class SignTest {
|
||||
*/
|
||||
@Test
|
||||
public void signAndVerifyTest2() {
|
||||
String str = "wx2421b1c4370ec43b 支付测试 JSAPI支付测试 10000100 1add1a30ac87aa2db72f57a2375d8fec http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php oUpF8uMuAJO_M2pxb1Q9zNjWeS6o 1415659990 14.23.150.211 1 JSAPI 0CB01533B8C1EF103065174F50BCA001";
|
||||
byte[] data = StrUtil.utf8Bytes(str);
|
||||
Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
|
||||
final String str = "wx2421b1c4370ec43b 支付测试 JSAPI支付测试 10000100 1add1a30ac87aa2db72f57a2375d8fec http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php oUpF8uMuAJO_M2pxb1Q9zNjWeS6o 1415659990 14.23.150.211 1 JSAPI 0CB01533B8C1EF103065174F50BCA001";
|
||||
final byte[] data = StrUtil.utf8Bytes(str);
|
||||
final Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
|
||||
|
||||
// 签名
|
||||
byte[] signed = sign.sign(data);
|
||||
final byte[] signed = sign.sign(data);
|
||||
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(data, signed);
|
||||
final boolean verify = sign.verify(data, signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signParamsTest(){
|
||||
Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||
final Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||
.put("key1", "value1")
|
||||
.put("key2", "value2").build();
|
||||
|
||||
String sign1 = SecureUtil.signParamsSha1(build);
|
||||
final String sign1 = SecureUtil.signParamsSha1(build);
|
||||
Assert.assertEquals("9ed30bfe2efbc7038a824b6c55c24a11bfc0dce5", sign1);
|
||||
String sign2 = SecureUtil.signParamsSha1(build, "12345678");
|
||||
final String sign2 = SecureUtil.signParamsSha1(build, "12345678");
|
||||
Assert.assertEquals("944b68d94c952ec178c4caf16b9416b6661f7720", sign2);
|
||||
String sign3 = SecureUtil.signParamsSha1(build, "12345678", "abc");
|
||||
final String sign3 = SecureUtil.signParamsSha1(build, "12345678", "abc");
|
||||
Assert.assertEquals("edee1b477af1b96ebd20fdf08d818f352928d25d", sign3);
|
||||
}
|
||||
|
||||
@@ -111,15 +111,15 @@ public class SignTest {
|
||||
*/
|
||||
@Test
|
||||
public void signAndVerifyPSSTest() {
|
||||
String str = "wx2421b1c4370ec43b 支付测试 JSAPI支付测试 10000100 1add1a30ac87aa2db72f57a2375d8fec http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php oUpF8uMuAJO_M2pxb1Q9zNjWeS6o 1415659990 14.23.150.211 1 JSAPI 0CB01533B8C1EF103065174F50BCA001";
|
||||
byte[] data = StrUtil.utf8Bytes(str);
|
||||
Sign sign = SecureUtil.sign(SignAlgorithm.SHA256withRSA_PSS);
|
||||
final String str = "wx2421b1c4370ec43b 支付测试 JSAPI支付测试 10000100 1add1a30ac87aa2db72f57a2375d8fec http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php oUpF8uMuAJO_M2pxb1Q9zNjWeS6o 1415659990 14.23.150.211 1 JSAPI 0CB01533B8C1EF103065174F50BCA001";
|
||||
final byte[] data = StrUtil.utf8Bytes(str);
|
||||
final Sign sign = SecureUtil.sign(SignAlgorithm.SHA256withRSA_PSS);
|
||||
|
||||
// 签名
|
||||
byte[] signed = sign.sign(data);
|
||||
final byte[] signed = sign.sign(data);
|
||||
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(data, signed);
|
||||
final boolean verify = sign.verify(data, signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
}
|
||||
|
@@ -16,12 +16,12 @@ public class CBCBlockCipherMacEngineTest {
|
||||
// https://github.com/dromara/hutool/issues/2206
|
||||
final byte[] key = new byte[16];
|
||||
final CipherParameters parameter = new KeyParameter(KeyUtil.generateKey("SM4", key).getEncoded());
|
||||
Mac mac = new Mac(new SM4MacEngine(parameter));
|
||||
final Mac mac = new Mac(new SM4MacEngine(parameter));
|
||||
|
||||
// 原文
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("3212e848db7f816a4bd591ad9948debf", macHex1);
|
||||
}
|
||||
|
||||
@@ -32,12 +32,12 @@ public class CBCBlockCipherMacEngineTest {
|
||||
final byte[] iv = new byte[16];
|
||||
CipherParameters parameter = new KeyParameter(KeyUtil.generateKey("SM4", key).getEncoded());
|
||||
parameter = new ParametersWithIV(parameter, iv);
|
||||
Mac mac = new Mac(new SM4MacEngine(parameter));
|
||||
final Mac mac = new Mac(new SM4MacEngine(parameter));
|
||||
|
||||
// 原文
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("3212e848db7f816a4bd591ad9948debf", macHex1);
|
||||
}
|
||||
}
|
||||
|
@@ -17,60 +17,60 @@ public class DigestTest {
|
||||
|
||||
@Test
|
||||
public void digesterTest(){
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
String digestHex = md5.digestHex(testStr);
|
||||
final Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
final String digestHex = md5.digestHex(testStr);
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", digestHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void md5Test(){
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
String md5Hex1 = DigestUtil.md5Hex(testStr);
|
||||
final String md5Hex1 = DigestUtil.md5Hex(testStr);
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", md5Hex1);
|
||||
|
||||
String md5Hex2 = DigestUtil.md5Hex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
final String md5Hex2 = DigestUtil.md5Hex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", md5Hex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void md5WithSaltTest(){
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
final Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
|
||||
//加盐
|
||||
md5.setSalt("saltTest".getBytes());
|
||||
String md5Hex1 = md5.digestHex(testStr);
|
||||
final String md5Hex1 = md5.digestHex(testStr);
|
||||
Assert.assertEquals("762f7335200299dfa09bebbb601a5bc6", md5Hex1);
|
||||
String md5Hex2 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
final String md5Hex2 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
Assert.assertEquals("762f7335200299dfa09bebbb601a5bc6", md5Hex2);
|
||||
|
||||
//重复2次
|
||||
md5.setDigestCount(2);
|
||||
String md5Hex3 = md5.digestHex(testStr);
|
||||
final String md5Hex3 = md5.digestHex(testStr);
|
||||
Assert.assertEquals("2b0616296f6755d25efc07f90afe9684", md5Hex3);
|
||||
String md5Hex4 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
final String md5Hex4 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
Assert.assertEquals("2b0616296f6755d25efc07f90afe9684", md5Hex4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sha1Test(){
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
String sha1Hex1 = DigestUtil.sha1Hex(testStr);
|
||||
final String sha1Hex1 = DigestUtil.sha1Hex(testStr);
|
||||
Assert.assertEquals("ecabf586cef0d3b11c56549433ad50b81110a836", sha1Hex1);
|
||||
|
||||
String sha1Hex2 = DigestUtil.sha1Hex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
final String sha1Hex2 = DigestUtil.sha1Hex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
Assert.assertEquals("ecabf586cef0d3b11c56549433ad50b81110a836", sha1Hex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hash256Test() {
|
||||
String testStr = "Test中文";
|
||||
String hex = DigestUtil.sha256Hex(testStr);
|
||||
final String testStr = "Test中文";
|
||||
final String hex = DigestUtil.sha256Hex(testStr);
|
||||
Assert.assertEquals(64, hex.length());
|
||||
}
|
||||
}
|
||||
|
@@ -21,66 +21,66 @@ public class HmacTest {
|
||||
|
||||
@Test
|
||||
public void hmacTest(){
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
byte[] key = "password".getBytes();
|
||||
HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
|
||||
final byte[] key = "password".getBytes();
|
||||
final HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
final String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacMd5Test(){
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
HMac mac = SecureUtil.hmacMd5("password");
|
||||
final HMac mac = SecureUtil.hmacMd5("password");
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
final String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacSha1Test(){
|
||||
HMac mac = SecureUtil.hmacSha1("password");
|
||||
final HMac mac = SecureUtil.hmacSha1("password");
|
||||
|
||||
String testStr = "test中文";
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String testStr = "test中文";
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("1dd68d2f119d5640f0d416e99d3f42408b88d511", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
final String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.UTF_8));
|
||||
Assert.assertEquals("1dd68d2f119d5640f0d416e99d3f42408b88d511", macHex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zuc128MacTest(){
|
||||
byte[] iv = new byte[16];
|
||||
final byte[] iv = new byte[16];
|
||||
final byte[] key = new byte[16];
|
||||
HMac mac = new HMac("ZUC-128",
|
||||
final HMac mac = new HMac("ZUC-128",
|
||||
KeyUtil.generateKey(ZUC.ZUCAlgorithm.ZUC_128.getValue(), key),
|
||||
new IvParameterSpec(iv));
|
||||
|
||||
String testStr = "test中文";
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String testStr = "test中文";
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("1e0b9455", macHex1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zuc256MacTest(){
|
||||
final byte[] key = new byte[32];
|
||||
byte[] iv = new byte[25];
|
||||
HMac mac = new HMac("ZUC-256",
|
||||
final byte[] iv = new byte[25];
|
||||
final HMac mac = new HMac("ZUC-256",
|
||||
KeyUtil.generateKey(ZUC.ZUCAlgorithm.ZUC_128.getValue(), key),
|
||||
new IvParameterSpec(iv));
|
||||
|
||||
String testStr = "test中文";
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String testStr = "test中文";
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("d9ad618357c1bfb1d9d1200a763d5eaa", macHex1);
|
||||
}
|
||||
|
||||
@@ -88,13 +88,13 @@ public class HmacTest {
|
||||
public void sm4CMACTest(){
|
||||
// https://github.com/dromara/hutool/issues/2206
|
||||
final byte[] key = new byte[16];
|
||||
HMac mac = new HMac(HmacAlgorithm.SM4CMAC,
|
||||
final HMac mac = new HMac(HmacAlgorithm.SM4CMAC,
|
||||
KeyUtil.generateKey("SM4", key));
|
||||
|
||||
// 原文
|
||||
String testStr = "test中文";
|
||||
final String testStr = "test中文";
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
final String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("58a0d231315664af51b858a174eabc21", macHex1);
|
||||
}
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ import cn.hutool.crypto.digest.MD5;
|
||||
|
||||
/**
|
||||
* MD5 单元测试
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
@@ -15,7 +15,7 @@ public class Md5Test {
|
||||
|
||||
@Test
|
||||
public void md5To16Test() {
|
||||
String hex16 = new MD5().digestHex16("中国");
|
||||
final String hex16 = new MD5().digestHex16("中国");
|
||||
Assert.assertEquals(16, hex16.length());
|
||||
Assert.assertEquals("cb143acd6c929826", hex16);
|
||||
}
|
||||
|
@@ -18,17 +18,17 @@ public class OTPTest {
|
||||
|
||||
@Test
|
||||
public void genKeyTest() {
|
||||
String key = TOTP.generateSecretKey(8);
|
||||
final String key = TOTP.generateSecretKey(8);
|
||||
Assert.assertEquals(8, Base32.decode(key).length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validTest() {
|
||||
String key = "VYCFSW2QZ3WZO";
|
||||
final String key = "VYCFSW2QZ3WZO";
|
||||
// 2021/7/1下午6:29:54 显示code为 106659
|
||||
//Assert.assertEquals(new TOTP(Base32.decode(key)).generate(Instant.ofEpochSecond(1625135394L)),106659);
|
||||
TOTP totp = new TOTP(Base32.decode(key));
|
||||
Instant instant = Instant.ofEpochSecond(1625135394L);
|
||||
final TOTP totp = new TOTP(Base32.decode(key));
|
||||
final Instant instant = Instant.ofEpochSecond(1625135394L);
|
||||
Assert.assertTrue(totp.validate(instant, 0, 106659));
|
||||
Assert.assertTrue(totp.validate(instant.plusSeconds(30), 1, 106659));
|
||||
Assert.assertTrue(totp.validate(instant.plusSeconds(60), 2, 106659));
|
||||
@@ -39,7 +39,7 @@ public class OTPTest {
|
||||
|
||||
@Test
|
||||
public void googleAuthTest() {
|
||||
String str = TOTP.generateGoogleSecretKey("xl7@qq.com", 10);
|
||||
final String str = TOTP.generateGoogleSecretKey("xl7@qq.com", 10);
|
||||
Assert.assertTrue(str.startsWith("otpauth://totp/xl7@qq.com?secret="));
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class OTPTest {
|
||||
|
||||
@Test
|
||||
public void generateHOPTTest(){
|
||||
byte[] key = "12345678901234567890".getBytes();
|
||||
final byte[] key = "12345678901234567890".getBytes();
|
||||
final HOTP hotp = new HOTP(key);
|
||||
Assert.assertEquals(755224, hotp.generate(0));
|
||||
Assert.assertEquals(287082, hotp.generate(1));
|
||||
@@ -75,9 +75,9 @@ public class OTPTest {
|
||||
|
||||
@Test
|
||||
public void generateHmacSHA1TOPTTest(){
|
||||
HmacAlgorithm algorithm = HmacAlgorithm.HmacSHA1;
|
||||
byte[] key = "12345678901234567890".getBytes();
|
||||
TOTP totp = new TOTP(Duration.ofSeconds(30), 8, algorithm, key);
|
||||
final HmacAlgorithm algorithm = HmacAlgorithm.HmacSHA1;
|
||||
final byte[] key = "12345678901234567890".getBytes();
|
||||
final TOTP totp = new TOTP(Duration.ofSeconds(30), 8, algorithm, key);
|
||||
|
||||
int generate = totp.generate(Instant.ofEpochSecond(59L));
|
||||
Assert.assertEquals(94287082, generate);
|
||||
@@ -95,9 +95,9 @@ public class OTPTest {
|
||||
|
||||
@Test
|
||||
public void generateHmacSHA256TOPTTest(){
|
||||
HmacAlgorithm algorithm = HmacAlgorithm.HmacSHA256;
|
||||
byte[] key = "12345678901234567890123456789012".getBytes();
|
||||
TOTP totp = new TOTP(Duration.ofSeconds(30), 8, algorithm, key);
|
||||
final HmacAlgorithm algorithm = HmacAlgorithm.HmacSHA256;
|
||||
final byte[] key = "12345678901234567890123456789012".getBytes();
|
||||
final TOTP totp = new TOTP(Duration.ofSeconds(30), 8, algorithm, key);
|
||||
|
||||
int generate = totp.generate(Instant.ofEpochSecond(59L));
|
||||
Assert.assertEquals(46119246, generate);
|
||||
@@ -115,9 +115,9 @@ public class OTPTest {
|
||||
|
||||
@Test
|
||||
public void generateHmacSHA512TOPTTest(){
|
||||
HmacAlgorithm algorithm = HmacAlgorithm.HmacSHA512;
|
||||
byte[] key = "1234567890123456789012345678901234567890123456789012345678901234".getBytes();
|
||||
TOTP totp = new TOTP(Duration.ofSeconds(30), 8, algorithm, key);
|
||||
final HmacAlgorithm algorithm = HmacAlgorithm.HmacSHA512;
|
||||
final byte[] key = "1234567890123456789012345678901234567890123456789012345678901234".getBytes();
|
||||
final TOTP totp = new TOTP(Duration.ofSeconds(30), 8, algorithm, key);
|
||||
|
||||
int generate = totp.generate(Instant.ofEpochSecond(59L));
|
||||
Assert.assertEquals(90693936, generate);
|
||||
|
@@ -19,16 +19,16 @@ public class AESTest {
|
||||
@Test
|
||||
public void encryptCBCTest() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding,
|
||||
final AES aes = new AES(Mode.CBC, Padding.PKCS5Padding,
|
||||
"1234567890123456".getBytes(), "1234567890123456".getBytes());
|
||||
String encryptHex = aes.encryptHex("123456");
|
||||
final String encryptHex = aes.encryptHex("123456");
|
||||
Assert.assertEquals("d637735ae9e21ba50cb686b74fab8d2c", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptCTSTest() {
|
||||
String content = "test中文";
|
||||
AES aes = new AES(Mode.CTS, Padding.PKCS5Padding,
|
||||
final String content = "test中文";
|
||||
final AES aes = new AES(Mode.CTS, Padding.PKCS5Padding,
|
||||
"0CoJUm6Qyw8W8jue".getBytes(), "0102030405060708".getBytes());
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
Assert.assertEquals("8dc9de7f050e86ca2c8261dde56dfec9", encryptHex);
|
||||
@@ -37,9 +37,9 @@ public class AESTest {
|
||||
@Test
|
||||
public void encryptPKCS7Test() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.CBC.name(), "pkcs7padding",
|
||||
final AES aes = new AES(Mode.CBC.name(), "pkcs7padding",
|
||||
"1234567890123456".getBytes(), "1234567890123456".getBytes());
|
||||
String encryptHex = aes.encryptHex("123456");
|
||||
final String encryptHex = aes.encryptHex("123456");
|
||||
Assert.assertEquals("d637735ae9e21ba50cb686b74fab8d2c", encryptHex);
|
||||
}
|
||||
|
||||
@@ -65,17 +65,17 @@ public class AESTest {
|
||||
@Test
|
||||
public void encryptPKCS7Test2() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.ECB.name(), "pkcs7padding",
|
||||
final AES aes = new AES(Mode.ECB.name(), "pkcs7padding",
|
||||
HexUtil.decodeHex("0102030405060708090a0b0c0d0e0f10"));
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 加密数据为16进制字符串
|
||||
String encryptHex = aes.encryptHex(HexUtil.decodeHex("16c5"));
|
||||
final String encryptHex = aes.encryptHex(HexUtil.decodeHex("16c5"));
|
||||
// 加密后的Hex
|
||||
Assert.assertEquals("25869eb3ff227d9e34b3512d3c3c92ed", encryptHex);
|
||||
|
||||
// 加密数据为16进制字符串
|
||||
String encryptHex2 = aes.encryptBase64(HexUtil.decodeHex("16c5"));
|
||||
final String encryptHex2 = aes.encryptBase64(HexUtil.decodeHex("16c5"));
|
||||
// 加密后的Base64
|
||||
Assert.assertEquals("JYaes/8ifZ40s1EtPDyS7Q==", encryptHex2);
|
||||
|
||||
@@ -86,12 +86,12 @@ public class AESTest {
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 加密数据为字符串(UTF-8)
|
||||
String encryptStr = aes.encryptHex("16c5");
|
||||
final String encryptStr = aes.encryptHex("16c5");
|
||||
// 加密后的Hex
|
||||
Assert.assertEquals("79c210d3e304932cf9ea6a9c887c6d7c", encryptStr);
|
||||
|
||||
// 加密数据为字符串(UTF-8)
|
||||
String encryptStr2 = aes.encryptBase64("16c5");
|
||||
final String encryptStr2 = aes.encryptBase64("16c5");
|
||||
// 加密后的Base64
|
||||
Assert.assertEquals("ecIQ0+MEkyz56mqciHxtfA==", encryptStr2);
|
||||
|
||||
@@ -106,8 +106,8 @@ public class AESTest {
|
||||
final SecureRandom random = RandomUtil.getSecureRandom("123456".getBytes());
|
||||
final SecretKey secretKey = KeyUtil.generateKey("AES", 128, random);
|
||||
|
||||
String content = "12sdfsdfs你好啊!";
|
||||
AES aes = new AES(secretKey);
|
||||
final String content = "12sdfsdfs你好啊!";
|
||||
final AES aes = new AES(secretKey);
|
||||
final String result1 = aes.encryptBase64(content);
|
||||
|
||||
final String decryptStr = aes.decryptStr(result1);
|
||||
@@ -120,16 +120,16 @@ public class AESTest {
|
||||
@Test
|
||||
public void gcmTest() {
|
||||
final SecretKey key = KeyUtil.generateKey("AES");
|
||||
byte[] iv = RandomUtil.randomBytes(12);
|
||||
final byte[] iv = RandomUtil.randomBytes(12);
|
||||
|
||||
AES aes = new AES("GCM", "NoPadding",
|
||||
final AES aes = new AES("GCM", "NoPadding",
|
||||
key,
|
||||
new GCMParameterSpec(128, iv));
|
||||
|
||||
// 原始数据
|
||||
String phone = "13534534567";
|
||||
final String phone = "13534534567";
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(phone);
|
||||
final byte[] encrypt = aes.encrypt(phone);
|
||||
final String decryptStr = aes.decryptStr(encrypt);
|
||||
Assert.assertEquals(phone, decryptStr);
|
||||
}
|
||||
|
@@ -14,17 +14,17 @@ public class ChaCha20Test {
|
||||
@Test
|
||||
public void encryptAndDecryptTest() {
|
||||
// 32 for 256 bit key or 16 for 128 bit
|
||||
byte[] key = RandomUtil.randomBytes(32);
|
||||
final byte[] key = RandomUtil.randomBytes(32);
|
||||
// 64 bit IV required by ChaCha20
|
||||
byte[] iv = RandomUtil.randomBytes(12);
|
||||
final byte[] iv = RandomUtil.randomBytes(12);
|
||||
|
||||
final ChaCha20 chacha = new ChaCha20(key, iv);
|
||||
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
// 加密为16进制表示
|
||||
String encryptHex = chacha.encryptHex(content);
|
||||
final String encryptHex = chacha.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = chacha.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String decryptStr = chacha.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ public class DesTest {
|
||||
|
||||
@Test
|
||||
public void encryptDecryptTest(){
|
||||
String content = "我是一个测试的test字符串123";
|
||||
final String content = "我是一个测试的test字符串123";
|
||||
final DES des = SecureUtil.des();
|
||||
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
@@ -26,7 +26,7 @@ public class DesTest {
|
||||
|
||||
@Test
|
||||
public void encryptDecryptWithCustomTest(){
|
||||
String content = "我是一个测试的test字符串123";
|
||||
final String content = "我是一个测试的test字符串123";
|
||||
final DES des = new DES(
|
||||
Mode.CTS,
|
||||
Padding.PKCS5Padding,
|
||||
|
@@ -10,62 +10,62 @@ public class RC4Test {
|
||||
|
||||
@Test
|
||||
public void testCryptMessage() {
|
||||
String key = "This is pretty long key";
|
||||
RC4 rc4 = new RC4(key);
|
||||
String message = "Hello, World!";
|
||||
byte[] crypt = rc4.encrypt(message);
|
||||
String msg = rc4.decrypt(crypt);
|
||||
final String key = "This is pretty long key";
|
||||
final RC4 rc4 = new RC4(key);
|
||||
final String message = "Hello, World!";
|
||||
final byte[] crypt = rc4.encrypt(message);
|
||||
final String msg = rc4.decrypt(crypt);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "Hello, World, this is megssage 2";
|
||||
byte[] crypt2 = rc4.encrypt(message2);
|
||||
String msg2 = rc4.decrypt(crypt2);
|
||||
final String message2 = "Hello, World, this is megssage 2";
|
||||
final byte[] crypt2 = rc4.encrypt(message2);
|
||||
final String msg2 = rc4.decrypt(crypt2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCryptWithChineseCharacters() {
|
||||
String message = "这是一个中文消息!";
|
||||
String key = "我是一个文件密钥";
|
||||
RC4 rc4 = new RC4(key);
|
||||
byte[] crypt = rc4.encrypt(message);
|
||||
String msg = rc4.decrypt(crypt);
|
||||
final String message = "这是一个中文消息!";
|
||||
final String key = "我是一个文件密钥";
|
||||
final RC4 rc4 = new RC4(key);
|
||||
final byte[] crypt = rc4.encrypt(message);
|
||||
final String msg = rc4.decrypt(crypt);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第二个中文消息!";
|
||||
byte[] crypt2 = rc4.encrypt(message2);
|
||||
String msg2 = rc4.decrypt(crypt2);
|
||||
final String message2 = "这是第二个中文消息!";
|
||||
final byte[] crypt2 = rc4.encrypt(message2);
|
||||
final String msg2 = rc4.decrypt(crypt2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptWithHexMessage() {
|
||||
String message = "这是第一个用来测试密文为十六进制字符串的消息!";
|
||||
String key = "生成一个密钥";
|
||||
RC4 rc4 = new RC4(key);
|
||||
String encryptHex = rc4.encryptHex(message, CharsetUtil.UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
final String message = "这是第一个用来测试密文为十六进制字符串的消息!";
|
||||
final String key = "生成一个密钥";
|
||||
final RC4 rc4 = new RC4(key);
|
||||
final String encryptHex = rc4.encryptHex(message, CharsetUtil.UTF_8);
|
||||
final String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第二个用来测试密文为十六进制字符串的消息!";
|
||||
String encryptHex2 = rc4.encryptHex(message2);
|
||||
String msg2 = rc4.decrypt(encryptHex2);
|
||||
final String message2 = "这是第二个用来测试密文为十六进制字符串的消息!";
|
||||
final String encryptHex2 = rc4.encryptHex(message2);
|
||||
final String msg2 = rc4.decrypt(encryptHex2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecryptWithBase64Message() {
|
||||
String message = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
String key = "生成一个密钥";
|
||||
RC4 rc4 = new RC4(key);
|
||||
String encryptHex = rc4.encryptBase64(message, CharsetUtil.UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
final String message = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
final String key = "生成一个密钥";
|
||||
final RC4 rc4 = new RC4(key);
|
||||
final String encryptHex = rc4.encryptBase64(message, CharsetUtil.UTF_8);
|
||||
final String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
String encryptHex2 = rc4.encryptBase64(message2);
|
||||
String msg2 = rc4.decrypt(encryptHex2);
|
||||
final String message2 = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
final String encryptHex2 = rc4.encryptBase64(message2);
|
||||
final String msg2 = rc4.decrypt(encryptHex2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
}
|
||||
|
@@ -22,29 +22,29 @@ public class Sm4StreamTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void sm4Test(){
|
||||
String source = "d:/test/sm4_1.txt";
|
||||
String target = "d:/test/sm4_2.data";
|
||||
String target2 = "d:/test/sm4_3.txt";
|
||||
final String source = "d:/test/sm4_1.txt";
|
||||
final String target = "d:/test/sm4_2.data";
|
||||
final String target2 = "d:/test/sm4_3.txt";
|
||||
encrypt(source, target);
|
||||
decrypt(target, target2);
|
||||
}
|
||||
|
||||
public static void encrypt(String source, String target) {
|
||||
try (InputStream input = new FileInputStream(source);
|
||||
OutputStream out = new FileOutputStream(target)) {
|
||||
public static void encrypt(final String source, final String target) {
|
||||
try (final InputStream input = new FileInputStream(source);
|
||||
final OutputStream out = new FileOutputStream(target)) {
|
||||
sm4.encrypt(input, out, IS_CLOSE);
|
||||
System.out.println("============encrypt end");
|
||||
} catch (IOException e) {
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void decrypt(String source, String target) {
|
||||
try (InputStream input = new FileInputStream(source);
|
||||
OutputStream out = new FileOutputStream(target)) {
|
||||
public static void decrypt(final String source, final String target) {
|
||||
try (final InputStream input = new FileInputStream(source);
|
||||
final OutputStream out = new FileOutputStream(target)) {
|
||||
sm4.decrypt(input, out, IS_CLOSE);
|
||||
System.out.println("============decrypt end");
|
||||
} catch (IOException e) {
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@@ -30,94 +30,94 @@ public class SymmetricTest {
|
||||
|
||||
@Test
|
||||
public void aesTest() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
// 随机生成密钥
|
||||
byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
final byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
|
||||
// 构建
|
||||
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
|
||||
final SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
final byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
final byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.str(decrypt, CharsetUtil.UTF_8));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesTest2() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
// 随机生成密钥
|
||||
byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
final byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
|
||||
// 构建
|
||||
AES aes = SecureUtil.aes(key);
|
||||
final AES aes = SecureUtil.aes(key);
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
final byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
final byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesTest3() {
|
||||
String content = "test中文aaaaaaaaaaaaaaaaaaaaa";
|
||||
final String content = "test中文aaaaaaaaaaaaaaaaaaaaa";
|
||||
|
||||
AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());
|
||||
final AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
final byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
final byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
final String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesTest4() {
|
||||
String content = "4321c9a2db2e6b08987c3b903d8d11ff";
|
||||
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
final String content = "4321c9a2db2e6b08987c3b903d8d11ff";
|
||||
final AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
|
||||
Assert.assertEquals("cd0e3a249eaf0ed80c330338508898c4bddcfd665a1b414622164a273ca5daf7b4ebd2c00aaa66b84dd0a237708dac8e", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pbeWithoutIvTest() {
|
||||
String content = "4321c9a2db2e6b08987c3b903d8d11ff";
|
||||
SymmetricCrypto crypto = new SymmetricCrypto(SymmetricAlgorithm.PBEWithMD5AndDES,
|
||||
final String content = "4321c9a2db2e6b08987c3b903d8d11ff";
|
||||
final SymmetricCrypto crypto = new SymmetricCrypto(SymmetricAlgorithm.PBEWithMD5AndDES,
|
||||
"0123456789ABHAEQ".getBytes());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = crypto.encryptHex(content);
|
||||
final String encryptHex = crypto.encryptHex(content);
|
||||
final String data = crypto.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, data);
|
||||
@@ -125,14 +125,14 @@ public class SymmetricTest {
|
||||
|
||||
@Test
|
||||
public void aesUpdateTest() {
|
||||
String content = "4321c9a2db2e6b08987c3b903d8d11ff";
|
||||
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
final String content = "4321c9a2db2e6b08987c3b903d8d11ff";
|
||||
final AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
|
||||
// 加密为16进制表示
|
||||
aes.setMode(CipherMode.encrypt);
|
||||
String randomData = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
|
||||
final String randomData = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
|
||||
aes.setMode(CipherMode.encrypt);
|
||||
String randomData2 = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
|
||||
final String randomData2 = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
|
||||
Assert.assertEquals(randomData2, randomData);
|
||||
Assert.assertEquals(randomData, "cd0e3a249eaf0ed80c330338508898c4");
|
||||
}
|
||||
@@ -140,20 +140,20 @@ public class SymmetricTest {
|
||||
|
||||
@Test
|
||||
public void aesZeroPaddingTest() {
|
||||
String content = RandomUtil.randomString(RandomUtil.randomInt(200));
|
||||
AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
final String content = RandomUtil.randomString(RandomUtil.randomInt(200));
|
||||
final AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
// 解密
|
||||
String decryptStr = aes.decryptStr(encryptHex);
|
||||
final String decryptStr = aes.decryptStr(encryptHex);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesZeroPaddingTest2() {
|
||||
String content = "RandomUtil.randomString(RandomUtil.randomInt(2000))";
|
||||
AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
final String content = "RandomUtil.randomString(RandomUtil.randomInt(2000))";
|
||||
final AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
|
||||
final ByteArrayOutputStream encryptStream = new ByteArrayOutputStream();
|
||||
aes.encrypt(IoUtil.toUtf8Stream(content), encryptStream, true);
|
||||
@@ -166,117 +166,117 @@ public class SymmetricTest {
|
||||
|
||||
@Test
|
||||
public void aesPkcs7PaddingTest() {
|
||||
String content = RandomUtil.randomString(RandomUtil.randomInt(200));
|
||||
AES aes = new AES("CBC", "PKCS7Padding",
|
||||
final String content = RandomUtil.randomString(RandomUtil.randomInt(200));
|
||||
final AES aes = new AES("CBC", "PKCS7Padding",
|
||||
RandomUtil.randomBytes(32),
|
||||
"DYgjCEIMVrj2W9xN".getBytes());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
// 解密
|
||||
String decryptStr = aes.decryptStr(encryptHex);
|
||||
final String decryptStr = aes.decryptStr(encryptHex);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
|
||||
SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DES, key);
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
final SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DES, key);
|
||||
final byte[] encrypt = des.encrypt(content);
|
||||
final byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest2() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
|
||||
DES des = SecureUtil.des(key);
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
final DES des = SecureUtil.des(key);
|
||||
final byte[] encrypt = des.encrypt(content);
|
||||
final byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest3() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
DES des = new DES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "01020304".getBytes());
|
||||
final DES des = new DES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "01020304".getBytes());
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
final byte[] encrypt = des.encrypt(content);
|
||||
final byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desdeTest() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
DESede des = SecureUtil.desede(key);
|
||||
final DESede des = SecureUtil.desede(key);
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
final byte[] encrypt = des.encrypt(content);
|
||||
final byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desdeTest2() {
|
||||
String content = "test中文";
|
||||
final String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
final byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
DESede des = new DESede(Mode.CBC, Padding.PKCS5Padding, key, "12345678".getBytes());
|
||||
final DESede des = new DESede(Mode.CBC, Padding.PKCS5Padding, key, "12345678".getBytes());
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
final byte[] encrypt = des.encrypt(content);
|
||||
final byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vigenereTest() {
|
||||
String content = "Wherethereisawillthereisaway";
|
||||
String key = "CompleteVictory";
|
||||
final String content = "Wherethereisawillthereisaway";
|
||||
final String key = "CompleteVictory";
|
||||
|
||||
String encrypt = Vigenere.encrypt(content, key);
|
||||
final String encrypt = Vigenere.encrypt(content, key);
|
||||
Assert.assertEquals("zXScRZ]KIOMhQjc0\\bYRXZOJK[Vi", encrypt);
|
||||
String decrypt = Vigenere.decrypt(encrypt, key);
|
||||
final String decrypt = Vigenere.decrypt(encrypt, key);
|
||||
Assert.assertEquals(content, decrypt);
|
||||
}
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ public class TEATest {
|
||||
|
||||
@Test
|
||||
public void teaTest() {
|
||||
String data = "测试的加密数据 by Hutool";
|
||||
final String data = "测试的加密数据 by Hutool";
|
||||
|
||||
// 密钥必须为128bit
|
||||
final SymmetricCrypto tea = new SymmetricCrypto("TEA", "MyPassword123456".getBytes());
|
||||
@@ -26,7 +26,7 @@ public class TEATest {
|
||||
|
||||
@Test
|
||||
public void xteaTest() {
|
||||
String data = "测试的加密数据 by Hutool";
|
||||
final String data = "测试的加密数据 by Hutool";
|
||||
|
||||
// 密钥必须为128bit
|
||||
final SymmetricCrypto tea = new SymmetricCrypto("XTEA", "MyPassword123456".getBytes());
|
||||
@@ -40,7 +40,7 @@ public class TEATest {
|
||||
|
||||
@Test
|
||||
public void xxteaTest() {
|
||||
String data = "测试的加密数据 by Hutool";
|
||||
final String data = "测试的加密数据 by Hutool";
|
||||
|
||||
// 密钥必须为128bit
|
||||
final XXTEA tea = new XXTEA("MyPassword123456".getBytes());
|
||||
|
@@ -11,24 +11,24 @@ public class ZucTest {
|
||||
@Test
|
||||
public void zuc128Test(){
|
||||
final byte[] secretKey = ZUC.generateKey(ZUC.ZUCAlgorithm.ZUC_128);
|
||||
byte[] iv = RandomUtil.randomBytes(16);
|
||||
final byte[] iv = RandomUtil.randomBytes(16);
|
||||
final ZUC zuc = new ZUC(ZUC.ZUCAlgorithm.ZUC_128, secretKey, iv);
|
||||
|
||||
String msg = RandomUtil.randomString(500);
|
||||
byte[] crypt2 = zuc.encrypt(msg);
|
||||
String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8);
|
||||
final String msg = RandomUtil.randomString(500);
|
||||
final byte[] crypt2 = zuc.encrypt(msg);
|
||||
final String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals(msg, msg2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zuc256Test(){
|
||||
final byte[] secretKey = ZUC.generateKey(ZUC.ZUCAlgorithm.ZUC_256);
|
||||
byte[] iv = RandomUtil.randomBytes(25);
|
||||
final byte[] iv = RandomUtil.randomBytes(25);
|
||||
final ZUC zuc = new ZUC(ZUC.ZUCAlgorithm.ZUC_256, secretKey, iv);
|
||||
|
||||
String msg = RandomUtil.randomString(500);
|
||||
byte[] crypt2 = zuc.encrypt(msg);
|
||||
String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8);
|
||||
final String msg = RandomUtil.randomString(500);
|
||||
final byte[] crypt2 = zuc.encrypt(msg);
|
||||
final String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals(msg, msg2);
|
||||
}
|
||||
}
|
||||
|
@@ -11,14 +11,14 @@ public class FPETest {
|
||||
@Test
|
||||
public void ff1Test(){
|
||||
// 映射字符表,规定了明文和密文的字符范围
|
||||
BasicAlphabetMapper numberMapper = new BasicAlphabetMapper("A0123456789");
|
||||
final BasicAlphabetMapper numberMapper = new BasicAlphabetMapper("A0123456789");
|
||||
// 初始化 aes 密钥
|
||||
byte[] keyBytes = RandomUtil.randomBytes(16);
|
||||
final byte[] keyBytes = RandomUtil.randomBytes(16);
|
||||
|
||||
final FPE fpe = new FPE(FPE.FPEMode.FF1, keyBytes, numberMapper, null);
|
||||
|
||||
// 原始数据
|
||||
String phone = RandomUtil.randomString("A0123456789", 13);
|
||||
final String phone = RandomUtil.randomString("A0123456789", 13);
|
||||
final String encrypt = fpe.encrypt(phone);
|
||||
// 加密后与原密文长度一致
|
||||
Assert.assertEquals(phone.length(), encrypt.length());
|
||||
@@ -30,14 +30,14 @@ public class FPETest {
|
||||
@Test
|
||||
public void ff3Test(){
|
||||
// 映射字符表,规定了明文和密文的字符范围
|
||||
BasicAlphabetMapper numberMapper = new BasicAlphabetMapper("A0123456789");
|
||||
final BasicAlphabetMapper numberMapper = new BasicAlphabetMapper("A0123456789");
|
||||
// 初始化 aes 密钥
|
||||
byte[] keyBytes = RandomUtil.randomBytes(16);
|
||||
final byte[] keyBytes = RandomUtil.randomBytes(16);
|
||||
|
||||
final FPE fpe = new FPE(FPE.FPEMode.FF3_1, keyBytes, numberMapper, null);
|
||||
|
||||
// 原始数据
|
||||
String phone = RandomUtil.randomString("A0123456789", 13);
|
||||
final String phone = RandomUtil.randomString("A0123456789", 13);
|
||||
final String encrypt = fpe.encrypt(phone);
|
||||
// 加密后与原密文长度一致
|
||||
Assert.assertEquals(phone.length(), encrypt.length());
|
||||
|
Reference in New Issue
Block a user