mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add method
This commit is contained in:
parent
5a91698a78
commit
6f3b5a1f1a
@ -8,6 +8,7 @@
|
||||
### 新特性
|
||||
* 【extra 】 增加自动装配SpringUtil类(pr#1366@Github)
|
||||
* 【extra 】 ArrayUtil增加map方法重载
|
||||
* 【crypto 】 AsymmetricAlgorithm增加RSA_ECB("RSA/ECB/NoPadding")(issue#1368@Github)
|
||||
|
||||
### Bug修复
|
||||
* 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee)
|
||||
|
@ -12,6 +12,8 @@ public enum AsymmetricAlgorithm {
|
||||
RSA("RSA"),
|
||||
/** RSA算法,此算法用了默认补位方式为RSA/ECB/PKCS1Padding */
|
||||
RSA_ECB_PKCS1("RSA/ECB/PKCS1Padding"),
|
||||
/** RSA算法,此算法用了默认补位方式为RSA/ECB/NoPadding */
|
||||
RSA_ECB("RSA/ECB/NoPadding"),
|
||||
/** RSA算法,此算法用了RSA/None/NoPadding */
|
||||
RSA_None("RSA/None/NoPadding");
|
||||
|
||||
|
@ -8,6 +8,7 @@ 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;
|
||||
@ -73,6 +74,50 @@ public class RSATest {
|
||||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user