Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two things:</p> <p>No padding can only work if you use input that is an exact mulitple of your key size, which is 128 bit or 16 bytes. So in your particular case "Hello World!!!!!".getBytes() is actually a multiple of 16, but this is of course not true for arbitrary Strings.</p> <p>Use "AES/CBC/PKCS5Padding" instead to solve this issue.</p> <p>Do not turn your encrypted data into a String - this will and change the encrypted output. There's no guarantee that new String(byte[]).getBytes() returns the exact same byte array! So you should leave the encrypted data as what it is - a stream of bytes. Thus <code>encrypt</code> should return <code>byte[]</code> instead and decrypt should take <code>byte[]</code> as input - this is a working example:</p> <pre><code>public class NewClass { public static void main(String [] args) { try { String plainText = "Hello World!!!!"; String encryptionKey = "E072EDF9534053A0B6C581C58FBF25CC"; System.out.println("Before encryption - " + plainText); byte[] cipherText = encrypt(plainText, encryptionKey); System.out.println("After encryption - " + cipherText); String decrypted = decrypt(cipherText, encryptionKey); // -&gt; Hello World!!!! System.out.println("After decryption - " + decrypted); } catch (Exception e) { e.printStackTrace(); } } public static byte[] encrypt(String plainText, String passkey) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(hexStringToByteArray(passkey), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(new byte[cipher.getBlockSize()])); return cipher.doFinal(plainText.getBytes()); } public static String decrypt(byte[] cipherText, String passkey) throws Exception{ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(hexStringToByteArray(passkey), "AES"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(new byte[cipher.getBlockSize()])); return new String(cipher.doFinal(cipherText)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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