2014-07-21 42 views
1

我已經做在Java中創建一個KeyPair如下:以RSA OpenSSH格式閱讀KeyPair的publickey?

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); 
keyPairGenerator.initialize(1024, new SecureRandom()); 
KeyPair keyPair = keyPairGenerator.generateKeyPair(); 

我如何從keyPairpublicKey在與「-----BEGIN」開始RSA OpenSSH格式?

+0

此代碼生成一個新的隨機密鑰對,它不會從任何地方閱讀。 –

+0

[如何讀取.pem文件以獲取私鑰和​​公鑰]可能的重複(http://stackoverflow.com/questions/11787571/how-to-read-pem-file-to-get-private-and-public -鍵) –

回答

2

這是一個我沒有測試過的快速入侵。這需要Java 6或更高版本。欲瞭解更多信息,請參閱以下RFC:

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("\n"); 
      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); 
    } 

}