Note that there are some explanatory texts on larger screens.

plurals
  1. POjavax.crypto.BadPaddingException: Data must start with zero” exception. Why does it occur?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/6483181/javax-crypto-badpaddingexception-data-must-start-with-zero-exception">&ldquo;javax.crypto.BadPaddingException: Data must start with zero&rdquo; exception.</a> </p> </blockquote> <p>I am trying to separate the usage of the encrypt and decrypt methods into two different classes. However, I encountered the abovementioned exception while I was decrypting a string.</p> <p>Below is my code:</p> <pre><code>import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; public class EncryptAndDecrypt { /** * */ public static Cipher createCipher () throws Exception{ Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); return cipher; } public static KeyPair generateKey () throws NoSuchAlgorithmException{ KeyPairGenerator keyGen = KeyPairGenerator.getInstance ("RSA"); keyGen.initialize(1024); KeyPair key = keyGen.generateKeyPair(); return key; } public static byte [] encrypt (String str, Cipher cip, KeyPair key) { byte [] cipherText = null; try { byte [] plainText = str.getBytes("UTF8"); cip.init(Cipher.ENCRYPT_MODE, key.getPublic()); cipherText = cip.doFinal(plainText); } catch (Exception e) { e.printStackTrace(); } return cipherText; } public static String decrypt (byte [] c, Cipher cip, KeyPair key) throws Exception { cip.init(Cipher.DECRYPT_MODE, key.getPrivate()); byte [] decryptedPlainText = cip.doFinal (c);// exception occurred here String decryptedPlainStr = new String (decryptedPlainText, "UTF8"); return decryptedPlainStr; } } // A separate class that uses the encrypt method in EncryptAndDecrypt public class EncryptionApp { public static void main (String [] args) { System.out.println (getEncrypted()); } public static byte [] getEncrypted () { byte [] encyptedByte = null; try { String plainText = "hello"; Cipher cip = EncryptAndDecrypt.createCipher(); KeyPair key = EncryptAndDecrypt.generateKey(); encyptedByte = EncryptAndDecrypt.encrypt(plainText, cip, key); String str = new String (encyptedByte, "UTF8"); System.out.println (str); } catch (Exception e) { e.printStackTrace(); } return encyptedByte; } } public class DecryptedApp { /** * A separate class to use the decrypt method in EncryptAndDecrypt */ public static void main(String[] args) { System.out.println (useDecrypted ()); } public static String useDecrypted () { String decryptedText = null; try { Cipher cip = EncryptAndDecrypt.createCipher(); KeyPair key = EncryptAndDecrypt.generateKey(); byte [] encrypted = EncryptionApp.getEncrypted(); System.out.println (encrypted); decryptedText = EncryptAndDecrypt.decrypt(encrypted, cip, key) ; } catch (Exception e) { e.printStackTrace(); } return decryptedText; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload