HTN_Code 2022-03-04 10:48 采纳率: 42.9%
浏览 24

关于Java加密解密的问题!

前端传给我密文,我在后端进行解密,然后再进行业务处理,但目前我不会解密,目前我知道加密的方法,如下
    public static String rsaEncrypt(String data, String publicKey) {
        String result = "";
        try {
            // 将Base64编码后的公钥转换成PublicKey对象
            byte[] buffer = Base64.decode(publicKey);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            PublicKey pubKey = keyFactory.generatePublic(keySpec);
            // 加密
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            byte[] inputArray = data.getBytes();
            int inputLength = inputArray.length;
            System.out.println("加密字节数:" + inputLength);
            // 最大加密字节数,超出最大字节数需要分组加密
            int MAX_ENCRYPT_BLOCK = 117;
            int real = 117;
            // 标识
            int offSet = 0;
            byte[] resultBytes = {};
            byte[] cache = {};
            while (inputLength - offSet > 0) {
                if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
                    if(!(inputArray[real + offSet] >= 0 && inputArray[real + offSet] <=127)){
                        real = real - 1;
                        continue;
                    }
                    cache = cipher.doFinal(inputArray, offSet, real);
                    offSet += real;
                } else {
                    cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
                    offSet = inputLength;
                }
                resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
                System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
            }
            result = Base64.encode(resultBytes);
        } catch (Exception e) {
            System.out.println("rsaEncrypt error:" + e.getMessage());
        }
        return result;
    }

请问解密方法该如何写,谢谢
  • 写回答

2条回答 默认 最新

  • 西凉的悲伤 2022-03-04 11:23
    关注

    RSA解密

    评论

报告相同问题?

问题事件

  • 创建了问题 3月4日

悬赏问题

  • ¥20 抖音商城拉码器安卓报错求解决办法或者有新的拉码脚本也可以介绍一下
  • ¥15 MPLAB IDE V2.35 报错make[2]: *** [build/default/production/_ext/1472/MSSP_I2C.p1] Error 1
  • ¥15 新唐M451 DTMF检测和发送代码修改
  • ¥15 在国外文献网站里点击view pdf 加载异常缓慢甚至加载不出来。
  • ¥65 python批量提取发票的信息
  • ¥15 虚幻五引擎内容如何上传至网盘?
  • ¥15 使用mmpose库时出现了问题
  • ¥15 IRI2016模型matlab运行报错
  • ¥50 bat怎么设置电脑后台自动点击网页指定词运行脚本,输入指定网页链接,指定点击词,指定间隔时间,指定网页出现的词,指定网页出现词出现后后点击锁定,放在后台运行不影响前台鼠标工作
  • ¥20 20CrMnMo的高温变形抗力