How to generate ssh compatible id_rsa(.pub) from Java

前端 未结 4 543
再見小時候
再見小時候 2020-12-02 10:52

I\'m looking for a way to programmatically create ssh compatible id_rsa and id_rsa.pub files in Java.

I got as far as creating the KeyPair:

KeyPairG         


        
4条回答
  •  清歌不尽
    2020-12-02 11:18

    gotoalberto's answer (quoted below) for a different question works for both RSA and DSA keys:

    If you want reverse the process, i.e. encode a PublicKey Java object to a Linux authorized_keys entry format, one can use this code:

        /**
         * 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());
            }
        }
    

提交回复
热议问题