Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将公钥转换为OpenSSH授权密钥格式_Java_Bouncycastle_Ssh Keys_Public Key_Authorized Keys - Fatal编程技术网

Java 如何将公钥转换为OpenSSH授权密钥格式

Java 如何将公钥转换为OpenSSH授权密钥格式,java,bouncycastle,ssh-keys,public-key,authorized-keys,Java,Bouncycastle,Ssh Keys,Public Key,Authorized Keys,下面是获取公钥的代码。我需要将公钥转换为OpenSSH格式,以将其添加到Linux中的authorized\u keys文件中。我该怎么做 KeyPairGenerator kpGen = KeyPairGenerator.getInstance("DSA", "BC"); kpGen.initialize(1024, new SecureRandom()); KeyPair keypair = kpGen.generateKeyPair(); 我确实使用了PEMWriter。但是它没有给出正

下面是获取公钥的代码。我需要将公钥转换为OpenSSH格式,以将其添加到Linux中的
authorized\u keys
文件中。我该怎么做

KeyPairGenerator kpGen = KeyPairGenerator.getInstance("DSA", "BC");
kpGen.initialize(1024, new SecureRandom());
KeyPair keypair = kpGen.generateKeyPair();
我确实使用了PEMWriter。但是它没有给出正确格式的输出字符串。

对于另一个问题:

如果您想要反向处理,即编码
PublicKey
Java对象 对于Linux
授权密钥
输入格式,可以使用以下代码:

/**
 * Encode PublicKey (DSA or RSA encoded) to authorized_keys like string
 *
 * @param publicKey DSA or RSA encoded
 * @param user username for output authorized_keys like string
 * @return authorized_keys like string
 * @throws IOException
 */
public static String encodePublicKey(PublicKey publicKey, String user)
        throws IOException {
    String publicKeyEncoded;
    if(publicKey.getAlgorithm().equals("RSA")){
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(byteOs);
        dos.writeInt("ssh-rsa".getBytes().length);
        dos.write("ssh-rsa".getBytes());
        dos.writeInt(rsaPublicKey.getPublicExponent().toByteArray().length);
        dos.write(rsaPublicKey.getPublicExponent().toByteArray());
        dos.writeInt(rsaPublicKey.getModulus().toByteArray().length);
        dos.write(rsaPublicKey.getModulus().toByteArray());
        publicKeyEncoded = new String(
                Base64.encodeBase64(byteOs.toByteArray()));
        return "ssh-rsa " + publicKeyEncoded + " " + user;
    }
    else if(publicKey.getAlgorithm().equals("DSA")){
        DSAPublicKey dsaPublicKey = (DSAPublicKey) publicKey;
        DSAParams dsaParams = dsaPublicKey.getParams();

        ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(byteOs);
        dos.writeInt("ssh-dss".getBytes().length);
        dos.write("ssh-dss".getBytes());
        dos.writeInt(dsaParams.getP().toByteArray().length);
        dos.write(dsaParams.getP().toByteArray());
        dos.writeInt(dsaParams.getQ().toByteArray().length);
        dos.write(dsaParams.getQ().toByteArray());
        dos.writeInt(dsaParams.getG().toByteArray().length);
        dos.write(dsaParams.getG().toByteArray());
        dos.writeInt(dsaPublicKey.getY().toByteArray().length);
        dos.write(dsaPublicKey.getY().toByteArray());
        publicKeyEncoded = new String(
                Base64.encodeBase64(byteOs.toByteArray()));
        return "ssh-dss " + publicKeyEncoded + " " + user;
    }
    else{
        throw new IllegalArgumentException(
                "Unknown public key encoding: " + publicKey.getAlgorithm());
    }
}

@gotoalberto的代码仅针对RSA和DSA密钥实现。如果您需要其他密钥,您必须自己添加它们