| | |
| | | package org.dromara.common.encrypt.core.encryptor; |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import cn.hutool.crypto.asymmetric.KeyType; |
| | | import cn.hutool.crypto.asymmetric.RSA; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.encrypt.core.EncryptContext; |
| | | import org.dromara.common.encrypt.enumd.AlgorithmType; |
| | | import org.dromara.common.encrypt.enumd.EncodeType; |
| | | import org.dromara.common.encrypt.utils.EncryptUtils; |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class RsaEncryptor extends AbstractEncryptor { |
| | | |
| | | private final RSA rsa; |
| | | private final EncryptContext context; |
| | | |
| | | public RsaEncryptor(EncryptContext context) { |
| | | super(context); |
| | |
| | | if (StringUtils.isAnyEmpty(privateKey, publicKey)) { |
| | | throw new IllegalArgumentException("RSA公私钥均需要提供,公钥加密,私钥解密。"); |
| | | } |
| | | this.rsa = SecureUtil.rsa(Base64.decode(privateKey), Base64.decode(publicKey)); |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public String encrypt(String value, EncodeType encodeType) { |
| | | if (encodeType == EncodeType.HEX) { |
| | | return rsa.encryptHex(value, KeyType.PublicKey); |
| | | return EncryptUtils.encryptByRsaHex(value, context.getPublicKey()); |
| | | } else { |
| | | return rsa.encryptBase64(value, KeyType.PublicKey); |
| | | return EncryptUtils.encryptByRsa(value, context.getPublicKey()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return this.rsa.decryptStr(value, KeyType.PrivateKey); |
| | | return EncryptUtils.decryptByRsa(value, context.getPrivateKey()); |
| | | } |
| | | } |