2016-11-11 97 views
3

我正在嘗試使用充氣城堡來生成ECDSA密鑰。該代碼似乎從Java的角度來看工作正常;但是,當我轉儲文件並嘗試驗證數據時,OpenSSL不喜歡數據的格式。在充氣城堡中生成ECDSA私鑰會返回一個公共密鑰

經過一番研究,我發現充氣城堡將私鑰編碼爲公鑰。

這裏是我的Java代碼:

public class Test { 
    public static void main(String[] args) { 
     Security.addProvider(new BouncyCastleProvider()); 
     System.out.println("Starting..."); 
     String name = "prime256v1"; 
     try { 
      KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDSA", BouncyCastleProvider.PROVIDER_NAME); 
      kpg.initialize(new ECGenParameterSpec(name)); 
      KeyPair keyPair = kpg.generateKeyPair();  
      FileOutputStream writer = new FileOutputStream("private.key"); 
      writer.write(keyPair.getPrivate().getEncoded()); 
      writer.close(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

在有效DER格式生成private.key文件;然而,當我運行以下命令來查看關鍵的ASN.1結構:

$ openssl asn1parse -inform DER -in /my/path/private.key 
    0:d=0 hl=3 l= 147 cons: SEQUENCE   
    3:d=1 hl=2 l= 1 prim: INTEGER   :00 
    6:d=1 hl=2 l= 19 cons: SEQUENCE   
    8:d=2 hl=2 l= 7 prim: OBJECT   :id-ecPublicKey 
    17:d=2 hl=2 l= 8 prim: OBJECT   :prime256v1 
    27:d=1 hl=2 l= 121 prim: OCTET STRING  [HEX DUMP]: <hex data> 

爲了進行比較,如果我運行下面的命令使用OpenSSL生成一個ECDSA鍵,我得到以下ASN.1結構:

$ openssl ecparam -name prime256v1 -genkey -noout -outform DER -out private.key 
$ openssl asn1parse -inform DER -in private.key 
    0:d=0 hl=2 l= 119 cons: SEQUENCE   
    2:d=1 hl=2 l= 1 prim: INTEGER   :01 
    5:d=1 hl=2 l= 32 prim: OCTET STRING  [HEX DUMP]: <hex data> 
    39:d=1 hl=2 l= 10 cons: cont [ 0 ]   
    41:d=2 hl=2 l= 8 prim: OBJECT   :prime256v1 
    51:d=1 hl=2 l= 68 cons: cont [ 1 ]   
    53:d=2 hl=2 l= 66 prim: BIT STRING   

所以,我想我的問題是

  • 有什麼我失蹤?
  • 或者這是一個已知的錯誤?
  • 有沒有辦法避開它?
+0

「OpenSSL不喜歡數據的格式。」你能詳細說明一下嗎?任何操作失敗了嗎? – jay

+0

@jay yes,'$ openssl ec -inform DER -in/my/path/private.key'失敗 – Rodolfo

+2

沒有錯誤,只是Java輸出PKCS8格式,而openssl命令並不期望它。 –

回答

0

Java以編碼格式輸出密鑰。您應該嘗試:

private String getPrivateKeyAsHex(PrivateKey privateKey) { 

    ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey; 
    byte[] privateKeyBytes = new byte[PRIVATE_KEY_LENGTH]; 
    writeToStream(privateKeyBytes, 0, ecPrivateKey.getS(), PRIVATE_KEY_LENGTH); 

    String hex = Hex.toHexString(privateKeyBytes); 

    logger.debug("Private key bytes: " + Arrays.toString(privateKeyBytes)); 
    logger.debug("Private key hex: " + hex); 

    return hex; 
} 

private String getPublicKeyAsHex(PublicKey publicKey) { 

    ECPublicKey ecPublicKey = (ECPublicKey) publicKey; 
    ECPoint ecPoint = ecPublicKey.getW(); 

    byte[] publicKeyBytes = new byte[PUBLIC_KEY_LENGTH]; 
    writeToStream(publicKeyBytes, 0, ecPoint.getAffineX(), PRIVATE_KEY_LENGTH); 
    writeToStream(publicKeyBytes, PRIVATE_KEY_LENGTH, ecPoint.getAffineY(), PRIVATE_KEY_LENGTH); 

    String hex = Hex.toHexString(publicKeyBytes); 

    logger.debug("Public key bytes: " + Arrays.toString(publicKeyBytes)); 
    logger.debug("Public key hex: " + hex); 

    return hex; 
} 

private void writeToStream(byte[] stream, int start, BigInteger value, int size) { 
    byte[] data = value.toByteArray(); 
    int length = Math.min(size, data.length); 
    int writeStart = start + size - length; 
    int readStart = data.length - length; 
    System.arraycopy(data, readStart, stream, writeStart, length); 
} 
+0

我真的不知道該怎麼做。我把它插入到我的代碼中,它顯示十六進制(如我所料),但它不能被openssl解析。 – Rodolfo