2015-06-19 215 views
1

我需要使用ECDSA算法對消息進行簽名併發送給接收方。然後,接收方應驗證發件人的簽名。將字節數組轉換爲publickey ECDSA

所以,對於這一點,接收器由波紋管命令轉換java.security.PublicKey到字節數組之後,但在字節數組格式發送者的公鑰:

byte[] byteArrayPublicKey = publickey.getEncoded(); 

在ECDSA算法(公共密鑰將其轉換爲之前的格式字節數組)是如下:

公鑰:

X: 8a83c389e7bb817c17bf2db4ed71055f18342b630221b2a3a1ca752502dc2e21 

Y: 3eaf48c9ab1700fe0966a0cde196b85af66bb8f0bacef711c9dca2368f9d8470 

但是,問題是該字節數組轉換爲可用的格式到v通過接收器驗證java.security.PublicKey的簽名。

通常,有沒有解決方案來驗證簽名而不將其轉換爲字節數組?換句話說,問題是使用任何方法驗證發件人公鑰的簽名。

+0

如果你的'PublicKey.getEncoded()'的結果是**不**只是公共點X和Y;它還包含一個包含參數的AlgId。 Java調用「X509」編碼,你可以直接將它提供給'KeyFactory.getInstance(「EC」)。generatePublic()'。如果你實際上只有單獨的點座標**和曲線**,請參閱http://stackoverflow.com/questions/22646792/how-does-one-convert-a-public-ec-code-point-and-curve -name-into-a-publickey和http://stackoverflow.com/questions/30445997/loading-raw-64-byte-long-ecdsa-public-key-in-java。 –

回答

2

但問題是將此字節數組轉換爲可用格式來驗證接收方是java.security.PublicKey的簽名。

可以解決像這樣的問題:

public static ECPublicKey genEcPubKey() throws Exception { 
    KeyFactory factory = KeyFactory.getInstance("ECDSA", "BC"); 
    java.security.PublicKey ecPublicKey = (ECPublicKey) factory 
      .generatePublic(new X509EncodedKeySpec(Helper 
        .toByte(ecRemotePubKey))); // Helper.toByte(ecRemotePubKey)) is java.security.PublicKey#getEncoded() 
    return (ECPublicKey) ecPublicKey; 
} 

需要注意的是,你需要BouncyCastle提供商做到這一點。

但問題仍然存在,你如何生成私鑰?

public KeyPair ecKeyPairGenerator(String curveName) throws Exception { 
    KeyPair keyPair; 
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
      "ECDSA", "BC"); 
    ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec(
      curveName); 
    keyPairGenerator.initialize(ecGenParameterSpec, new SecureRandom()); 
    keyPair = keyPairGenerator.generateKeyPair(); 
    java.security.PublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic(); 
    System.out.println("JAVA EC PublicKey: " 
      + Helper.toHex(ecPublicKey.getEncoded())); 

    // write private key into a file. Just for testing purpose 
    FileOutputStream fileOutputStream = new FileOutputStream(
      "ECPrivateKey.key"); 
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
      fileOutputStream); 
    objectOutputStream.writeObject(keyPair.getPrivate()); 
    objectOutputStream.close(); 
    return keyPair; 
} 

我在github有EC符號/驗證的完整運行代碼。你可以看看更好的理解。

+0

Rakeb無效,非常感謝您的有用和偉大的答案。只是應該補充另一件事,我在另一個問題在這裏解釋它: http://stackoverflow.com/questions/30956867/signature-verification-is-always-false-java?noredirect=1#comment49946507_30956867 這件事是尊重ASN.1進行簽名和驗證。 – sas

+0

如果這是您正在尋找的答案,然後嘗試接受它。 TY。 –

+0

我試着爲它投票接受這個答案,但實際上,這至少需要15個聲望。我的聲譽是7.我儘可能的儘快做到這一點,我可以投票。 – sas