I'm implementing rsa and aes classes in java, but i have some trouble with the rsa implementation. Since i have two keys in asymetric encryption, with two different types, i've made two differents methods, one for each. Is there a better/cleaner way to do that?
private static byte[] doCrypto(
int cipherMode,
PublicKey publicKey,
byte[] inputBytes)
throws CryptoException {
Cipher cipher;
byte[] outputBytes;
try {
cipher = Cipher.getInstance("RSA");
cipher.init(cipherMode, publicKey);
outputBytes = cipher.doFinal(inputBytes);
} catch (NoSuchPaddingException
| NoSuchAlgorithmException
| InvalidKeyException
| BadPaddingException
| IllegalBlockSizeException ex) {
throw new CryptoException("Error doCrypto", ex);
}
return outputBytes;
}
private static byte[] doCrypto(
int cipherMode,
PrivateKey privateKey,
byte[] inputBytes)
throws CryptoException {
Cipher cipher;
byte[] outputBytes;
try {
cipher = Cipher.getInstance("RSA");
cipher.init(cipherMode, privateKey);
outputBytes = cipher.doFinal(inputBytes);
} catch (NoSuchPaddingException
| NoSuchAlgorithmException
| InvalidKeyException
| BadPaddingException
| IllegalBlockSizeException ex) {
throw new CryptoException("Error doCrypto", ex);
}
return outputBytes;
}