gpt4 book ai didi

android - 从 Base64 编码的字符串生成公钥

转载 作者:行者123 更新时间:2023-11-29 21:12:31 28 4
gpt4 key购买 nike

我需要使用 PublicKey 来加密 session key ,但我有公钥作为 Base64 编码字符串,所以我尝试将其转换为:

KeyFactory kf = KeyFactory.getInstance("RSA"); // or "EC" or whatever
byte[] keyBytes = _publicKey.getBytes();
byte[] publicKeyBytes = Base64.decode(keyBytes, Base64.DEFAULT);

X509EncodedKeySpec publicKeySpecs = new X509EncodedKeySpec(publicKeyBytes);
Log.v("", "X509 KS" + publicKeySpecs);
publicKey = kf.generatePublic(publicKeySpecs);

但是我得到的公钥是空值,异常是:

java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

如果有人知道如何去做或有工作代码,请帮助我。

提前致谢

最佳答案

大家好,我从 Base64 编码的字符串生成了 Public Key

经过几天的思考,这段代码对我有用。

String _publicKey= base64encoded_publickey;


// InputStream fileInputStream = null;
try {
// Receiving side

byte[] decodedBytes = Base64.decode(_publicKey, Base64.DEFAULT);

fileInputStream = new ByteArrayInputStream(decodedBytes);

// Print the decoded string

String decodedString = new String(decodedBytes);


CertificateFactory certFactory = CertificateFactory
.getInstance(CERTIFICATE_TYPE);
// certFactory.
X509Certificate cert = (X509Certificate) certFactory
.generateCertificate(fileInputStream);
publicKey = cert.getPublicKey();
Log.d("TAG", "Public key of the certificate:" + publicKey);


} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Could not intialize encryption module",
e);
} finally {
if (keyStream != null) {
try {
keyStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

希望对你有帮助...谢谢...

关于android - 从 Base64 编码的字符串生成公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22371045/

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