dcsimg
AES Encryption throws Bad Padding Exception
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Amatare_Oki
Posted On:   Tuesday, September 14, 2004 07:54 AM

I am writing a class to encrypt licencing information onto a text file in Base64 encoding. This write aspect works fine but the reading the licence back throw a Bad Padding Exception. (error and code below) Any assistance will be much appreciated javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_h.b(DashoA6275) at com.sun.crypto.provider.SunJCE_h.b(DashoA6275) at com.sun.crypto.provider.AESCipher.engineDoFinal (DashoA6275) at javax.crypto.Cipher.doFinal(DashoA6275) at Encryptor.encrypt(Encryptor.java:117) at Encryptor.loadAllEncryptedLayerLicences (Encryptor.java:158) at    More>>

I am writing a class to encrypt licencing information onto a text file in Base64 encoding. This write aspect works fine but the reading the licence back throw a Bad Padding Exception. (error and code below)

Any assistance will be much appreciated


javax.crypto.BadPaddingException: Given final block not properly padded

at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)

at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)

at com.sun.crypto.provider.AESCipher.engineDoFinal
(DashoA6275)

at javax.crypto.Cipher.doFinal(DashoA6275)

at Encryptor.encrypt(Encryptor.java:117)

at Encryptor.loadAllEncryptedLayerLicences
(Encryptor.java:158)

at Encryptor.main(Encryptor.java:183)



KeyGenerator kgen = KeyGenerator.getInstance("AES");

kgen.init(128);

SecretKey skey = kgen.generateKey();

byte[] raw = skey.getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES");



//encrypt

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

byte[] encrypted = cipher.doFinal(value.getBytes());

//CONVERT TO BASE64 Encoding

String s = new sun.misc.BASE64Encoder().encode(encrypted);

//write the encrypted data to the file

fos = new FileOutputStream("C:\aes\output.properties");

new PrintStream(fos).println("layerhandle "+s);


fos.flush();

//fos.close();



//decrypt
byte[] buf = new sun.misc.BASE64Decoder().decodeBuffer(value);

cipher.init(Cipher.DECRYPT_MODE, skeySpec);

//read from file and decrypt the values

byte[] unecrypted = cipher.doFinal(buf);

String word = new String(unecrypted);

   <<Less
About | Sitemap | Contact