有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java数据块大小未在codenameone BouncyCastle中对齐(无填充)

我正在尝试使用codenameone BouncyCastle库加密ISO-0 pinblock。 我使用的方法如下:

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)

尽管加垫的钉块长度为16(8的倍数)

任何关于这个问题的帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    加密工作在二进制数据上,而你的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) {
    

    避免完全通过字符串转换