AES encryption and decryption using IV and CBC mode

561 views
Skip to first unread message

jitendra shelar

unread,
Jun 29, 2014, 9:46:35 AM6/29/14
to play-fr...@googlegroups.com
Hi All,

I am newbie to playframework/scala. 
I am trying to implement AES algorithm encryption and decryption using IV and CBC mode.

    def decrypt(data:Array[Byte], iv:Array[Byte], key1:Array[Byte]): String={
    
     var cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
     val key = new SecretKeySpec(key1, "AES")
     cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv))

     var encry= cipher.doFinal(data)   // getting error here

     val encodedBytes =  Base64.encodeBase64(encry)
     new String(encodedBytes)
  } 

I am getting following error at cipher.doFinal(data) line during decryption.

[BadPaddingException: Given final block not properly padded]

Can some body please help me on this.
If any other way implementing AES algorithm is present, please let me know.

Thanks & Regards,
Jitendra

alex s

unread,
Jun 29, 2014, 10:37:14 AM6/29/14
to play-fr...@googlegroups.com


воскресенье, 29 июня 2014 г., 17:46:35 UTC+4 пользователь jitendra shelar написал:

I am trying to implement AES algorithm encryption and decryption using IV and CBC mode.
 
Don't.
 
Can some body please help me on this.

No.

Will Sargent

unread,
Jun 29, 2014, 12:49:24 PM6/29/14
to play-fr...@googlegroups.com
From your code example, it looks like you're using the JCE implementation of AES. There's an example here that may help


You may also want to check out this stack overflow answer:


Please don't say "implementing AES algorithm" -- you're using the algorithm to encrypt and decrypt, not implementing the algorithm itself.  The answer that Alex gave you is the correct response to that statement -- as an end user, you should never implement your own encryption algorithm, as it's almost certain to be incorrect.


Will Sargent
Consultant, Professional Services
Typesafe, the company behind Play Framework, Akka and Scala


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jitendra shelar

unread,
Jun 29, 2014, 2:27:09 PM6/29/14
to play-fr...@googlegroups.com
Thanks Will/Alex for correcting me. 
Thanks for your help.

Regards,
Jitendra

Kevin Wright

unread,
Jul 1, 2014, 4:33:19 PM7/1/14
to play-fr...@googlegroups.com
You might want to try stackoverflow

This is very much a question about Java's encryption APIs, not something specific to either Play or Scala.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Kevin Wright
mail: kevin....@scalatechnology.com
gtalk / msn : kev.lee...@gmail.com
vibe / skype: kev.lee.wright
steam: kev_lee_wright

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
Reply all
Reply to author
Forward
0 new messages