gpt4 book ai didi

java - Android-Java RSA解密

转载 作者:行者123 更新时间:2023-12-01 12:28:45 24 4
gpt4 key购买 nike

我需要从二进制字符串创建 RSA 算法的公钥。

我的代码是:

String pubKey = "tihq/Gk3OUs5NzP+XTRKXBwSxHtB0TWn0RREcpXEtp316tyD9DzKaIbdKexb/mRr";

byte[] keyBytes = Base64.decode(pubKey,Base64.DEFAULT);
//test if is correct: ok
Log.d("response keyBytes",new String(Base64.encode(keyBytes,Base64.DEFAULT)));

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);

RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(spec);
//PublicKey publicKey = (PublicKey) keyFactory.generatePublic(spec);

但是我有这个错误:

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

最佳答案

已解决。问题是我有模数和指数,所以我必须这样做:

String pubKey = "tihq/Gk3OUs5NzP+XTRKXBwSxHtB0TWn0RREcpXEtp316tyD9DzKaIbdKexb/mRr"; //64 caratteri
String exponent = "AQAB";

byte[] keyBytes = Base64.decode(pubKey,Base64.DEFAULT);
byte[] exponentByte = Base64.decode(exponent,Base64.DEFAULT);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(keyBytes), new BigInteger(exponentByte));
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);

关于java - Android-Java RSA解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26117881/

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