我目前正在尝试使用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或字节数组更改为它。
抱歉,如果这是一个简单的问题,我对编码密码学相当陌生。
感谢您的提前回复。
发布于 2017-02-21 03:24:47
设法找出问题所在。我需要补充的是:
Security.addProvider(new BouncyCastleProvider());
Security.addProvider(new BouncyCastlePQCProvider());https://stackoverflow.com/questions/42350016
复制相似问题