gpt4 book ai didi

java - 无效 key 异常 : invalid key format in Java using RSA Keys

转载 作者:行者123 更新时间:2023-12-02 00:15:21 26 4
gpt4 key购买 nike

我使用 java keygenerator 生成了一个公共(public) key :

Sun RSA public key, 1024 bits modulus: 106394877608018766537720801416655991345106535990850729605963854419450103716730599362154190537257597233014065015311499176359112816419965961469419756050290964343366687334245741905264407605904082573446954295309549250335299907317631410981650257400135070254491184426528002396792285738067623733919575203674519111607 public exponent: 65537

我从客户端发送了这个 key (通过复制、粘贴)并收到了与字符串完全相同的 key 。然后我尝试重建它:

byte [] bytes = publicKey.getBytes("UTF-8");
Key key = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));

然后我将从函数中返回。我从形成 key 的第 2 行收到Invalid Key Exception

有什么想法吗?

最佳答案

您需要使用RSAPublicKeySpec

String modulus="106394877608018766537720801416655991345106535990850729605963854419450103716730599362154190537257597233014065015311499176359112816419965961469419756050290964343366687334245741905264407605904082573446954295309549250335299907317631410981650257400135070254491184426528002396792285738067623733919575203674519111607";
RSAPublicKeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus),BigInteger.valueOf(65537));

Key key = KeyFactory.getInstance("RSA").generatePublic(spec);

关于java - 无效 key 异常 : invalid key format in Java using RSA Keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11952922/

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