mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
change line sep
This commit is contained in:
@@ -1,30 +1,30 @@
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.crypto.BCUtil;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BCUtilTest {
|
||||
|
||||
/**
|
||||
* 密钥生成来自:https://i.goto327.top/CryptTools/SM2.aspx?tdsourcetag=s_pctim_aiomsg
|
||||
*/
|
||||
@Test
|
||||
public void createECPublicKeyParametersTest() {
|
||||
String x = "706AD9DAA3E5CEAC3DA59F583429E8043BAFC576BE10092C4EA4D8E19846CA62";
|
||||
String y = "F7E938B02EED7280277493B8556E5B01CB436E018A562DFDC53342BF41FDF728";
|
||||
|
||||
final ECPublicKeyParameters keyParameters = BCUtil.toSm2Params(x, y);
|
||||
Assert.notNull(keyParameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createECPrivateKeyParametersTest() {
|
||||
String privateKeyHex = "5F6CA5BB044C40ED2355F0372BF72A5B3AE6943712F9FDB7C1FFBAECC06F3829";
|
||||
|
||||
final ECPrivateKeyParameters keyParameters = BCUtil.toSm2Params(privateKeyHex);
|
||||
Assert.notNull(keyParameters);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.crypto.BCUtil;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BCUtilTest {
|
||||
|
||||
/**
|
||||
* 密钥生成来自:https://i.goto327.top/CryptTools/SM2.aspx?tdsourcetag=s_pctim_aiomsg
|
||||
*/
|
||||
@Test
|
||||
public void createECPublicKeyParametersTest() {
|
||||
String x = "706AD9DAA3E5CEAC3DA59F583429E8043BAFC576BE10092C4EA4D8E19846CA62";
|
||||
String y = "F7E938B02EED7280277493B8556E5B01CB436E018A562DFDC53342BF41FDF728";
|
||||
|
||||
final ECPublicKeyParameters keyParameters = BCUtil.toSm2Params(x, y);
|
||||
Assert.notNull(keyParameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createECPrivateKeyParametersTest() {
|
||||
String privateKeyHex = "5F6CA5BB044C40ED2355F0372BF72A5B3AE6943712F9FDB7C1FFBAECC06F3829";
|
||||
|
||||
final ECPrivateKeyParameters keyParameters = BCUtil.toSm2Params(privateKeyHex);
|
||||
Assert.notNull(keyParameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.crypto.CryptoException;
|
||||
import cn.hutool.crypto.GlobalBouncyCastleProvider;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
public class KeyUtilTest {
|
||||
|
||||
/**
|
||||
* 测试关闭BouncyCastle支持时是否会正常抛出异常,即关闭是否有效
|
||||
*/
|
||||
@Test(expected = CryptoException.class)
|
||||
@Ignore
|
||||
public void generateKeyPairTest() {
|
||||
GlobalBouncyCastleProvider.setUseBouncyCastle(false);
|
||||
KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
Assert.assertNotNull(pair);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRSAPublicKeyTest(){
|
||||
final KeyPair keyPair = KeyUtil.generateKeyPair("RSA");
|
||||
final PrivateKey aPrivate = keyPair.getPrivate();
|
||||
final PublicKey rsaPublicKey = KeyUtil.getRSAPublicKey(aPrivate);
|
||||
Assert.assertEquals(rsaPublicKey, keyPair.getPublic());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试EC和ECIES算法生成的KEY是一致的
|
||||
*/
|
||||
@Test
|
||||
public void generateECIESKeyTest(){
|
||||
final KeyPair ecies = KeyUtil.generateKeyPair("ECIES");
|
||||
Assert.assertNotNull(ecies.getPrivate());
|
||||
Assert.assertNotNull(ecies.getPublic());
|
||||
|
||||
byte[] privateKeyBytes = ecies.getPrivate().getEncoded();
|
||||
|
||||
final PrivateKey privateKey = KeyUtil.generatePrivateKey("EC", privateKeyBytes);
|
||||
Assert.assertEquals(ecies.getPrivate(), privateKey);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.crypto.CryptoException;
|
||||
import cn.hutool.crypto.GlobalBouncyCastleProvider;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
public class KeyUtilTest {
|
||||
|
||||
/**
|
||||
* 测试关闭BouncyCastle支持时是否会正常抛出异常,即关闭是否有效
|
||||
*/
|
||||
@Test(expected = CryptoException.class)
|
||||
@Ignore
|
||||
public void generateKeyPairTest() {
|
||||
GlobalBouncyCastleProvider.setUseBouncyCastle(false);
|
||||
KeyPair pair = KeyUtil.generateKeyPair("SM2");
|
||||
Assert.assertNotNull(pair);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRSAPublicKeyTest(){
|
||||
final KeyPair keyPair = KeyUtil.generateKeyPair("RSA");
|
||||
final PrivateKey aPrivate = keyPair.getPrivate();
|
||||
final PublicKey rsaPublicKey = KeyUtil.getRSAPublicKey(aPrivate);
|
||||
Assert.assertEquals(rsaPublicKey, keyPair.getPublic());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试EC和ECIES算法生成的KEY是一致的
|
||||
*/
|
||||
@Test
|
||||
public void generateECIESKeyTest(){
|
||||
final KeyPair ecies = KeyUtil.generateKeyPair("ECIES");
|
||||
Assert.assertNotNull(ecies.getPrivate());
|
||||
Assert.assertNotNull(ecies.getPublic());
|
||||
|
||||
byte[] privateKeyBytes = ecies.getPrivate().getEncoded();
|
||||
|
||||
final PrivateKey privateKey = KeyUtil.generatePrivateKey("EC", privateKeyBytes);
|
||||
Assert.assertEquals(ecies.getPrivate(), privateKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.crypto.PemUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
public class PemUtilTest {
|
||||
|
||||
@Test
|
||||
public void readPrivateKeyTest() {
|
||||
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"));
|
||||
Assert.assertNotNull(publicKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readPemKeyTest() {
|
||||
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"));
|
||||
|
||||
RSA rsa = new RSA(privateKey, publicKey);
|
||||
String str = "你好,Hutool";//测试字符串
|
||||
|
||||
String encryptStr = rsa.encryptBase64(str, KeyType.PublicKey);
|
||||
String decryptStr = rsa.decryptStr(encryptStr, KeyType.PrivateKey);
|
||||
Assert.assertEquals(str, decryptStr);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.crypto.PemUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
public class PemUtilTest {
|
||||
|
||||
@Test
|
||||
public void readPrivateKeyTest() {
|
||||
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"));
|
||||
Assert.assertNotNull(publicKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readPemKeyTest() {
|
||||
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"));
|
||||
|
||||
RSA rsa = new RSA(privateKey, publicKey);
|
||||
String str = "你好,Hutool";//测试字符串
|
||||
|
||||
String encryptStr = rsa.encryptBase64(str, KeyType.PublicKey);
|
||||
String decryptStr = rsa.decryptStr(encryptStr, KeyType.PrivateKey);
|
||||
Assert.assertEquals(str, decryptStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,84 +1,84 @@
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
import cn.hutool.crypto.symmetric.SM4;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
|
||||
/**
|
||||
* SM单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SmTest {
|
||||
|
||||
@Test
|
||||
public void sm3Test() {
|
||||
String digestHex = SmUtil.sm3("aaaaa");
|
||||
Assert.assertEquals("136ce3c86e4ed909b76082055a61586af20b4dab674732ebd4b599eef080c9be", digestHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4Test() {
|
||||
String content = "test中文";
|
||||
SM4 sm4 = SmUtil.sm4();
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4Test2() {
|
||||
String content = "test中文";
|
||||
SM4 sm4 = new SM4(Mode.CTR, Padding.PKCS5Padding);
|
||||
sm4.setIv("aaaabbbb".getBytes());
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4ECBPKCS5PaddingTest2() {
|
||||
String content = "test中文";
|
||||
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.CHARSET_UTF_8);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4TestWithCustomKeyTest() {
|
||||
String content = "test中文";
|
||||
|
||||
SecretKey key = KeyUtil.generateKey(SM4.ALGORITHM_NAME);
|
||||
|
||||
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.CHARSET_UTF_8);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacSm3Test() {
|
||||
String content = "test中文";
|
||||
HMac hMac = SmUtil.hmacSm3("password".getBytes());
|
||||
String digest = hMac.digestHex(content);
|
||||
Assert.assertEquals("493e3f9a1896b43075fbe54658076727960d69632ac6b6ed932195857a6840c6", digest);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
import cn.hutool.crypto.symmetric.SM4;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
|
||||
/**
|
||||
* SM单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SmTest {
|
||||
|
||||
@Test
|
||||
public void sm3Test() {
|
||||
String digestHex = SmUtil.sm3("aaaaa");
|
||||
Assert.assertEquals("136ce3c86e4ed909b76082055a61586af20b4dab674732ebd4b599eef080c9be", digestHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4Test() {
|
||||
String content = "test中文";
|
||||
SM4 sm4 = SmUtil.sm4();
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4Test2() {
|
||||
String content = "test中文";
|
||||
SM4 sm4 = new SM4(Mode.CTR, Padding.PKCS5Padding);
|
||||
sm4.setIv("aaaabbbb".getBytes());
|
||||
|
||||
String encryptHex = sm4.encryptHex(content);
|
||||
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4ECBPKCS5PaddingTest2() {
|
||||
String content = "test中文";
|
||||
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.CHARSET_UTF_8);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4TestWithCustomKeyTest() {
|
||||
String content = "test中文";
|
||||
|
||||
SecretKey key = KeyUtil.generateKey(SM4.ALGORITHM_NAME);
|
||||
|
||||
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.CHARSET_UTF_8);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacSm3Test() {
|
||||
String content = "test中文";
|
||||
HMac hMac = SmUtil.hmacSm3("password".getBytes());
|
||||
String digest = hMac.digestHex(content);
|
||||
Assert.assertEquals("493e3f9a1896b43075fbe54658076727960d69632ac6b6ed932195857a6840c6", digest);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,47 @@
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
|
||||
import cn.hutool.crypto.asymmetric.ECIES;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ECIESTest {
|
||||
|
||||
@Test
|
||||
public void eciesTest(){
|
||||
final ECIES ecies = new ECIES();
|
||||
|
||||
doTest(ecies, ecies);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eciesTest2(){
|
||||
final ECIES ecies = new ECIES();
|
||||
final byte[] privateKeyBytes = ecies.getPrivateKey().getEncoded();
|
||||
final ECIES ecies2 = new ECIES(privateKeyBytes, null);
|
||||
|
||||
doTest(ecies, ecies2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试用例
|
||||
*
|
||||
* @param cryptoForEncrypt 加密的Crypto
|
||||
* @param cryptoForDecrypt 解密的Crypto
|
||||
*/
|
||||
private void doTest(AsymmetricCrypto cryptoForEncrypt, AsymmetricCrypto cryptoForDecrypt){
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = cryptoForEncrypt.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
|
||||
String decryptStr = StrUtil.utf8Str(cryptoForDecrypt.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
|
||||
import cn.hutool.crypto.asymmetric.ECIES;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ECIESTest {
|
||||
|
||||
@Test
|
||||
public void eciesTest(){
|
||||
final ECIES ecies = new ECIES();
|
||||
|
||||
doTest(ecies, ecies);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eciesTest2(){
|
||||
final ECIES ecies = new ECIES();
|
||||
final byte[] privateKeyBytes = ecies.getPrivateKey().getEncoded();
|
||||
final ECIES ecies2 = new ECIES(privateKeyBytes, null);
|
||||
|
||||
doTest(ecies, ecies2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试用例
|
||||
*
|
||||
* @param cryptoForEncrypt 加密的Crypto
|
||||
* @param cryptoForDecrypt 解密的Crypto
|
||||
*/
|
||||
private void doTest(AsymmetricCrypto cryptoForEncrypt, AsymmetricCrypto cryptoForDecrypt){
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = cryptoForEncrypt.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
|
||||
String decryptStr = StrUtil.utf8Str(cryptoForDecrypt.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,244 +1,244 @@
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.math.BigInteger;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PublicKey;
|
||||
|
||||
/**
|
||||
* RSA算法单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class RSATest {
|
||||
|
||||
@Test
|
||||
public void generateKeyPairTest() {
|
||||
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();
|
||||
|
||||
RSA rsa = SecureUtil.rsa(privateKey, publicKey);
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaTest() {
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaECBTest() {
|
||||
final RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB.getValue());
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaNoneTest() {
|
||||
final RSA rsa = new RSA(AsymmetricAlgorithm.RSA_None.getValue());
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaWithBlockTest2() {
|
||||
final RSA rsa = new RSA();
|
||||
rsa.setEncryptBlockSize(3);
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaBcdTest() {
|
||||
String text = "我是一段测试aaaa";
|
||||
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = rsa.encryptBcd(text, KeyType.PublicKey);
|
||||
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));
|
||||
Assert.assertEquals(text, decrypt2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaBase64Test() {
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = rsa.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
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));
|
||||
Assert.assertEquals(text.toString(), decrypt2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaDecodeTest() {
|
||||
String PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIL7pbQ+5KKGYRhw7jE31hmA" //
|
||||
+ "f8Q60ybd+xZuRmuO5kOFBRqXGxKTQ9TfQI+aMW+0lw/kibKzaD/EKV91107xE384qOy6IcuBfaR5lv39OcoqNZ"//
|
||||
+ "5l+Dah5ABGnVkBP9fKOFhPgghBknTRo0/rZFGI6Q1UHXb+4atP++LNFlDymJcPAgMBAAECgYBammGb1alndta" //
|
||||
+ "xBmTtLLdveoBmp14p04D8mhkiC33iFKBcLUvvxGg2Vpuc+cbagyu/NZG+R/WDrlgEDUp6861M5BeFN0L9O4hz"//
|
||||
+ "GAEn8xyTE96f8sh4VlRmBOvVdwZqRO+ilkOM96+KL88A9RKdp8V2tna7TM6oI3LHDyf/JBoXaQJBAMcVN7fKlYP" //
|
||||
+ "Skzfh/yZzW2fmC0ZNg/qaW8Oa/wfDxlWjgnS0p/EKWZ8BxjR/d199L3i/KMaGdfpaWbYZLvYENqUCQQCobjsuCW"//
|
||||
+ "nlZhcWajjzpsSuy8/bICVEpUax1fUZ58Mq69CQXfaZemD9Ar4omzuEAAs2/uee3kt3AvCBaeq05NyjAkBme8SwB0iK"//
|
||||
+ "kLcaeGuJlq7CQIkjSrobIqUEf+CzVZPe+AorG+isS+Cw2w/2bHu+G0p5xSYvdH59P0+ZT0N+f9LFAkA6v3Ae56OrI"//
|
||||
+ "wfMhrJksfeKbIaMjNLS9b8JynIaXg9iCiyOHmgkMl5gAbPoH/ULXqSKwzBw5mJ2GW1gBlyaSfV3AkA/RJC+adIjsRGg"//
|
||||
+ "JOkiRjSmPpGv3FOhl9fsBPjupZBEIuoMWOC8GXK/73DHxwmfNmN7C9+sIi4RBcjEeQ5F5FHZ";
|
||||
|
||||
RSA rsa = new RSA(PRIVATE_KEY, null);
|
||||
|
||||
String a = "2707F9FD4288CEF302C972058712F24A5F3EC62C5A14AD2FC59DAB93503AA0FA17113A020EE4EA35EB53F" //
|
||||
+ "75F36564BA1DABAA20F3B90FD39315C30E68FE8A1803B36C29029B23EB612C06ACF3A34BE815074F5EB5AA3A"//
|
||||
+ "C0C8832EC42DA725B4E1C38EF4EA1B85904F8B10B2D62EA782B813229F9090E6F7394E42E6F44494BB8";
|
||||
|
||||
byte[] aByte = HexUtil.decodeHex(a);
|
||||
byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
|
||||
|
||||
Assert.assertEquals("虎头闯杭州,多抬头看天,切勿只管种地", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaTest2() throws Exception {
|
||||
String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6s" +
|
||||
"XqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9S" +
|
||||
"dB1Ua44oncaTWz7OBGLbCiK45wIDAQAB";
|
||||
|
||||
byte[] keyBytes = Base64.decode(publicKeyStr);
|
||||
PublicKey publicKey = KeyUtil.generateRSAPublicKey(keyBytes);
|
||||
|
||||
byte[] data = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).getBytes();
|
||||
//长度不满足128补0
|
||||
byte[] finalData = ArrayUtil.resize(data, 128);
|
||||
|
||||
//jdk原生加密
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
String result1 = HexUtil.encodeHexStr(cipher.doFinal(finalData));
|
||||
|
||||
//hutool加密
|
||||
RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);
|
||||
rsa.setEncryptBlockSize(128);
|
||||
String result2 = rsa.encryptHex(finalData, KeyType.PublicKey);
|
||||
|
||||
Assert.assertEquals(result1, result2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exponentTest(){
|
||||
String modulus = "BD99BAAB9E56B7FD85FB8BCF53CAD2913C1ACEF9063E7C913CD6FC4FEE040DA44D8" +
|
||||
"ADAA35A9DCABD6E936C402D47278049638407135BAB22BB091396CB6873195C8AC8B0B7AB123" +
|
||||
"C3BF7A6341A4419BDBC0EFB85DBCD9A3AD12C99E2265BDCC1197913749E2AFA568EB7623DA3A" +
|
||||
"361335AA1F9FFA6E1801DDC8228AA86306B87";
|
||||
String publicExponent = "65537";
|
||||
RSA rsa = new RSA(new BigInteger(modulus, 16), null, new BigInteger(publicExponent));
|
||||
|
||||
final String encryptBase64 = rsa.encryptBase64("测试内容", KeyType.PublicKey);
|
||||
Assert.assertNotNull(encryptBase64);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.math.BigInteger;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PublicKey;
|
||||
|
||||
/**
|
||||
* RSA算法单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class RSATest {
|
||||
|
||||
@Test
|
||||
public void generateKeyPairTest() {
|
||||
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();
|
||||
|
||||
RSA rsa = SecureUtil.rsa(privateKey, publicKey);
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaTest() {
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaECBTest() {
|
||||
final RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB.getValue());
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaNoneTest() {
|
||||
final RSA rsa = new RSA(AsymmetricAlgorithm.RSA_None.getValue());
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaWithBlockTest2() {
|
||||
final RSA rsa = new RSA();
|
||||
rsa.setEncryptBlockSize(3);
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(rsa.getPrivateKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(rsa.getPublicKey());
|
||||
Assert.assertNotNull(rsa.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 私钥加密,公钥解密
|
||||
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
|
||||
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaBcdTest() {
|
||||
String text = "我是一段测试aaaa";
|
||||
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = rsa.encryptBcd(text, KeyType.PublicKey);
|
||||
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));
|
||||
Assert.assertEquals(text, decrypt2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaBase64Test() {
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
final RSA rsa = new RSA();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = rsa.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
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));
|
||||
Assert.assertEquals(text.toString(), decrypt2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaDecodeTest() {
|
||||
String PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIL7pbQ+5KKGYRhw7jE31hmA" //
|
||||
+ "f8Q60ybd+xZuRmuO5kOFBRqXGxKTQ9TfQI+aMW+0lw/kibKzaD/EKV91107xE384qOy6IcuBfaR5lv39OcoqNZ"//
|
||||
+ "5l+Dah5ABGnVkBP9fKOFhPgghBknTRo0/rZFGI6Q1UHXb+4atP++LNFlDymJcPAgMBAAECgYBammGb1alndta" //
|
||||
+ "xBmTtLLdveoBmp14p04D8mhkiC33iFKBcLUvvxGg2Vpuc+cbagyu/NZG+R/WDrlgEDUp6861M5BeFN0L9O4hz"//
|
||||
+ "GAEn8xyTE96f8sh4VlRmBOvVdwZqRO+ilkOM96+KL88A9RKdp8V2tna7TM6oI3LHDyf/JBoXaQJBAMcVN7fKlYP" //
|
||||
+ "Skzfh/yZzW2fmC0ZNg/qaW8Oa/wfDxlWjgnS0p/EKWZ8BxjR/d199L3i/KMaGdfpaWbYZLvYENqUCQQCobjsuCW"//
|
||||
+ "nlZhcWajjzpsSuy8/bICVEpUax1fUZ58Mq69CQXfaZemD9Ar4omzuEAAs2/uee3kt3AvCBaeq05NyjAkBme8SwB0iK"//
|
||||
+ "kLcaeGuJlq7CQIkjSrobIqUEf+CzVZPe+AorG+isS+Cw2w/2bHu+G0p5xSYvdH59P0+ZT0N+f9LFAkA6v3Ae56OrI"//
|
||||
+ "wfMhrJksfeKbIaMjNLS9b8JynIaXg9iCiyOHmgkMl5gAbPoH/ULXqSKwzBw5mJ2GW1gBlyaSfV3AkA/RJC+adIjsRGg"//
|
||||
+ "JOkiRjSmPpGv3FOhl9fsBPjupZBEIuoMWOC8GXK/73DHxwmfNmN7C9+sIi4RBcjEeQ5F5FHZ";
|
||||
|
||||
RSA rsa = new RSA(PRIVATE_KEY, null);
|
||||
|
||||
String a = "2707F9FD4288CEF302C972058712F24A5F3EC62C5A14AD2FC59DAB93503AA0FA17113A020EE4EA35EB53F" //
|
||||
+ "75F36564BA1DABAA20F3B90FD39315C30E68FE8A1803B36C29029B23EB612C06ACF3A34BE815074F5EB5AA3A"//
|
||||
+ "C0C8832EC42DA725B4E1C38EF4EA1B85904F8B10B2D62EA782B813229F9090E6F7394E42E6F44494BB8";
|
||||
|
||||
byte[] aByte = HexUtil.decodeHex(a);
|
||||
byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
|
||||
|
||||
Assert.assertEquals("虎头闯杭州,多抬头看天,切勿只管种地", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rsaTest2() throws Exception {
|
||||
String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6s" +
|
||||
"XqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9S" +
|
||||
"dB1Ua44oncaTWz7OBGLbCiK45wIDAQAB";
|
||||
|
||||
byte[] keyBytes = Base64.decode(publicKeyStr);
|
||||
PublicKey publicKey = KeyUtil.generateRSAPublicKey(keyBytes);
|
||||
|
||||
byte[] data = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).getBytes();
|
||||
//长度不满足128补0
|
||||
byte[] finalData = ArrayUtil.resize(data, 128);
|
||||
|
||||
//jdk原生加密
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
String result1 = HexUtil.encodeHexStr(cipher.doFinal(finalData));
|
||||
|
||||
//hutool加密
|
||||
RSA rsa = new RSA("RSA/ECB/NoPadding", null, publicKeyStr);
|
||||
rsa.setEncryptBlockSize(128);
|
||||
String result2 = rsa.encryptHex(finalData, KeyType.PublicKey);
|
||||
|
||||
Assert.assertEquals(result1, result2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exponentTest(){
|
||||
String modulus = "BD99BAAB9E56B7FD85FB8BCF53CAD2913C1ACEF9063E7C913CD6FC4FEE040DA44D8" +
|
||||
"ADAA35A9DCABD6E936C402D47278049638407135BAB22BB091396CB6873195C8AC8B0B7AB123" +
|
||||
"C3BF7A6341A4419BDBC0EFB85DBCD9A3AD12C99E2265BDCC1197913749E2AFA568EB7623DA3A" +
|
||||
"361335AA1F9FFA6E1801DDC8228AA86306B87";
|
||||
String publicExponent = "65537";
|
||||
RSA rsa = new RSA(new BigInteger(modulus, 16), null, new BigInteger(publicExponent));
|
||||
|
||||
final String encryptBase64 = rsa.encryptBase64("测试内容", KeyType.PublicKey);
|
||||
Assert.assertNotNull(encryptBase64);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,246 +1,246 @@
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.ECKeyUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.SM2;
|
||||
import org.bouncycastle.crypto.engines.SM2Engine;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
/**
|
||||
* SM2算法单元测试
|
||||
*
|
||||
* @author Looly, Gsealy
|
||||
*/
|
||||
public class SM2Test {
|
||||
|
||||
@Test
|
||||
public void generateKeyPairTest() {
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
Console.log(HexUtil.encodeHexStr(pair.getPublic().getEncoded()));
|
||||
Assert.assertNotNull(pair.getPrivate());
|
||||
Assert.assertNotNull(pair.getPublic());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void KeyPairOIDTest() {
|
||||
// OBJECT IDENTIFIER 1.2.156.10197.1.301
|
||||
String OID = "06082A811CCF5501822D";
|
||||
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();
|
||||
|
||||
SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
|
||||
sm2.setMode(SM2Engine.Mode.C1C2C3);
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2Test() {
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(sm2.getPrivateKey());
|
||||
Assert.assertNotNull(sm2.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(sm2.getPublicKey());
|
||||
Assert.assertNotNull(sm2.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2BcdTest() {
|
||||
String text = "我是一段测试aaaa";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2Base64Test() {
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
SM2 sm2 = new SM2();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = sm2.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
|
||||
// 测试自定义密钥后是否生效
|
||||
PrivateKey privateKey = sm2.getPrivateKey();
|
||||
PublicKey publicKey = sm2.getPublicKey();
|
||||
|
||||
sm2 = SmUtil.sm2();
|
||||
sm2.setPrivateKey(privateKey);
|
||||
sm2.setPublicKey(publicKey);
|
||||
String decryptStr2 = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
byte[] sign = sm2.sign(content.getBytes());
|
||||
boolean verify = sm2.verify(content.getBytes(), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyHexTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
String sign = sm2.signHex(HexUtil.encodeHexStr(content));
|
||||
boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyUseKeyTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
|
||||
final SM2 sm2 = new SM2(pair.getPrivate(), pair.getPublic());
|
||||
|
||||
byte[] sign = sm2.sign(content.getBytes());
|
||||
boolean verify = sm2.verify(content.getBytes(), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyUseKeyTest2() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
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());
|
||||
boolean verify = sm2.verify(content.getBytes(), 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);
|
||||
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";
|
||||
|
||||
String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
String id = "31323334353637383132333435363738";
|
||||
|
||||
final SM2 sm2 = new SM2(d, x, y);
|
||||
final String sign = sm2.signHex(data, id);
|
||||
Assert.assertTrue(sm2.verifyHex(data, sign));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2PlainWithPointTest() {
|
||||
// 测试地址:https://i.goto327.top/CryptTools/SM2.aspx?tdsourcetag=s_pctim_aiomsg
|
||||
|
||||
String d = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
|
||||
String x = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
|
||||
String y = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
|
||||
|
||||
String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
String id = "31323334353637383132333435363738";
|
||||
|
||||
final SM2 sm2 = new SM2(d, x, y);
|
||||
// 生成的签名是64位
|
||||
sm2.usePlainEncoding();
|
||||
|
||||
String sign = "DCA0E80A7F46C93714B51C3EFC55A922BCEF7ECF0FE9E62B53BA6A7438B543A76C145A452CA9036F3CB70D7E6C67D4D9D7FE114E5367A2F6F5A4D39F2B10F3D6";
|
||||
Assert.assertTrue(sm2.verifyHex(data, sign));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2PlainWithPointTest2() {
|
||||
String d = "4BD9A450D7E68A5D7E08EB7A0BFA468FD3EB32B71126246E66249A73A9E4D44A";
|
||||
String q = "04970AB36C3B870FBC04041087DB1BC36FB4C6E125B5EA406DB0EC3E2F80F0A55D8AFF28357A0BB215ADC2928BE76F1AFF869BF4C0A3852A78F3B827812C650AD3";
|
||||
|
||||
String data = "123456";
|
||||
|
||||
final ECPublicKeyParameters ecPublicKeyParameters = ECKeyUtil.toSm2PublicParams(q);
|
||||
final ECPrivateKeyParameters ecPrivateKeyParameters = ECKeyUtil.toSm2PrivateParams(d);
|
||||
|
||||
final SM2 sm2 = new SM2(ecPrivateKeyParameters, ecPublicKeyParameters);
|
||||
sm2.setMode(SM2Engine.Mode.C1C2C3);
|
||||
final String encryptHex = sm2.encryptHex(data, KeyType.PublicKey);
|
||||
Console.log(encryptHex);
|
||||
final String decryptStr = sm2.decryptStr(encryptHex, KeyType.PrivateKey);
|
||||
|
||||
Assert.assertEquals(data, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptAndSignTest(){
|
||||
SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
String src = "Sm2Test";
|
||||
byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
byte[] sign = sm2.sign(src.getBytes());
|
||||
|
||||
Assert.assertTrue(sm2.verify( src.getBytes(), sign));
|
||||
|
||||
byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
Assert.assertArrayEquals(dec, src.getBytes());
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.ECKeyUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.SM2;
|
||||
import org.bouncycastle.crypto.engines.SM2Engine;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
/**
|
||||
* SM2算法单元测试
|
||||
*
|
||||
* @author Looly, Gsealy
|
||||
*/
|
||||
public class SM2Test {
|
||||
|
||||
@Test
|
||||
public void generateKeyPairTest() {
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
Console.log(HexUtil.encodeHexStr(pair.getPublic().getEncoded()));
|
||||
Assert.assertNotNull(pair.getPrivate());
|
||||
Assert.assertNotNull(pair.getPublic());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void KeyPairOIDTest() {
|
||||
// OBJECT IDENTIFIER 1.2.156.10197.1.301
|
||||
String OID = "06082A811CCF5501822D";
|
||||
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();
|
||||
|
||||
SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
|
||||
sm2.setMode(SM2Engine.Mode.C1C2C3);
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2Test() {
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
// 获取私钥和公钥
|
||||
Assert.assertNotNull(sm2.getPrivateKey());
|
||||
Assert.assertNotNull(sm2.getPrivateKeyBase64());
|
||||
Assert.assertNotNull(sm2.getPublicKey());
|
||||
Assert.assertNotNull(sm2.getPrivateKeyBase64());
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
byte[] encrypt = sm2.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
byte[] decrypt = sm2.decrypt(encrypt, KeyType.PrivateKey);
|
||||
Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2BcdTest() {
|
||||
String text = "我是一段测试aaaa";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2Base64Test() {
|
||||
String textBase = "我是一段特别长的测试";
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
text.append(textBase);
|
||||
}
|
||||
|
||||
SM2 sm2 = new SM2();
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = sm2.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
|
||||
// 测试自定义密钥后是否生效
|
||||
PrivateKey privateKey = sm2.getPrivateKey();
|
||||
PublicKey publicKey = sm2.getPublicKey();
|
||||
|
||||
sm2 = SmUtil.sm2();
|
||||
sm2.setPrivateKey(privateKey);
|
||||
sm2.setPublicKey(publicKey);
|
||||
String decryptStr2 = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
byte[] sign = sm2.sign(content.getBytes());
|
||||
boolean verify = sm2.verify(content.getBytes(), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyHexTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
String sign = sm2.signHex(HexUtil.encodeHexStr(content));
|
||||
boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyUseKeyTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
||||
|
||||
final SM2 sm2 = new SM2(pair.getPrivate(), pair.getPublic());
|
||||
|
||||
byte[] sign = sm2.sign(content.getBytes());
|
||||
boolean verify = sm2.verify(content.getBytes(), sign);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2SignAndVerifyUseKeyTest2() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
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());
|
||||
boolean verify = sm2.verify(content.getBytes(), 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);
|
||||
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";
|
||||
|
||||
String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
String id = "31323334353637383132333435363738";
|
||||
|
||||
final SM2 sm2 = new SM2(d, x, y);
|
||||
final String sign = sm2.signHex(data, id);
|
||||
Assert.assertTrue(sm2.verifyHex(data, sign));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2PlainWithPointTest() {
|
||||
// 测试地址:https://i.goto327.top/CryptTools/SM2.aspx?tdsourcetag=s_pctim_aiomsg
|
||||
|
||||
String d = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
|
||||
String x = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
|
||||
String y = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
|
||||
|
||||
String data = "434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
|
||||
String id = "31323334353637383132333435363738";
|
||||
|
||||
final SM2 sm2 = new SM2(d, x, y);
|
||||
// 生成的签名是64位
|
||||
sm2.usePlainEncoding();
|
||||
|
||||
String sign = "DCA0E80A7F46C93714B51C3EFC55A922BCEF7ECF0FE9E62B53BA6A7438B543A76C145A452CA9036F3CB70D7E6C67D4D9D7FE114E5367A2F6F5A4D39F2B10F3D6";
|
||||
Assert.assertTrue(sm2.verifyHex(data, sign));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm2PlainWithPointTest2() {
|
||||
String d = "4BD9A450D7E68A5D7E08EB7A0BFA468FD3EB32B71126246E66249A73A9E4D44A";
|
||||
String q = "04970AB36C3B870FBC04041087DB1BC36FB4C6E125B5EA406DB0EC3E2F80F0A55D8AFF28357A0BB215ADC2928BE76F1AFF869BF4C0A3852A78F3B827812C650AD3";
|
||||
|
||||
String data = "123456";
|
||||
|
||||
final ECPublicKeyParameters ecPublicKeyParameters = ECKeyUtil.toSm2PublicParams(q);
|
||||
final ECPrivateKeyParameters ecPrivateKeyParameters = ECKeyUtil.toSm2PrivateParams(d);
|
||||
|
||||
final SM2 sm2 = new SM2(ecPrivateKeyParameters, ecPublicKeyParameters);
|
||||
sm2.setMode(SM2Engine.Mode.C1C2C3);
|
||||
final String encryptHex = sm2.encryptHex(data, KeyType.PublicKey);
|
||||
Console.log(encryptHex);
|
||||
final String decryptStr = sm2.decryptStr(encryptHex, KeyType.PrivateKey);
|
||||
|
||||
Assert.assertEquals(data, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptAndSignTest(){
|
||||
SM2 sm2 = SmUtil.sm2();
|
||||
|
||||
String src = "Sm2Test";
|
||||
byte[] data = sm2.encrypt(src, KeyType.PublicKey);
|
||||
byte[] sign = sm2.sign(src.getBytes());
|
||||
|
||||
Assert.assertTrue(sm2.verify( src.getBytes(), sign));
|
||||
|
||||
byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
Assert.assertArrayEquals(dec, src.getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,108 +1,108 @@
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.Sign;
|
||||
import cn.hutool.crypto.asymmetric.SignAlgorithm;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 签名单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SignTest {
|
||||
|
||||
@Test
|
||||
public void signAndVerifyUseKeyTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
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());
|
||||
|
||||
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeHxvLydLc7u7Y1zEiYcjY5ROQexpEyjChEJUei2NyISITBVY7gOlvg6h9VTMZfYdxiDOMgihtlUWRGBD2s4FcWuf4nzvFtKX5q81gj63XVKuLdEpc+btpSyspi6T4ws26B6A2/FZRftRzsYykHJAF6vau1O3VeVcTsyQ7mV5c9wIDAQAB";
|
||||
sign = SecureUtil.sign(SignAlgorithm.SHA1withRSA, null, publicKey);
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(content.getBytes(), signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signAndVerifyTest() {
|
||||
signAndVerify(SignAlgorithm.NONEwithRSA);
|
||||
signAndVerify(SignAlgorithm.MD2withRSA);
|
||||
signAndVerify(SignAlgorithm.MD5withRSA);
|
||||
|
||||
signAndVerify(SignAlgorithm.SHA1withRSA);
|
||||
signAndVerify(SignAlgorithm.SHA256withRSA);
|
||||
signAndVerify(SignAlgorithm.SHA384withRSA);
|
||||
signAndVerify(SignAlgorithm.SHA512withRSA);
|
||||
|
||||
signAndVerify(SignAlgorithm.NONEwithDSA);
|
||||
signAndVerify(SignAlgorithm.SHA1withDSA);
|
||||
|
||||
signAndVerify(SignAlgorithm.NONEwithECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA1withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA1withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA256withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA384withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA512withECDSA);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试各种算法的签名和验证签名
|
||||
*
|
||||
* @param signAlgorithm 算法
|
||||
*/
|
||||
private void signAndVerify(SignAlgorithm signAlgorithm) {
|
||||
byte[] data = StrUtil.utf8Bytes("我是一段测试ab");
|
||||
Sign sign = SecureUtil.sign(signAlgorithm);
|
||||
|
||||
// 签名
|
||||
byte[] signed = sign.sign(data);
|
||||
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(data, signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试MD5withRSA算法的签名和验证签名
|
||||
*/
|
||||
@Test
|
||||
public void signAndVerify2() {
|
||||
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);
|
||||
|
||||
// 签名
|
||||
byte[] signed = sign.sign(data);
|
||||
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(data, signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signParamsTest(){
|
||||
Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||
.put("key1", "value1")
|
||||
.put("key2", "value2").build();
|
||||
|
||||
String sign1 = SecureUtil.signParamsSha1(build);
|
||||
Assert.assertEquals("9ed30bfe2efbc7038a824b6c55c24a11bfc0dce5", sign1);
|
||||
String sign2 = SecureUtil.signParamsSha1(build, "12345678");
|
||||
Assert.assertEquals("944b68d94c952ec178c4caf16b9416b6661f7720", sign2);
|
||||
String sign3 = SecureUtil.signParamsSha1(build, "12345678", "abc");
|
||||
Assert.assertEquals("edee1b477af1b96ebd20fdf08d818f352928d25d", sign3);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.asymmetric;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.Sign;
|
||||
import cn.hutool.crypto.asymmetric.SignAlgorithm;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 签名单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SignTest {
|
||||
|
||||
@Test
|
||||
public void signAndVerifyUseKeyTest() {
|
||||
String content = "我是Hanley.";
|
||||
|
||||
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());
|
||||
|
||||
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeHxvLydLc7u7Y1zEiYcjY5ROQexpEyjChEJUei2NyISITBVY7gOlvg6h9VTMZfYdxiDOMgihtlUWRGBD2s4FcWuf4nzvFtKX5q81gj63XVKuLdEpc+btpSyspi6T4ws26B6A2/FZRftRzsYykHJAF6vau1O3VeVcTsyQ7mV5c9wIDAQAB";
|
||||
sign = SecureUtil.sign(SignAlgorithm.SHA1withRSA, null, publicKey);
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(content.getBytes(), signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signAndVerifyTest() {
|
||||
signAndVerify(SignAlgorithm.NONEwithRSA);
|
||||
signAndVerify(SignAlgorithm.MD2withRSA);
|
||||
signAndVerify(SignAlgorithm.MD5withRSA);
|
||||
|
||||
signAndVerify(SignAlgorithm.SHA1withRSA);
|
||||
signAndVerify(SignAlgorithm.SHA256withRSA);
|
||||
signAndVerify(SignAlgorithm.SHA384withRSA);
|
||||
signAndVerify(SignAlgorithm.SHA512withRSA);
|
||||
|
||||
signAndVerify(SignAlgorithm.NONEwithDSA);
|
||||
signAndVerify(SignAlgorithm.SHA1withDSA);
|
||||
|
||||
signAndVerify(SignAlgorithm.NONEwithECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA1withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA1withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA256withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA384withECDSA);
|
||||
signAndVerify(SignAlgorithm.SHA512withECDSA);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试各种算法的签名和验证签名
|
||||
*
|
||||
* @param signAlgorithm 算法
|
||||
*/
|
||||
private void signAndVerify(SignAlgorithm signAlgorithm) {
|
||||
byte[] data = StrUtil.utf8Bytes("我是一段测试ab");
|
||||
Sign sign = SecureUtil.sign(signAlgorithm);
|
||||
|
||||
// 签名
|
||||
byte[] signed = sign.sign(data);
|
||||
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(data, signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试MD5withRSA算法的签名和验证签名
|
||||
*/
|
||||
@Test
|
||||
public void signAndVerify2() {
|
||||
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);
|
||||
|
||||
// 签名
|
||||
byte[] signed = sign.sign(data);
|
||||
|
||||
// 验证签名
|
||||
boolean verify = sign.verify(data, signed);
|
||||
Assert.assertTrue(verify);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signParamsTest(){
|
||||
Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||
.put("key1", "value1")
|
||||
.put("key2", "value2").build();
|
||||
|
||||
String sign1 = SecureUtil.signParamsSha1(build);
|
||||
Assert.assertEquals("9ed30bfe2efbc7038a824b6c55c24a11bfc0dce5", sign1);
|
||||
String sign2 = SecureUtil.signParamsSha1(build, "12345678");
|
||||
Assert.assertEquals("944b68d94c952ec178c4caf16b9416b6661f7720", sign2);
|
||||
String sign3 = SecureUtil.signParamsSha1(build, "12345678", "abc");
|
||||
Assert.assertEquals("edee1b477af1b96ebd20fdf08d818f352928d25d", sign3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
package cn.hutool.crypto.test.digest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.digest.DigestAlgorithm;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import cn.hutool.crypto.digest.Digester;
|
||||
|
||||
/**
|
||||
* 摘要算法单元测试
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class DigestTest {
|
||||
|
||||
@Test
|
||||
public void digesterTest(){
|
||||
String testStr = "test中文";
|
||||
|
||||
Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
String digestHex = md5.digestHex(testStr);
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", digestHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void md5Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
String md5Hex1 = DigestUtil.md5Hex(testStr);
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", md5Hex1);
|
||||
|
||||
String md5Hex2 = DigestUtil.md5Hex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", md5Hex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void md5WithSaltTest(){
|
||||
String testStr = "test中文";
|
||||
|
||||
Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
|
||||
//加盐
|
||||
md5.setSalt("saltTest".getBytes());
|
||||
String md5Hex1 = md5.digestHex(testStr);
|
||||
Assert.assertEquals("762f7335200299dfa09bebbb601a5bc6", md5Hex1);
|
||||
String md5Hex2 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
Assert.assertEquals("762f7335200299dfa09bebbb601a5bc6", md5Hex2);
|
||||
|
||||
//重复2次
|
||||
md5.setDigestCount(2);
|
||||
String md5Hex3 = md5.digestHex(testStr);
|
||||
Assert.assertEquals("2b0616296f6755d25efc07f90afe9684", md5Hex3);
|
||||
String md5Hex4 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
Assert.assertEquals("2b0616296f6755d25efc07f90afe9684", md5Hex4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sha1Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
String sha1Hex1 = DigestUtil.sha1Hex(testStr);
|
||||
Assert.assertEquals("ecabf586cef0d3b11c56549433ad50b81110a836", sha1Hex1);
|
||||
|
||||
String sha1Hex2 = DigestUtil.sha1Hex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("ecabf586cef0d3b11c56549433ad50b81110a836", sha1Hex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hash256Test() {
|
||||
String testStr = "Test中文";
|
||||
String hex = DigestUtil.sha256Hex(testStr);
|
||||
Assert.assertEquals(64, hex.length());
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.digest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.digest.DigestAlgorithm;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import cn.hutool.crypto.digest.Digester;
|
||||
|
||||
/**
|
||||
* 摘要算法单元测试
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class DigestTest {
|
||||
|
||||
@Test
|
||||
public void digesterTest(){
|
||||
String testStr = "test中文";
|
||||
|
||||
Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
String digestHex = md5.digestHex(testStr);
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", digestHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void md5Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
String md5Hex1 = DigestUtil.md5Hex(testStr);
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", md5Hex1);
|
||||
|
||||
String md5Hex2 = DigestUtil.md5Hex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("5393554e94bf0eb6436f240a4fd71282", md5Hex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void md5WithSaltTest(){
|
||||
String testStr = "test中文";
|
||||
|
||||
Digester md5 = new Digester(DigestAlgorithm.MD5);
|
||||
|
||||
//加盐
|
||||
md5.setSalt("saltTest".getBytes());
|
||||
String md5Hex1 = md5.digestHex(testStr);
|
||||
Assert.assertEquals("762f7335200299dfa09bebbb601a5bc6", md5Hex1);
|
||||
String md5Hex2 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
Assert.assertEquals("762f7335200299dfa09bebbb601a5bc6", md5Hex2);
|
||||
|
||||
//重复2次
|
||||
md5.setDigestCount(2);
|
||||
String md5Hex3 = md5.digestHex(testStr);
|
||||
Assert.assertEquals("2b0616296f6755d25efc07f90afe9684", md5Hex3);
|
||||
String md5Hex4 = md5.digestHex(IoUtil.toUtf8Stream(testStr));
|
||||
Assert.assertEquals("2b0616296f6755d25efc07f90afe9684", md5Hex4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sha1Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
String sha1Hex1 = DigestUtil.sha1Hex(testStr);
|
||||
Assert.assertEquals("ecabf586cef0d3b11c56549433ad50b81110a836", sha1Hex1);
|
||||
|
||||
String sha1Hex2 = DigestUtil.sha1Hex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("ecabf586cef0d3b11c56549433ad50b81110a836", sha1Hex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hash256Test() {
|
||||
String testStr = "Test中文";
|
||||
String hex = DigestUtil.sha256Hex(testStr);
|
||||
Assert.assertEquals(64, hex.length());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,58 @@
|
||||
package cn.hutool.crypto.test.digest;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
import cn.hutool.crypto.digest.HmacAlgorithm;
|
||||
|
||||
/**
|
||||
* Hmac单元测试
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class HmacTest {
|
||||
|
||||
@Test
|
||||
public void hmacTest(){
|
||||
String testStr = "test中文";
|
||||
|
||||
byte[] key = "password".getBytes();
|
||||
HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacMd5Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
HMac mac = SecureUtil.hmacMd5("password");
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacSha1Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
HMac mac = SecureUtil.hmacSha1("password");
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("1dd68d2f119d5640f0d416e99d3f42408b88d511", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("1dd68d2f119d5640f0d416e99d3f42408b88d511", macHex2);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.digest;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
import cn.hutool.crypto.digest.HmacAlgorithm;
|
||||
|
||||
/**
|
||||
* Hmac单元测试
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class HmacTest {
|
||||
|
||||
@Test
|
||||
public void hmacTest(){
|
||||
String testStr = "test中文";
|
||||
|
||||
byte[] key = "password".getBytes();
|
||||
HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacMd5Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
HMac mac = SecureUtil.hmacMd5("password");
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("b977f4b13f93f549e06140971bded384", macHex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hmacSha1Test(){
|
||||
String testStr = "test中文";
|
||||
|
||||
HMac mac = SecureUtil.hmacSha1("password");
|
||||
|
||||
String macHex1 = mac.digestHex(testStr);
|
||||
Assert.assertEquals("1dd68d2f119d5640f0d416e99d3f42408b88d511", macHex1);
|
||||
|
||||
String macHex2 = mac.digestHex(IoUtil.toStream(testStr, CharsetUtil.CHARSET_UTF_8));
|
||||
Assert.assertEquals("1dd68d2f119d5640f0d416e99d3f42408b88d511", macHex2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package cn.hutool.crypto.test.digest;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.crypto.digest.MD5;
|
||||
|
||||
/**
|
||||
* MD5 单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class Md5Test {
|
||||
|
||||
@Test
|
||||
public void md5To16Test() {
|
||||
String hex16 = new MD5().digestHex16("中国");
|
||||
Assert.assertEquals(16, hex16.length());
|
||||
Assert.assertEquals("cb143acd6c929826", hex16);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.digest;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.crypto.digest.MD5;
|
||||
|
||||
/**
|
||||
* MD5 单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class Md5Test {
|
||||
|
||||
@Test
|
||||
public void md5To16Test() {
|
||||
String hex16 = new MD5().digestHex16("中国");
|
||||
Assert.assertEquals(16, hex16.length());
|
||||
Assert.assertEquals("cb143acd6c929826", hex16);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,115 +1,115 @@
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class AESTest {
|
||||
|
||||
@Test
|
||||
public void encryptTest() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding,
|
||||
"1234567890123456".getBytes(), "1234567890123456".getBytes());
|
||||
String encryptHex = aes.encryptHex("123456");
|
||||
Assert.assertEquals("d637735ae9e21ba50cb686b74fab8d2c", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptTest2() {
|
||||
String content = "test中文";
|
||||
AES aes = new AES(Mode.CTS, Padding.PKCS5Padding,
|
||||
"0CoJUm6Qyw8W8jue".getBytes(), "0102030405060708".getBytes());
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
Assert.assertEquals("8dc9de7f050e86ca2c8261dde56dfec9", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptPKCS7Test() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.CBC.name(), "pkcs7padding",
|
||||
"1234567890123456".getBytes(), "1234567890123456".getBytes());
|
||||
String encryptHex = aes.encryptHex("123456");
|
||||
Assert.assertEquals("d637735ae9e21ba50cb686b74fab8d2c", encryptHex);
|
||||
}
|
||||
|
||||
/**
|
||||
* AES加密/解密
|
||||
* 加解密模式:ECB模式 数据填充模式:PKCS7
|
||||
* <p>
|
||||
* 数据:16c5
|
||||
* 密钥: 0102030405060708090a0b0c0d0e0f10
|
||||
* 数据格式:hex格式 加解密模式:ECB模式 数据填充模式:PKCS7
|
||||
* 结果: 25869eb3ff227d9e34b3512d3c3c92ed 【加密后的Hex】
|
||||
* 结果: JYaes/8ifZ40s1EtPDyS7Q== 【加密后的Base64】
|
||||
* <p>
|
||||
* 数据:16c5
|
||||
* 密钥: 0102030405060708090a0b0c0d0e0f10
|
||||
* 数据格式:UTF-8格式 加解密模式:ECB模式 数据填充模式:PKCS7
|
||||
* 结果: 79c210d3e304932cf9ea6a9c887c6d7c 【加密后的Hex】
|
||||
* 结果: ecIQ0+MEkyz56mqciHxtfA== 【加密后的Base64】
|
||||
* <p>
|
||||
* AES在线解密 AES在线加密 Aes online hex 十六进制密钥 - The X 在线工具
|
||||
* https://the-x.cn/cryptography/Aes.aspx
|
||||
*/
|
||||
@Test
|
||||
public void encryptPKCS7Test2() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.ECB.name(), "pkcs7padding",
|
||||
HexUtil.decodeHex("0102030405060708090a0b0c0d0e0f10"));
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 加密数据为16进制字符串
|
||||
String encryptHex = aes.encryptHex(HexUtil.decodeHex("16c5"));
|
||||
// 加密后的Hex
|
||||
Assert.assertEquals("25869eb3ff227d9e34b3512d3c3c92ed", encryptHex);
|
||||
|
||||
// 加密数据为16进制字符串
|
||||
String encryptHex2 = aes.encryptBase64(HexUtil.decodeHex("16c5"));
|
||||
// 加密后的Base64
|
||||
Assert.assertEquals("JYaes/8ifZ40s1EtPDyS7Q==", encryptHex2);
|
||||
|
||||
// 解密
|
||||
Assert.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt("25869eb3ff227d9e34b3512d3c3c92ed")));
|
||||
Assert.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt(HexUtil.encodeHexStr(Base64.decode("JYaes/8ifZ40s1EtPDyS7Q==")))));
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 加密数据为字符串(UTF-8)
|
||||
String encryptStr = aes.encryptHex("16c5");
|
||||
// 加密后的Hex
|
||||
Assert.assertEquals("79c210d3e304932cf9ea6a9c887c6d7c", encryptStr);
|
||||
|
||||
// 加密数据为字符串(UTF-8)
|
||||
String encryptStr2 = aes.encryptBase64("16c5");
|
||||
// 加密后的Base64
|
||||
Assert.assertEquals("ecIQ0+MEkyz56mqciHxtfA==", encryptStr2);
|
||||
|
||||
// 解密
|
||||
Assert.assertEquals("16c5", aes.decryptStr("79c210d3e304932cf9ea6a9c887c6d7c"));
|
||||
Assert.assertEquals("16c5", aes.decryptStr(Base64.decode("ecIQ0+MEkyz56mqciHxtfA==")));
|
||||
// ------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesWithSha1PrngTest() {
|
||||
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 result1 = aes.encryptBase64(content);
|
||||
|
||||
final String decryptStr = aes.decryptStr(result1);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class AESTest {
|
||||
|
||||
@Test
|
||||
public void encryptTest() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding,
|
||||
"1234567890123456".getBytes(), "1234567890123456".getBytes());
|
||||
String encryptHex = aes.encryptHex("123456");
|
||||
Assert.assertEquals("d637735ae9e21ba50cb686b74fab8d2c", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptTest2() {
|
||||
String content = "test中文";
|
||||
AES aes = new AES(Mode.CTS, Padding.PKCS5Padding,
|
||||
"0CoJUm6Qyw8W8jue".getBytes(), "0102030405060708".getBytes());
|
||||
final String encryptHex = aes.encryptHex(content);
|
||||
Assert.assertEquals("8dc9de7f050e86ca2c8261dde56dfec9", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptPKCS7Test() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.CBC.name(), "pkcs7padding",
|
||||
"1234567890123456".getBytes(), "1234567890123456".getBytes());
|
||||
String encryptHex = aes.encryptHex("123456");
|
||||
Assert.assertEquals("d637735ae9e21ba50cb686b74fab8d2c", encryptHex);
|
||||
}
|
||||
|
||||
/**
|
||||
* AES加密/解密
|
||||
* 加解密模式:ECB模式 数据填充模式:PKCS7
|
||||
* <p>
|
||||
* 数据:16c5
|
||||
* 密钥: 0102030405060708090a0b0c0d0e0f10
|
||||
* 数据格式:hex格式 加解密模式:ECB模式 数据填充模式:PKCS7
|
||||
* 结果: 25869eb3ff227d9e34b3512d3c3c92ed 【加密后的Hex】
|
||||
* 结果: JYaes/8ifZ40s1EtPDyS7Q== 【加密后的Base64】
|
||||
* <p>
|
||||
* 数据:16c5
|
||||
* 密钥: 0102030405060708090a0b0c0d0e0f10
|
||||
* 数据格式:UTF-8格式 加解密模式:ECB模式 数据填充模式:PKCS7
|
||||
* 结果: 79c210d3e304932cf9ea6a9c887c6d7c 【加密后的Hex】
|
||||
* 结果: ecIQ0+MEkyz56mqciHxtfA== 【加密后的Base64】
|
||||
* <p>
|
||||
* AES在线解密 AES在线加密 Aes online hex 十六进制密钥 - The X 在线工具
|
||||
* https://the-x.cn/cryptography/Aes.aspx
|
||||
*/
|
||||
@Test
|
||||
public void encryptPKCS7Test2() {
|
||||
// 构建
|
||||
AES aes = new AES(Mode.ECB.name(), "pkcs7padding",
|
||||
HexUtil.decodeHex("0102030405060708090a0b0c0d0e0f10"));
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 加密数据为16进制字符串
|
||||
String encryptHex = aes.encryptHex(HexUtil.decodeHex("16c5"));
|
||||
// 加密后的Hex
|
||||
Assert.assertEquals("25869eb3ff227d9e34b3512d3c3c92ed", encryptHex);
|
||||
|
||||
// 加密数据为16进制字符串
|
||||
String encryptHex2 = aes.encryptBase64(HexUtil.decodeHex("16c5"));
|
||||
// 加密后的Base64
|
||||
Assert.assertEquals("JYaes/8ifZ40s1EtPDyS7Q==", encryptHex2);
|
||||
|
||||
// 解密
|
||||
Assert.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt("25869eb3ff227d9e34b3512d3c3c92ed")));
|
||||
Assert.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt(HexUtil.encodeHexStr(Base64.decode("JYaes/8ifZ40s1EtPDyS7Q==")))));
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// 加密数据为字符串(UTF-8)
|
||||
String encryptStr = aes.encryptHex("16c5");
|
||||
// 加密后的Hex
|
||||
Assert.assertEquals("79c210d3e304932cf9ea6a9c887c6d7c", encryptStr);
|
||||
|
||||
// 加密数据为字符串(UTF-8)
|
||||
String encryptStr2 = aes.encryptBase64("16c5");
|
||||
// 加密后的Base64
|
||||
Assert.assertEquals("ecIQ0+MEkyz56mqciHxtfA==", encryptStr2);
|
||||
|
||||
// 解密
|
||||
Assert.assertEquals("16c5", aes.decryptStr("79c210d3e304932cf9ea6a9c887c6d7c"));
|
||||
Assert.assertEquals("16c5", aes.decryptStr(Base64.decode("ecIQ0+MEkyz56mqciHxtfA==")));
|
||||
// ------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesWithSha1PrngTest() {
|
||||
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 result1 = aes.encryptBase64(content);
|
||||
|
||||
final String decryptStr = aes.decryptStr(result1);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,42 @@
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.symmetric.DES;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* DES加密解密单元测试
|
||||
*/
|
||||
public class DesTest {
|
||||
|
||||
@Test
|
||||
public void encryptDecryptTest(){
|
||||
String content = "我是一个测试的test字符串123";
|
||||
final DES des = SecureUtil.des();
|
||||
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String result = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptDecryptWithCustomTest(){
|
||||
String content = "我是一个测试的test字符串123";
|
||||
final DES des = new DES(
|
||||
Mode.CTS,
|
||||
Padding.PKCS5Padding,
|
||||
StrUtil.bytes("12345678"),
|
||||
StrUtil.bytes("11223344")
|
||||
);
|
||||
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String result = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, result);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.symmetric.DES;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* DES加密解密单元测试
|
||||
*/
|
||||
public class DesTest {
|
||||
|
||||
@Test
|
||||
public void encryptDecryptTest(){
|
||||
String content = "我是一个测试的test字符串123";
|
||||
final DES des = SecureUtil.des();
|
||||
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String result = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptDecryptWithCustomTest(){
|
||||
String content = "我是一个测试的test字符串123";
|
||||
final DES des = new DES(
|
||||
Mode.CTS,
|
||||
Padding.PKCS5Padding,
|
||||
StrUtil.bytes("12345678"),
|
||||
StrUtil.bytes("11223344")
|
||||
);
|
||||
|
||||
final String encryptHex = des.encryptHex(content);
|
||||
final String result = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +1,71 @@
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.crypto.symmetric.RC4;
|
||||
|
||||
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);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "Hello, World, this is megssage 2";
|
||||
byte[] crypt2 = rc4.encrypt(message2);
|
||||
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);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第二个中文消息!";
|
||||
byte[] crypt2 = rc4.encrypt(message2);
|
||||
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.CHARSET_UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第二个用来测试密文为十六进制字符串的消息!";
|
||||
String encryptHex2 = rc4.encryptHex(message2);
|
||||
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.CHARSET_UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
String encryptHex2 = rc4.encryptBase64(message2);
|
||||
String msg2 = rc4.decrypt(encryptHex2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.crypto.symmetric.RC4;
|
||||
|
||||
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);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "Hello, World, this is megssage 2";
|
||||
byte[] crypt2 = rc4.encrypt(message2);
|
||||
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);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第二个中文消息!";
|
||||
byte[] crypt2 = rc4.encrypt(message2);
|
||||
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.CHARSET_UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第二个用来测试密文为十六进制字符串的消息!";
|
||||
String encryptHex2 = rc4.encryptHex(message2);
|
||||
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.CHARSET_UTF_8);
|
||||
String msg = rc4.decrypt(encryptHex);
|
||||
Assert.assertEquals(message, msg);
|
||||
|
||||
String message2 = "这是第一个用来测试密文为Base64编码的消息!";
|
||||
String encryptHex2 = rc4.encryptBase64(message2);
|
||||
String msg2 = rc4.decrypt(encryptHex2);
|
||||
Assert.assertEquals(message2, msg2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,217 +1,217 @@
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.symmetric.*;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 对称加密算法单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class SymmetricTest {
|
||||
|
||||
@Test
|
||||
public void aesTest() {
|
||||
String content = "test中文";
|
||||
|
||||
// 随机生成密钥
|
||||
byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
|
||||
// 构建
|
||||
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesTest2() {
|
||||
String content = "test中文";
|
||||
|
||||
// 随机生成密钥
|
||||
byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
|
||||
// 构建
|
||||
AES aes = SecureUtil.aes(key);
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesTest3() {
|
||||
String content = "test中文aaaaaaaaaaaaaaaaaaaaa";
|
||||
|
||||
AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_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());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
|
||||
Assert.assertEquals("cd0e3a249eaf0ed80c330338508898c4bddcfd665a1b414622164a273ca5daf7b4ebd2c00aaa66b84dd0a237708dac8e", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesZeroPaddingTest() {
|
||||
String content = RandomUtil.randomString(RandomUtil.randomInt(200));
|
||||
AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密
|
||||
String decryptStr = aes.decryptStr(encryptHex);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest() {
|
||||
String content = "test中文";
|
||||
|
||||
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);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest2() {
|
||||
String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
|
||||
DES des = SecureUtil.des(key);
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest3() {
|
||||
String content = "test中文";
|
||||
|
||||
DES des = new DES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "01020304".getBytes());
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desdeTest() {
|
||||
String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
DESede des = SecureUtil.desede(key);
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desdeTest2() {
|
||||
String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
DESede des = new DESede(Mode.CBC, Padding.PKCS5Padding, key, "12345678".getBytes());
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vigenereTest() {
|
||||
String content = "Wherethereisawillthereisaway";
|
||||
String key = "CompleteVictory";
|
||||
|
||||
String encrypt = Vigenere.encrypt(content, key);
|
||||
Assert.assertEquals("zXScRZ]KIOMhQjc0\\bYRXZOJK[Vi", encrypt);
|
||||
String decrypt = Vigenere.decrypt(encrypt, key);
|
||||
Assert.assertEquals(content, decrypt);
|
||||
}
|
||||
}
|
||||
package cn.hutool.crypto.test.symmetric;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.symmetric.*;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 对称加密算法单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class SymmetricTest {
|
||||
|
||||
@Test
|
||||
public void aesTest() {
|
||||
String content = "test中文";
|
||||
|
||||
// 随机生成密钥
|
||||
byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
|
||||
// 构建
|
||||
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesTest2() {
|
||||
String content = "test中文";
|
||||
|
||||
// 随机生成密钥
|
||||
byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
|
||||
|
||||
// 构建
|
||||
AES aes = SecureUtil.aes(key);
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesTest3() {
|
||||
String content = "test中文aaaaaaaaaaaaaaaaaaaaa";
|
||||
|
||||
AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());
|
||||
|
||||
// 加密
|
||||
byte[] encrypt = aes.encrypt(content);
|
||||
// 解密
|
||||
byte[] decrypt = aes.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密为字符串
|
||||
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_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());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
|
||||
Assert.assertEquals("cd0e3a249eaf0ed80c330338508898c4bddcfd665a1b414622164a273ca5daf7b4ebd2c00aaa66b84dd0a237708dac8e", encryptHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aesZeroPaddingTest() {
|
||||
String content = RandomUtil.randomString(RandomUtil.randomInt(200));
|
||||
AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
|
||||
|
||||
// 加密为16进制表示
|
||||
String encryptHex = aes.encryptHex(content);
|
||||
// 解密
|
||||
String decryptStr = aes.decryptStr(encryptHex);
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest() {
|
||||
String content = "test中文";
|
||||
|
||||
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);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest2() {
|
||||
String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
|
||||
|
||||
DES des = SecureUtil.des(key);
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desTest3() {
|
||||
String content = "test中文";
|
||||
|
||||
DES des = new DES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "01020304".getBytes());
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desdeTest() {
|
||||
String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
DESede des = SecureUtil.desede(key);
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void desdeTest2() {
|
||||
String content = "test中文";
|
||||
|
||||
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
|
||||
|
||||
DESede des = new DESede(Mode.CBC, Padding.PKCS5Padding, key, "12345678".getBytes());
|
||||
|
||||
byte[] encrypt = des.encrypt(content);
|
||||
byte[] decrypt = des.decrypt(encrypt);
|
||||
|
||||
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
|
||||
|
||||
String encryptHex = des.encryptHex(content);
|
||||
String decryptStr = des.decryptStr(encryptHex);
|
||||
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vigenereTest() {
|
||||
String content = "Wherethereisawillthereisaway";
|
||||
String key = "CompleteVictory";
|
||||
|
||||
String encrypt = Vigenere.encrypt(content, key);
|
||||
Assert.assertEquals("zXScRZ]KIOMhQjc0\\bYRXZOJK[Vi", encrypt);
|
||||
String decrypt = Vigenere.decrypt(encrypt, key);
|
||||
Assert.assertEquals(content, decrypt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXgIBAAKBgQC3G09zGCmlMetvNaWiHbp9d8jItkj5ik0wKcn7jBy/eOdlno5m
|
||||
y+eijTP/KX8D2QNj2vlF+31/AThYoxI80qUZ3imw8vDVc0cBeGLxEDLVweCQEy7C
|
||||
ivpkmEWCeyoqThenrwONoIjajG7ZJggFTrsXHL6HsbkGxYrABG2PmQ/w0QIDAQAB
|
||||
AoGBAIxvTcggSBCC8OciZh6oXlfMfxoxdFavU/QUmO1s0L+pow+1Q9JjoQxy7+ZL
|
||||
lTcGQitbzsN11xKJhQW2TE6J4EVimJZQSAE4DDmYpMOrkjnBQhkUlaZkkukvDSRS
|
||||
JqwBI/04G7se+RouHyXjRS9U76HnPM8+/IS2h+T6CbXLOpYBAkEA2j0JmyGVs+WV
|
||||
I9sG5glamJqTBa4CfTORrdFW4EULoGkUc24ZFFqn9W4e5yfl/pCkPptCenvIrAWp
|
||||
/ymnHeLn6QJBANbKGO9uBizAt4+o+kHYdANcbU/Cs3PLj8yOOtjkuMbH4tPNQmB6
|
||||
/u3npiVk7/Txfkg0BjRzDDZib109eKbvGKkCQBgMneBghRS7+gFng40Z/sfOUOFR
|
||||
WajeY/FZnk88jJlyuvQ1b8IUc2nSZslmViwFWHQlu9+vgF+kiCU8O9RJSvECQQCl
|
||||
Vkx7giYerPqgC2MY7JXhQHSkwSuCJ2A6BgImk2npGlTw1UATJJq4Z2jtwBU2Z+7d
|
||||
ha6BEU6FTqCLFZaaadKBAkEAxko4hrgBsX9BKpFJE3aUIUcMTJfJQdiAhq0k4DV8
|
||||
5GVrcp8zl6mUTPZDaOmDhuAjGdAQJqj0Xo0PZ0fOZPtR+w==
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXgIBAAKBgQC3G09zGCmlMetvNaWiHbp9d8jItkj5ik0wKcn7jBy/eOdlno5m
|
||||
y+eijTP/KX8D2QNj2vlF+31/AThYoxI80qUZ3imw8vDVc0cBeGLxEDLVweCQEy7C
|
||||
ivpkmEWCeyoqThenrwONoIjajG7ZJggFTrsXHL6HsbkGxYrABG2PmQ/w0QIDAQAB
|
||||
AoGBAIxvTcggSBCC8OciZh6oXlfMfxoxdFavU/QUmO1s0L+pow+1Q9JjoQxy7+ZL
|
||||
lTcGQitbzsN11xKJhQW2TE6J4EVimJZQSAE4DDmYpMOrkjnBQhkUlaZkkukvDSRS
|
||||
JqwBI/04G7se+RouHyXjRS9U76HnPM8+/IS2h+T6CbXLOpYBAkEA2j0JmyGVs+WV
|
||||
I9sG5glamJqTBa4CfTORrdFW4EULoGkUc24ZFFqn9W4e5yfl/pCkPptCenvIrAWp
|
||||
/ymnHeLn6QJBANbKGO9uBizAt4+o+kHYdANcbU/Cs3PLj8yOOtjkuMbH4tPNQmB6
|
||||
/u3npiVk7/Txfkg0BjRzDDZib109eKbvGKkCQBgMneBghRS7+gFng40Z/sfOUOFR
|
||||
WajeY/FZnk88jJlyuvQ1b8IUc2nSZslmViwFWHQlu9+vgF+kiCU8O9RJSvECQQCl
|
||||
Vkx7giYerPqgC2MY7JXhQHSkwSuCJ2A6BgImk2npGlTw1UATJJq4Z2jtwBU2Z+7d
|
||||
ha6BEU6FTqCLFZaaadKBAkEAxko4hrgBsX9BKpFJE3aUIUcMTJfJQdiAhq0k4DV8
|
||||
5GVrcp8zl6mUTPZDaOmDhuAjGdAQJqj0Xo0PZ0fOZPtR+w==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -1,15 +1,15 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIICYTCCAcoCCQCs45mePIbzRTANBgkqhkiG9w0BAQUFADB1MQswCQYDVQQGEwJV
|
||||
UzENMAsGA1UECAwETWFyczETMBEGA1UEBwwKaVRyYW5zd2FycDETMBEGA1UECgwK
|
||||
aVRyYW5zd2FycDETMBEGA1UECwwKaVRyYW5zd2FycDEYMBYGA1UEAwwPd3d3LjU5
|
||||
MXdpZmkuY29tMB4XDTE4MTAxNzAyMTA0OFoXDTI4MTAxNDAyMTA0OFowdTELMAkG
|
||||
A1UEBhMCVVMxDTALBgNVBAgMBE1hcnMxEzARBgNVBAcMCmlUcmFuc3dhcnAxEzAR
|
||||
BgNVBAoMCmlUcmFuc3dhcnAxEzARBgNVBAsMCmlUcmFuc3dhcnAxGDAWBgNVBAMM
|
||||
D3d3dy41OTF3aWZpLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtxtP
|
||||
cxgppTHrbzWloh26fXfIyLZI+YpNMCnJ+4wcv3jnZZ6OZsvnoo0z/yl/A9kDY9r5
|
||||
Rft9fwE4WKMSPNKlGd4psPLw1XNHAXhi8RAy1cHgkBMuwor6ZJhFgnsqKk4Xp68D
|
||||
jaCI2oxu2SYIBU67Fxy+h7G5BsWKwARtj5kP8NECAwEAATANBgkqhkiG9w0BAQUF
|
||||
AAOBgQC2Pko8q1NicJ0oPuhFTPm7n03LtPhCaV/aDf3mqtGxraYifg8iFTxVyZ1c
|
||||
ol0eEJFsibrQrPEwdSuSVqzwif5Tab9dV92PPFm+Sq0D1Uc0xI4ziXQ+a55K9wrV
|
||||
TKXxS48TOpnTA8fVFNkUkFNB54Lhh9AwKsx123kJmyaWccbt9Q==
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIICYTCCAcoCCQCs45mePIbzRTANBgkqhkiG9w0BAQUFADB1MQswCQYDVQQGEwJV
|
||||
UzENMAsGA1UECAwETWFyczETMBEGA1UEBwwKaVRyYW5zd2FycDETMBEGA1UECgwK
|
||||
aVRyYW5zd2FycDETMBEGA1UECwwKaVRyYW5zd2FycDEYMBYGA1UEAwwPd3d3LjU5
|
||||
MXdpZmkuY29tMB4XDTE4MTAxNzAyMTA0OFoXDTI4MTAxNDAyMTA0OFowdTELMAkG
|
||||
A1UEBhMCVVMxDTALBgNVBAgMBE1hcnMxEzARBgNVBAcMCmlUcmFuc3dhcnAxEzAR
|
||||
BgNVBAoMCmlUcmFuc3dhcnAxEzARBgNVBAsMCmlUcmFuc3dhcnAxGDAWBgNVBAMM
|
||||
D3d3dy41OTF3aWZpLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtxtP
|
||||
cxgppTHrbzWloh26fXfIyLZI+YpNMCnJ+4wcv3jnZZ6OZsvnoo0z/yl/A9kDY9r5
|
||||
Rft9fwE4WKMSPNKlGd4psPLw1XNHAXhi8RAy1cHgkBMuwor6ZJhFgnsqKk4Xp68D
|
||||
jaCI2oxu2SYIBU67Fxy+h7G5BsWKwARtj5kP8NECAwEAATANBgkqhkiG9w0BAQUF
|
||||
AAOBgQC2Pko8q1NicJ0oPuhFTPm7n03LtPhCaV/aDf3mqtGxraYifg8iFTxVyZ1c
|
||||
ol0eEJFsibrQrPEwdSuSVqzwif5Tab9dV92PPFm+Sq0D1Uc0xI4ziXQ+a55K9wrV
|
||||
TKXxS48TOpnTA8fVFNkUkFNB54Lhh9AwKsx123kJmyaWccbt9Q==
|
||||
-----END CERTIFICATE-----
|
||||
Reference in New Issue
Block a user