This commit is contained in:
Looly
2020-09-14 19:06:06 +08:00
parent 7d81b20e2b
commit 999c1d80d4
7 changed files with 257 additions and 64 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}