Read KeyPair#39;s publickey in RSA OpenSSH format?(以 RSA OpenSSH 格式读取 KeyPair 的公钥?)
问题描述
我通过执行以下操作在 Java 中创建了一个 KeyPair
:
I’ve created a KeyPair
in Java by doing the following:
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
如何从 keyPair
获取以 -----BEGIN"
开头的 RSA OpenSSH 格式的 publicKey
?
How do I get the publicKey
from keyPair
in the RSA OpenSSH format that begins with "-----BEGIN"
?
推荐答案
这是一个我没有测试过的快速 hack.这需要 Java 6 或更高版本.有关详细信息,请参阅以下 RFC:
Here is a quick hack which I haven't tested. This requires Java 6 or greater. For more information see the following RFCs:
RFC 4716
RFC 4253
RFC 4251
import java.io.*;
import java.math.BigInteger;
import java.nio.*;
import java.nio.charset.Charset;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import javax.xml.bind.DatatypeConverter;
public class SecshPublicKey {
/*
* Taken from RFC 4716, with reference to RFCs 4253 and 4251.
*/
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
ByteArrayOutputStream binaryOS = new ByteArrayOutputStream();
writeSshString(binaryOS, "ssh-rsa");
RSAPublicKey rsaPub = (RSAPublicKey)keyPair.getPublic();
writeSshMPInt(binaryOS, rsaPub.getPublicExponent());
writeSshMPInt(binaryOS, rsaPub.getModulus());
// Now base64-encode the result.
String b64Encoded = sshBase64Encode(binaryOS.toByteArray());
// Now write out the result
System.out.println("---- BEGIN SSH2 PUBLIC KEY ----");
System.out.println(b64Encoded);
System.out.println("---- END SSH2 PUBLIC KEY ----");
}
private static String sshBase64Encode(byte[] byteArray) {
String b64_prelim = DatatypeConverter.printBase64Binary(byteArray);
// Break into lines of at most 72 characters.
StringBuilder b64_final = new StringBuilder(b64_prelim.length() * 2);
while (b64_prelim.length() > 72) {
b64_final.append(b64_prelim.substring(0, 72));
b64_final.append("
");
b64_prelim = b64_prelim.substring(72);
}
b64_final.append(b64_prelim);
return b64_final.toString();
}
private static void writeSshMPInt(OutputStream os, BigInteger mpint) throws IOException {
ByteBuffer lengthBuf = ByteBuffer.allocate(4);
lengthBuf.order(ByteOrder.BIG_ENDIAN);
byte [] x;
if (mpint.equals(BigInteger.ZERO)) {
x = new byte[0];
} else {
x = mpint.toByteArray();
}
lengthBuf.putInt(x.length);
os.write(lengthBuf.array());
os.write(x);
}
private static void writeSshString(OutputStream os, String s) throws IOException {
ByteBuffer lengthBuf = ByteBuffer.allocate(4);
lengthBuf.order(ByteOrder.BIG_ENDIAN);
byte [] encoded = s.getBytes(Charset.forName("UTF-8"));
lengthBuf.putInt(encoded.length);
os.write(lengthBuf.array());
os.write(encoded);
}
}
这篇关于以 RSA OpenSSH 格式读取 KeyPair 的公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以 RSA OpenSSH 格式读取 KeyPair 的公钥?
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01