给定Java ssh-rsa PublicKey,如何构建SSH2公钥? [英] Given a Java ssh-rsa PublicKey, how can I build an SSH2 public key?

查看:193
本文介绍了给定Java ssh-rsa PublicKey,如何构建SSH2公钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用publicKey.getEncoded(),然后将ssh-rsa附加到前面,然后对其进行base64编码。然后我添加SSH2页眉/页脚。但它不会解码...

I'm doing publicKey.getEncoded(), then appending "ssh-rsa" to the front, then base64 encoding it. Then I add the SSH2 header/footer. But it won't decode...

推荐答案

Java公钥被编码为标准的X.509 SubjectPublicKeyInfo结构。

Java public keys are encoded as a standard X.509 SubjectPublicKeyInfo structure.

SSH2使用自己的简单格式。 Base-64编码下面显示的 encode 方法的结果,并附加必要的SSH2页眉和页脚。

SSH2 uses its own simple format. Base-64 encode the result of the encode method shown below, and affix the necessary SSH2 header and footer.

public static byte[] encode(RSAPublicKey key)
  throws IOException
{
  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  byte[] name = "ssh-rsa".getBytes("US-ASCII");
  write(name, buf);
  write(key.getPublicExponent().toByteArray(), buf);
  write(key.getModulus().toByteArray(), buf);
  return buf.toByteArray();
}

private static void write(byte[] str, OutputStream os)
  throws IOException
{
  for (int shift = 24; shift >= 0; shift -= 8)
    os.write((str.length >>> shift) & 0xFF);
  os.write(str);
}

参见这个答案用于转换另一个方向,从OpenSSH转换为Java。

See this answer for converting the other direction, from OpenSSH to Java.

这篇关于给定Java ssh-rsa PublicKey,如何构建SSH2公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆