The data does not match the block size in the code name BouncyCastle (without filling)

I am trying to encrypt pinblock ISO-0 using codenameone BouncyCastle lib. To do this, I use the following methods:

private static byte[] performEncrypt(byte[] key, String plainText, boolean padding) { byte[] ptBytes = plainText.getBytes(); BufferedBlockCipher cipher; if (padding) { cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); } else { cipher = new BufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); } cipher.init(true, new KeyParameter(key)); byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)]; int oLen = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0); try { cipher.doFinal(rv, oLen); } catch (CryptoException ce) { LoggingUtil.error(TAG, ce, "Unexpected Exception"); } return rv; } private static String createIso0PinBlock(String pin, String number) { ... } private static String getPaddedData(String data, byte padCharacter) { String paddedData = ByteUtil.pad(data, (char) padCharacter, 8).toString(); return paddedData; } public static String createPinBlockAndEncrypt(String pin, String number) { LoggingUtil.debug("SecurityUtil", "CREAT PIN BLOCK AND ENCRYPT.. PIN: " + pin + " NUMBER: " + number); String pb = createIso0PinBlock(pin, number.substring(0, number.length() - 1)); LoggingUtil.debug("SecurityUtil", "PINBLOCK: " + pb); String padded = getPaddedData(pb, (byte) 0x00); LoggingUtil.debug("SecurityUtil", "PADDED: " + padded); byte[] encrypted = performEncrypt(Hex.decode(KEY.getBytes()), new String(ByteUtil.hex2byte(padded)), false); return ByteUtil.byte2hex(encrypted); } 

In ByteUtil :

 public static StringBuilder pad(String data, char padCharacter, int multiplier) { StringBuilder text = new StringBuilder(); text.append(data); while (text.length() % multiplier != 0) { text.append(padCharacter); } return text; } 

To give an example of log outputs:

 [SecurityUtil] CREAT PIN BLOCK AND ENCRYPT.. PIN: 2255 NUMBER: 6284734104205417486 [SecurityUtil] PINBLOCK: 042214FBDFABE8B7 [SecurityUtil] PADDED: 042214FBDFABE8B7 

When I run it using the public static void main method, it works as expected, however, when I create it for Android via Codenameone, I get the following error in logcat:

 org.bouncycastle.crypto.DataLengthException: data not block size aligned org.bouncycastle.crypto.BufferedBlockCipher.doFinal(BufferedBlockCipher.java:275) 

Despite the fact that the laid hopper has a length of 16 (a multiple of 8).

Any help on this would be appreciated.

+5
source share
1 answer

Encryption works with binary data, and your pinblock is binary , so keep it that way.

When you call performEncrypt(..) you convert the hex encoded pinblock to a string with new String(ByteUtil.hex2byte(padded)) , and inside performEncrypt(...) you convert it to an byte array with byte[] ptBytes = plainText.getBytes(); . The problem is that not all byte sequences can correctly be displayed back and forth along the line, and you can get different data and even different lengths, etc. look at here

Change your signature on you performEncrypt(..) to:

 private static byte[] performEncrypt(byte[] key, byte[] plainText, boolean padding) { 

and don't recode the string at all.

+2
source

All Articles