RSAEncrypt&RSAEncrypt

public static String encryptString(String input, String channel) throws Exception {
        if (StringUtils.isBlank(channel)) {
            throw new BusinessException("缺少channel");
        }
        Cipher cipher = Cipher.getInstance("RSA");
        RSAPublicKey pubKey = (RSAPublicKey) getPublicKey(RSAEncrypt.class.getResourceAsStream("/security/" + channel + "/publicKey"));
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] cipherText = cipher.doFinal(input.getBytes());
        // 加密后的东西
        if (logger.isDebugEnabled()) {
            logger.debug("cipher: " + new String(cipherText));
        }
        return Base64.encodeBase64String(cipherText);
    }

    public static PublicKey getPublicKey(InputStream inputStream) throws Exception {
        DataInputStream dis = new DataInputStream(inputStream);
        byte[] keyBytes = new byte[inputStream.available()];
        dis.readFully(keyBytes);
        dis.close();
        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePublic(spec);
    }

    /**
     * 通过私有密匙加密
     *
     * @param input         需要解密的内容
     * @param privateKeyStr 私有密匙
     * @author wangxy2
     * @since 2021-06-18
     */
    public static String encryptStringByPrivateKey(String input, String privateKeyStr, String charsetName) throws Exception {
        if (StringUtils.isBlank(privateKeyStr)) {
            throw new BusinessException("缺少密匙");
        }
        Cipher cipher = Cipher.getInstance("RSA");
        RSAPrivateKey privateKey = (RSAPrivateKey) getPrivateKey(privateKeyStr);
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);


        byte[] cipherText;
        if (StringUtils.isNotEmpty(charsetName)) {
            cipherText = cipher.doFinal(input.getBytes());
        } else {
            cipherText = cipher.doFinal(input.getBytes(charsetName));
        }
        // 加密后的东西
        if (logger.isDebugEnabled()) {
            logger.debug("private_encrypt_cipher: " + new String(cipherText));
        }
        return Base64.encodeBase64String(cipherText);
    }
    
    public static PrivateKey getPrivateKey(String  privateKeyStr) throws Exception {
        PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new Base64().decode(privateKeyStr.getBytes()));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(priPKCS8);
    }





/**
     * @param input   需要解密的内容
     * @param channel 渠道
     * @return
     * @throws Exception
     */
    public static String decryptString(String input, String channel) {
        if (StringUtils.isBlank(channel)) {
            throw new BusinessException("缺少channel");
        }
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("RSA");
            RSAPrivateKey privKey;
            privKey = (RSAPrivateKey) getPrivateKey(RSADecrypt.class.getResourceAsStream("/security/" + channel + "/privateKey"));
            // 开始解密
            cipher.init(Cipher.DECRYPT_MODE, privKey);
            byte[] plainText = cipher.doFinal(Base64.decodeBase64(input));
            if (logger.isDebugEnabled()) {
                logger.debug("cipher: " + new String(plainText));
            }
            return new String(plainText);
        } catch (Exception e) {
            logger.error(ExceptionCollect.collectExceptionStackMsg(e));
        }
        return null;
    }

    public static PrivateKey getPrivateKey(InputStream inputStream) throws Exception {
        DataInputStream dis = new DataInputStream(inputStream);
        byte[] keyBytes = new byte[inputStream.available()];
        dis.readFully(keyBytes);
        dis.close();
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(spec);
    }

    /**
     * 通过私有密匙解密
     *
     * @param input         需要解密的内容
     * @param privateKeyStr 私有密匙
     * @author wangxy2
     * @since 2021-06-18
     */
    public static String decryptStringByPrivateKey(String input, String privateKeyStr, String charsetName) {
        if (StringUtils.isBlank(privateKeyStr)) {
            throw new BusinessException("缺少密匙");
        }
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("RSA");
            RSAPrivateKey privateKey = (RSAPrivateKey) getPrivateKey(privateKeyStr);
            // 开始解密
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] plainText = cipher.doFinal(Base64.decodeBase64(input));
            if (logger.isDebugEnabled()) {
                logger.debug("private_decrypt_cipher: " + new String(plainText));
            }
            return new String(plainText);
        } catch (Exception e) {
            logger.error(ExceptionCollect.collectExceptionStackMsg(e));
        }
        return null;
    }

    public static PrivateKey getPrivateKey(String privateKeyStr) throws Exception {
        PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new Base64().decode(privateKeyStr.getBytes()));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(priPKCS8);
    }


AlipaySignature.rsaEncrypt()
<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>4.13.50.ALL</version>
</dependency>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值