gpt4 book ai didi

java.lang.RuntimeException : error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag 错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:41 24 4
gpt4 key购买 nike

我收到这个错误(在标题中)。我不确定为什么,请帮助。代码如下:

public static String decryptRSA(Context mContext, byte[] message) throws Exception { 


InputStream in = mContext.getResources().openRawResource(R.raw.publicrsakey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(org.apache.commons.io.IOUtils.toByteArray(in));

PublicKey publicKey =
KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
final String encryptedString = Base64.encode(cipher.doFinal(message));

return encryptedString;


}

编辑。最后,我使用扩展名为 .der 的公钥文件解决了这个问题(之前是 .crt),有效的代码是:

InputStream in = mContext.getResources().openRawResource(R.raw.key);

CertificateFactory cf = CertificateFactory.getInstance("X509");
Certificate cert = cf.generateCertificate(new ByteArrayInputStream(org.apache.commons.io.IOUtils.toByteArray(in)));
PublicKey pubKey = cert.getPublicKey();
try
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
final String encryptedString = Base64.encode(cipher.doFinal(message));
return encryptedString;
}
catch (Exception e)
{
e.printStackTrace();
}
return "";

但是“divanov”回答了我的问题。

最佳答案

异常 error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag 表示结果

InputStream in = mContext.getResources().openRawResource(R.raw.publicrsakey);
byte[] pubKeyBytes = org.apache.commons.io.IOUtils.toByteArray(in);

不代表ASN.1 DER编码的消息。将它作为十六进制打印在某个地方以验证什么是确切的问题

Log.v("HEX", org.apache.commons.codec.binary.Hex.encodeHexString(pubKeyBytes);

关于java.lang.RuntimeException : error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22842235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com