Java Cipher - AES Padding Problem

非 Y 不嫁゛ 提交于 2019-12-06 02:49:05

问题


I am using a AES cipher with a 16 byte block size.

If I try and encrypt a 16 byte string I have no problems, but any other length not a multiple of 16 is throwing an exception.

I know with 3-DES you can specify a padding type as part of the algorithm and it's handled with no extra work (e.g. DES/CBC/PKCS5Padding), but is there a way to specify this with AES?

Or do I need to pad the pytes manually to a multiple of 16, and then strip them when I decrypt? Here is an abbreviated code sample.

encrypt = Cipher.getInstance("AES", provider);
encrypt.init(Cipher.ENCRYPT_MODE, key) ;
byte[] encrypted = encrypt.doFinal(plainTxt.getBytes()) ;

Any and all replies appreciated!

Thanks in advance, Ciarán


回答1:


It should work exactly the same with AES, i.e. the padding mode has to be specified together with the cipher. Which padding modes are implemented depends on the provider and should be described in its documentation.

According to the JCE documentation: http://java.sun.com/j2se/1.5.0/docs/guide/security/jce/JCERefGuide.html#AppA standard padding modes like PKCS5Padding should be always supported (at least, that's how I interpret it).



来源:https://stackoverflow.com/questions/340820/java-cipher-aes-padding-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!