疯狂的狮子Li
2023-08-07 86a8f5a700a19d8e55da5f5f0d5edd6b1284934f
ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/RsaEncryptor.java
@@ -1,13 +1,10 @@
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;
/**
@@ -18,7 +15,7 @@
 */
public class RsaEncryptor extends AbstractEncryptor {
    private final RSA rsa;
    private final EncryptContext context;
    public RsaEncryptor(EncryptContext context) {
        super(context);
@@ -27,7 +24,7 @@
        if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
            throw new IllegalArgumentException("RSA公私钥均需要提供,公钥加密,私钥解密。");
        }
        this.rsa = SecureUtil.rsa(Base64.decode(privateKey), Base64.decode(publicKey));
        this.context = context;
    }
    /**
@@ -47,9 +44,9 @@
    @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());
        }
    }
@@ -60,6 +57,6 @@
     */
    @Override
    public String decrypt(String value) {
        return this.rsa.decryptStr(value, KeyType.PrivateKey);
        return EncryptUtils.decryptByRsa(value, context.getPrivateKey());
    }
}