
如何将 ssh-keygen 生成的公钥和私钥转换为 java 中的 rsapublickey 和 rsaprivatekey
您提到的函数无法正确转换 ssh-keygen 生成的公钥,因为 ssh-keygen 生成的公钥采用专有格式,而该函数适用于标准 x.509 der 编码的公钥。
转换步骤:
要在 java 中转换 ssh-keygen 生成的公钥和私钥:
解码公钥:
立即学习“Java免费学习笔记(深入)”;
Waymark
Waymark是一个视频制作工具,帮助企业快速轻松地制作高影响力的广告。
79 查看详情
使用 base64 解码公钥字符串。忽略前 11 个字节(固定签名“0007ssh-rsa”)。读出第 11 到第 14 个字节,将其解释为指数长度(小端表示)。读出指数长度个字节,将其解释为指数(大端表示)。读出第 15 到第 18 个字节,将其解释为模数长度(小端表示)。读出模数长度个字节,将其解释为模数(大端表示)。
解码私钥:
根据公钥转换得到的指数和模数,生成 rsapublickeyspec。使用 base64 解码私钥字符串。同样忽略前 11 个字节。读出第 11 到第 14 个字节,将其解释为私钥长度(大端表示)。读出私钥长度个字节,将其解释为 der 编码的私钥(包括 n、e、d、p、q、dp 和 iq)。
生成 java 对象:
使用 rsapublickeyspec 实例化 rsapublickey。使用 asn.1 der 解码器解析私钥 der 编码数据。从解码的数据中提取 rsaprivatecrtkeyfields(n、e、d、p、q、dp 和 iq)并实例化 rsaprivatekey。
示例代码:
// 公钥转换byte[] decodedPublicKey = Base64.getDecoder().decode(publicKeyString);// 忽略前 11 字节int exponentLength = decodedPublicKey[11] & 0xFF | (decodedPublicKey[12] & 0xFF) << 8 | (decodedPublicKey[13] & 0xFF) << 16 | (decodedPublicKey[14] & 0xFF) << 24;byte[] exponent = new byte[exponentLength];System.arraycopy(decodedPublicKey, 15, exponent, 0, exponentLength);int modulusLength = decodedPublicKey[15 + exponentLength] & 0xFF | (decodedPublicKey[16 + exponentLength] & 0xFF) << 8 | (decodedPublicKey[17 + exponentLength] & 0xFF) << 16 | (decodedPublicKey[18 + exponentLength] & 0xFF) << 24;byte[] modulus = new byte[modulusLength];System.arraycopy(decodedPublicKey, 19 + exponentLength, modulus, 0, modulusLength);RSAPublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(new BigInteger(1, modulus), new BigInteger(1, exponent)));// 私钥转换byte[] decodedPrivateKey = Base64.getDecoder().decode(privateKeyString);// 忽略前 11 字节int privateKeyLength = decodedPrivateKey[11] & 0xFF | (decodedPrivateKey[12] & 0xFF) << 8 | (decodedPrivateKey[13] & 0xFF) << 16 | (decodedPrivateKey[14] & 0xFF) << 24;byte[] derPrivateKey = new byte[privateKeyLength];System.arraycopy(decodedPrivateKey, 15, derPrivateKey, 0, privateKeyLength);ASN1InputStream input = new ASN1InputStream(new ByteArrayInputStream(derPrivateKey));ASN1Sequence privateKeySequence = (ASN1Sequence) input.readObject();BigInteger n = ((ASN1Integer) privateKeySequence.getObjectAt(0)).getValue();BigInteger e = ((ASN1Integer) privateKeySequence.getObjectAt(1)).getValue();BigInteger d = ((ASN1Integer) privateKeySequence.getObjectAt(2)).getValue();BigInteger p = ((ASN1Integer) privateKeySequence.getObjectAt(3)).getValue();BigInteger q = ((ASN1Integer) privateKeySequence.getObjectAt(4)).getValue();BigInteger dp = ((ASN1Integer) privateKeySequence.getObjectAt(5)).getValue();BigInteger iq = ((ASN1Integer) privateKeySequence.getObjectAt(6)).getValue();RSAPrivateKey privateKey = new RSAPrivateCrtKey(n, e, d, p, q, dp, iq);
以上就是如何将 SSH-Keygen 生成的公钥和私钥转换为 Java 中的 RSAPublicKey 和 RSAPrivateKey?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1004756.html
微信扫一扫
支付宝扫一扫