Java ANSI X923 Padding

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 16:45:44

If you use Bouncy Castle JCE, it supports X923 padding. You can get cipher like this (assuming you use CBC mode),

cipher = Cipher.getInstance("DESede/CBC/X9.23PADDING");

I don't think Sun's JCE supports it. But you can simply decrypt it without padding and remove padding yourself. With X9.23, the last byte is the number of padding added. So you can do something like this,

cipher = Cipher.getInstance("DESede/CBC/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
int outSize = cipher.getOutputSize(cipherText.length);  
plainText = new byte[outSize];
length = cipher.update(cipherText, plainText, 0);
cipher.doFinal(plainText, length);

//Remove padding
int newLen = plainText.length - (plainText[plainText.length-1] & 0xFF);
byte[] data = new byte[newLen];
System.arraycopy(plainText, 0, data, 0, newLen);

The "Cipher Algorithm Padding" section in Sun's document on JCA Standard Algorithm Names contains no mention of that padding scheme so it seems that it is unsupported. That being said, Bouncy Castle provides an implementation of X9.23 padding that can be used directly if you are able to use an external library and venture out of the confines of JCA.

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