mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -31,4 +31,19 @@ public class KeyUtilTest {
|
||||
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,6 +1,7 @@
|
||||
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;
|
||||
@@ -12,6 +13,25 @@ public class ECIESTest {
|
||||
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++) {
|
||||
@@ -19,8 +39,9 @@ public class ECIESTest {
|
||||
}
|
||||
|
||||
// 公钥加密,私钥解密
|
||||
String encryptStr = ecies.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
String decryptStr = StrUtil.utf8Str(ecies.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
String encryptStr = cryptoForEncrypt.encryptBase64(text.toString(), KeyType.PublicKey);
|
||||
|
||||
String decryptStr = StrUtil.utf8Str(cryptoForDecrypt.decrypt(encryptStr, KeyType.PrivateKey));
|
||||
Assert.assertEquals(text.toString(), decryptStr);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user