【发布时间】:2017-02-20 16:47:16
【问题描述】:
我目前正在尝试使用 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 for McEliece,但没有成功,因为这些方法要么受到保护,要么不允许使用 KeySpec,并且想要 SubjectPublicKeyInfo,我无法弄清楚如何将 KeySpec 更改为字节数组进入。
对不起,如果这是一个简单的问题,我对密码学编码相当陌生。
感谢您提前回复。
【问题讨论】:
标签: java security cryptography bouncycastle public-key-encryption