I'm trying to use RSA to encrypt and decrypt a json string.Method encrypt is good,also the encryptJSON. Everthing good until
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
and e = "javax.cryppto.BadPaddingException:Decrption error" Here is the source code.
public class RSAEncryptJSON {
public static void main(String[] args) {
try {
String jsonData = "{\"key1\": \"value1\", \"key2\": \"value2\"}";
KeyPair keyPair = generateKeyPair();
String encryptedJSON = encryptJSON(jsonData, keyPair.getPublic());
System.out.println("Encrypted JSON: " + encryptedJSON);
String decryptedJSON = decryptJSON(encryptedJSON, keyPair.getPrivate());
System.out.println("Decrypted JSON: " + decryptedJSON);
} catch (Exception e) {
e.printStackTrace();
}
}
private static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
public static String encryptJSON(String jsonData, java.security.PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
StringBuilder encryptedDataBuilder = new StringBuilder();
int blockSize = 245;
for (int i = 0; i < jsonData.length(); i += blockSize) {
int endIndex = Math.min(i + blockSize, jsonData.length());
String encryptedChunk = encrypt(jsonData.substring(i, endIndex), publicKey);
encryptedDataBuilder.append(encryptedChunk);
}
return encryptedDataBuilder.toString();
}
public static String decryptJSON(String encryptedJSON, java.security.PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
StringBuilder decryptedDataBuilder = new StringBuilder();
int blockSize = 256;
for (int i = 0; i < encryptedJSON.length(); i += blockSize) {
int endIndex = Math.min(i + blockSize, encryptedJSON.length());
String decryptedChunk = decrypt(encryptedJSON.substring(i, endIndex), privateKey);
decryptedDataBuilder.append(decryptedChunk);
}
return decryptedDataBuilder.toString();
}
private static String encrypt(String input, java.security.PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("RSA");
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
private static String decrypt(String input, java.security.PrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte[] encryptedBytes = Base64.getDecoder().decode(input);
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("RSA");
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
}
Am I did something wrong?