Mr_Folo
Mr_Folo

Reputation: 11

McEliece (Bouncy Castle) Getting the public key back

I am currently trying to implement McEliece encryption using BC but running into some trouble. I currently have the capabilities to create the keys and place them into a file, i can read them back into the program but cannot get it to go from bytes back to Public Key.

Below is what i currently have:

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

The current error im having with this code is with the KeyFactory and that "McEliece" is not considered an algorithm as im getting NoSuchAlgorithmException but im not really sure what else to try at the moment. i have also tried to use the KeyFactory that is included with BouncyCastle for McEliece but had no success as the methods were either protected or did not allow for KeySpec and wanted SubjectPublicKeyInfo which i could not figure out how to change the KeySpec into or the Byte array into.

Sorry if this is a simple question im fairly new to coding Cryptography.

Thanks for the replies in advance.

Upvotes: 0

Views: 408

Answers (1)

Mr_Folo
Mr_Folo

Reputation: 11

Managed to figure out the issue. i needed to add:

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

Upvotes: 1

Related Questions