McEliece(Bouncy Castle)获取公钥

时间:2017-02-20 16:47:16

标签: java security cryptography bouncycastle public-key-encryption

我目前正在尝试使用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为McEliece包含的KeyFactory但没有成功,因为这些方法受到保护或者不允许KeySpec并且想要SubjectPublicKeyInfo,我无法弄清楚如何将KeySpec更改为Byte数组成。

很抱歉,如果这是一个简单的问题,对于编码密码学来说还是一个新手。

感谢您提前回复。

1 个答案:

答案 0 :(得分:1)

管理以找出问题所在。我需要补充一下:

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