У меня есть свой Symmetri c, созданный с помощью следующей команды
openssl rand 32 > test.key
И он зашифрован с помощью моего ключа publi c, как показано ниже. Он использует режим заполнения OAEP.
openssl pkeyutl -pkeyopt rsa_padding_mode:oaep -encrypt -inkey public.key -pubin -in test.key -out test.key.enc
Но когда я пытаюсь расшифровать, используя свой закрытый ключ, он дает мне ошибку Badding.
Мой Java Код
// few imports
private static PrivateKey getPrivateKey(final String privateKeyFile)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
final KeyFactory keyFactory = KeyFactory.getInstance(PayboxUtil.ENCRYPTION_ALGORITHM);
final PemReader reader = new PemReader(new FileReader(privateKeyFile));
final byte[] pubKey = reader.readPemObject().getContent();
final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(pubKey);
return keyFactory.generatePrivate(spec);
}
public static byte[] decryptRandomKey(final byte[] encryptedKey, final String private_key_file)
throws NoSuchProviderException {
try {
final Key privKey = getPrivateKey(private_key_file);
final byte[] ciphertext = encryptedKey;
final Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
final OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-512", "MGF1",
new MGF1ParameterSpec("SHA-1"), PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privKey, oaepParams);
final byte[] symmetricKey = cipher.doFinal(ciphertext);
return symmetricKey;
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}