1

I am trying a program to first encrypt and decrypt a string and in between encoding it into 64base and then decoding it into 64base(this is required). But I am getting the below error. What is the possible fix?

Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
    at java.base/sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:378)
    at java.base/sun.security.rsa.RSAPadding.unpad(RSAPadding.java:290)
    at java.base/com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:366)
    at java.base/com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:392)
    at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2202)
    at CryptographyExample.decrypt(encryt_decrypt.java:53)
    at CryptographyExample.main(encryt_decrypt.java:88)

My code

class CryptographyExamples {

    private static final String ALGORITHM = "RSA";

    public static byte[] encrypt(byte[] publicKey, byte[] inputData) throws Exception {
        PublicKey key = KeyFactory.getInstance(ALGORITHM).generatePublic(new X509EncodedKeySpec(publicKey));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedBytes = cipher.doFinal(inputData);
        return encryptedBytes;
    }

    public static byte[] decrypt(byte[] privateKey, byte[] inputData) throws Exception {
        PrivateKey key = KeyFactory.getInstance(ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(privateKey));
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(inputData);
        return decryptedBytes;
    }

    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        // 512 is keysize
        keyGen.initialize(512, random);
        KeyPair generateKeyPair = keyGen.generateKeyPair();
        return generateKeyPair;
    }

    public static MessageDigest md;

    public static void main(String[] args) throws Exception {
        String originalMessage = "The message to be encrypted and sent";
        md = MessageDigest.getInstance("SHA-256");
        KeyPair generateKeyPair = generateKeyPair();
        byte[] publicKey = generateKeyPair.getPublic().getEncoded();
        byte[] privateKey = generateKeyPair.getPrivate().getEncoded();
        byte[] encryptedData = encrypt(publicKey, originalMessage.getBytes());
        byte[] shaEncryptedData = md.digest(encryptedData);
        String shaEncryption64 = Base64.getEncoder().encodeToString(shaEncryptedData);
        byte[] decryptedData = decrypt(privateKey, Base64.getDecoder().decode(shaEncryption64));
        System.out.println("Decrypted Message: " + new String(decryptedData));
    }
}
barbsan
  • 3,418
  • 11
  • 21
  • 28
  • 1
    Why are you making a SHA-256 digest of your encrypted data, and calling it shaEncryptedData? SHA-256 is a One-way-hash, not encryption. This basically destroys your encrypted data. – Ebbe M. Pedersen Sep 04 '19 at 11:29
  • I have to implement a simple signature method here. So in the main program, I have both the encrypted message and SHA-256 hash of the encrypted message. Later on, I need these both to check if the message received by the receiver has been unaltered and not the actual message which comes from decrypting the encrypted message. – Ayush Chaurasia Sep 04 '19 at 14:41
  • But you try to decrypt your SHA-256 hash, not your encrypted message .. – Ebbe M. Pedersen Sep 05 '19 at 02:14

0 Answers0