收藏
回答

调/v3/apply4subject/applyment接口返回平台私钥解密失败

但是我这边加密解密都可以正常显示,不知道这个是啥问题



public static String rsaEncryptOAEP(String message, X509Certificate certificate)
throws IllegalBlockSizeException, IOException {
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
       cipher.init(Cipher.ENCRYPT_MODE, certificate.getPublicKey());

       byte[] data = message.getBytes("utf-8");
       byte[] cipherdata = cipher.doFinal(data);
       return Base64.getEncoder().encodeToString(cipherdata);
   } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
   } catch (InvalidKeyException e) {
throw new IllegalArgumentException("无效的证书", e);
   } catch (IllegalBlockSizeException | BadPaddingException e) {
throw new IllegalBlockSizeException("加密原串的长度不能超过214字节");
   }
}

public static String rsaDecryptOAEP(String ciphertext, PrivateKey privateKey)
throws BadPaddingException, IOException {
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
       cipher.init(Cipher.DECRYPT_MODE, privateKey);

       byte[] data = Base64.getDecoder().decode(ciphertext);
       return new String(cipher.doFinal(data), "utf-8");
   } catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
   } catch (InvalidKeyException e) {
throw new IllegalArgumentException("无效的私钥", e);
   } catch (BadPaddingException | IllegalBlockSizeException e) {
throw new BadPaddingException("解密失败");
   }
}


最后一次编辑于  2019-11-18
回答关注问题邀请回答
收藏

1 个回答

  • 陈华
    陈华
    2019-11-18

    已经解决了,加密的私钥不对,需要调证书下载接口获取

    2019-11-18
    有用
    回复 3
    • 星辰
      星辰
      2019-12-25
      X509Certificate 这个对象如何获取的呢? 我掉证书下载接口返回的是json串
      2019-12-25
      回复
    • 我是一根萝卜
      我是一根萝卜
      2020-01-01
      老哥是那个私钥加密能解释下嘛
      2020-01-01
      回复
    • 田晓东
      田晓东
      2020-01-04
      怎么解决的,可以说一下
      2020-01-04
      回复
登录 后发表内容
问题标签