2016-07-22 51 views
5

我试图使用codenameone BouncyCastle lib加密ISO-0 pinblock。 我使用达到此目的的方法如下:在codenameone BouncyCastle(无填充)中对齐数据块大小

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); 
} 

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; 
} 

,得到实施例日志输出:

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

当我通过public static void main方法运行该,但它的工作方式与预期的一样,但是,当我通过Codenameone为Android构建此应用程序时,logcat中出现以下错误:

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

尽管填充的pinblock的长度是16(8的倍数)。

有关这个问题的任何帮助,将不胜感激。

回答

2

加密工作在二进制数据以及pinblock是二进制,因此保持这种方式。

当调用performEncrypt(..)您在十六进制编码pinblock转换为字符串new String(ByteUtil.hex2byte(padded))了,里面performEncrypt(...)将其转换为一个字节数组byte[] ptBytes = plainText.getBytes();。这里的问题是,并不是所有的字节序列可以正确地来回串映射,你可能最终得到不同的数据,甚至不同的长度等take a look here

更改您performEncrypt(..)到的签名:

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

并避免完全转换为字符串。

+0

很棒的回答。我们会避免在Codename One中使用字符串作为二进制数据,因为我们将字符串映射到本地字符串,例如在iOS上,它们在某些极端边缘情况下可能会有轻微的行为差异。例如。我们在整合zip支持https://www.codenameone.com/blog/zip-and-toast.html时遇到了问题 –