Note that there are some explanatory texts on larger screens.

plurals
  1. POException in AES decryption algorithm in java
    primarykey
    data
    text
    <p>I got an exception in the following code for AES algorithm in java.</p> <p>Code decryptes an encrypted string and returns the original string. </p> <p>Plz help me to fix this.</p> <p>Code:</p> <pre><code>public class AES { public byte[] encrypted; public byte[] original; public String originalString; public static String asHex (byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i; for (i = 0; i &lt; buf.length; i++) { if (((int) buf[i] &amp; 0xff) &lt; 0x10) strbuf.append("0"); strbuf.append(Long.toString((int) buf[i] &amp; 0xff, 16)); } return strbuf.toString(); } public String AESencryptalgo(byte[] text) { String newtext=""; // Get the KeyGenerator try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); // 192 and 256 bits may not be available // Generate the secret key specs. SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); // Instantiate the cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encrypted = cipher.doFinal(text); System.out.println("encrypted string: " + asHex(encrypted)); cipher.init(Cipher.DECRYPT_MODE, skeySpec); original = cipher.doFinal(encrypted); originalString = new String(original); System.out.println("Original string: " + originalString + " " + asHex(original)); } catch(Exception e) { } finally { newtext=new String(encrypted); System.out.println("ENCRYPTED "+newtext); //AESdecryptalgo(newtext.getBytes()); return newtext; } } public String AESdecryptalgo(byte[] text) { // Get the KeyGenerator try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); // 192 and 256 bits may not be available // Generate the secret key specs. SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); // Instantiate the cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); original = cipher.doFinal(text); //Exception occurs here originalString = new String(original); System.out.println("Original string: " + originalString + " " + asHex(original)); } catch(Exception e) { System.out.println("exception"); } finally { System.out.println("DECRYPTED "+originalString); return originalString; } } public static void main(String[] args) { AES a=new AES(); a.AESencryptalgo("hello".getBytes()); System.out.println(); }} ` </code></pre> <p>exception:</p> <pre><code>javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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