McEliece (Bouncy Castle) 找回 public 钥匙
McEliece (Bouncy Castle) Getting the public key back
我目前正在尝试使用 BC 实施 McEliece 加密,但 运行 遇到了一些麻烦。我目前有能力创建密钥并将它们放入文件中,我可以将它们读回到程序中,但无法让它从字节返回到 Public 密钥。
以下是我目前拥有的:
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 into 或 Byte array into.
抱歉,如果这是一个简单的问题,我对编码密码学还很陌生。
提前感谢您的回复。
设法找出问题所在。我需要添加:
Security.addProvider(new BouncyCastleProvider());
Security.addProvider(new BouncyCastlePQCProvider());
我目前正在尝试使用 BC 实施 McEliece 加密,但 运行 遇到了一些麻烦。我目前有能力创建密钥并将它们放入文件中,我可以将它们读回到程序中,但无法让它从字节返回到 Public 密钥。
以下是我目前拥有的:
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 into 或 Byte array into.
抱歉,如果这是一个简单的问题,我对编码密码学还很陌生。
提前感谢您的回复。
设法找出问题所在。我需要添加:
Security.addProvider(new BouncyCastleProvider());
Security.addProvider(new BouncyCastlePQCProvider());