0

我目前正在嘗試使用BC實現McEliece加密,但遇到了一些麻煩。我目前有能力創建密鑰並將它們放置到文件中,我可以將它們讀回到程序中,但無法從字節回到公鑰。McEliece(Bouncy Castle)獲取公鑰返回

下面是我目前有:

 public static String EncryptText(Component tmp, String Plaintext) throws InvalidKeyException, InvalidCipherTextException { 
    String CipherText = "Didnt Work"; 
    try { 
     // The message to encrypt. 
     byte[] messageBytes = Plaintext.getBytes(); 

     //read in the Public Key to use to Encrypt. 
     File f = new File(tmp.getPublicKey()); 
     FileInputStream fis = new FileInputStream(f); 
     byte[] PubKeybytes = new byte[fis.available()]; 
     fis.read(PubKeybytes); 
     fis.close(); 


     //turn the bytes into the Key. 
     X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(PubKeybytes); 
     SubjectPublicKeyInfo PKI ; 
     KeyFactory KF = null; 
     try { 
      KF = KeyFactory.getInstance("McEliece"); 
     } catch (NoSuchAlgorithmException ex) { 
      Logger.getLogger(McEliecePKCS.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     PublicKey PK = null; 
     try { 
      PK = KF.generatePublic(pubKeySpec); 
     } catch (InvalidKeySpecException ex) { 
      Logger.getLogger(McEliecePKCS.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     //Public Key 
     PublicKey aPublic = PK; 
     McEliecePublicKeyParameters GPKP = (McEliecePublicKeyParameters) McElieceKeysToParams.generatePublicKeyParameter(aPublic); 

     //set the public key to use. 
     McElieceCipher EnCipheredText = new McElieceCipher(); 
     EnCipheredText.init(true, GPKP); 
     EnCipheredText.initCipherEncrypt(GPKP); 

     byte[] ciphertextBytes; 

     //sign the message with the public key. 
     ciphertextBytes = EnCipheredText.messageEncrypt(messageBytes); 
     CipherText = new String(ciphertextBytes); 
     return CipherText; 
    } catch (IOException ex) { 
     Logger.getLogger(McEliecePKCS.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    return CipherText; 
}\ 

與此代碼具有當前誤差IM與的KeyFactory和「McEliece公鑰體制」不認爲是算法即時得到拋出:NoSuchAlgorithmException,但我不是很確定什麼別的現在就試試。我也嘗試過使用BouncyCastle包含的McEliece中的KeyFactory,但沒有成功,因爲這些方法是受保護的,或者不允許使用KeySpec,並且想要SubjectPublicKeyInfo,我無法弄清楚如何將KeySpec改成Byte數組成。

對不起,如果這是一個簡單的問題,相當新的編碼密碼學。

感謝您提前回復。

回答

1

管理找出問題。我需要補充:

  Security.addProvider(new BouncyCastleProvider()); 
      Security.addProvider(new BouncyCastlePQCProvider()); 
+0

感謝您報告返回MrFolo,你可以在一段時間後接受你自己的答案(如果我沒有弄錯,一天或2天)。 –

相關問題