我目前正在尝试使用BC实现McEliece加密,但是遇到了一些麻烦。我目前具有创建密钥并将其放置到文件中的功能,我可以将它们读回到程序中,但无法将其从字节转换回公钥。

以下是我目前拥有的:

        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;
}\


该代码存在的当前错误与KeyFactory有关,并且“ McEliece”不被视为一种算法,因为它获得了NoSuchAlgorithmException,但我不确定目前还可以尝试什么。我也曾尝试将BouncyCastle随附的KeyFactory用于McEliece,但由于该方法受到保护或不允许使用KeySpec并希望SubjectPublicKeyInfo而没有成功,因此我无法弄清楚如何将KeySpec更改为Byte数组入。

抱歉,如果这是一个对密码术尚不新鲜的简单问题。

感谢您的提前答复。

最佳答案

设法找出问题所在。我需要添加:

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

10-06 15:36