当length可被16整除时,CipherInputStream会跳过最后一个字节

时间:2014-06-09 13:28:10

标签: java encryption aes

我一直在玩Java加密,我遇到了奇怪的行为。使用

加密byte[] data
InputStream fin = new ByteArrayInputStream(data);
CipherInputStream cin = new CipherInputStream(fin, mEcipher);

其中mEcipher是一个初始化良好的AES密码,当数组的长度是16的倍数时,它总是除了。然后加密会跳过最后16个字节。我必须通过添加额外的16 0来解决这个问题,这不是意图,因为加密是按照16字节的块完成的,因此不需要最后的字节为0。

下面是一个演示行为的小例子。为什么会发生这种情况,我该如何解决?它只发生在加密,解密工作正常(并且长度始终是16的倍数)。例程encrypt()decrypt()完全对称。我认为这在CipherInputStream中是一个奇怪的怪癖,但我想了解它的细节。

class Crypto {


String mPassword = null;
public final static int SALT_LEN = 8;
byte[] mInitVec = null;
byte[] mSalt = null;
Cipher mEcipher = null;
Cipher mDecipher = null;
private final int KEYLEN_BITS = 128; // see notes below where this is used.
private final int ITERATIONS = 65536;
private final int MAX_FILE_BUF = 1024;

public Crypto(String password) {mPassword = password;}
public byte[] getSalt() {return (mSalt);}
public byte[] getInitVec() {return (mInitVec);}

public void setupEncrypt() throws Exception {
    mSalt = new byte[SALT_LEN];
    SecureRandom rnd = new SecureRandom();
    rnd.nextBytes(mSalt);
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(mPassword.toCharArray(), mSalt, ITERATIONS, KEYLEN_BITS);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    mEcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    mEcipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = mEcipher.getParameters();
    mInitVec = params.getParameterSpec(IvParameterSpec.class).getIV();
}

public void setupDecrypt(String initvec, String salt) throws Exception {
    mSalt = decodeHex(salt.toCharArray());
    mInitVec = decodeHex(initvec.toCharArray());
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(mPassword.toCharArray(), mSalt, ITERATIONS, KEYLEN_BITS);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    mDecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec));
}

public byte[] decrypt(byte[] data) throws IllegalBlockSizeException, BadPaddingException, IOException {
    byte[] decdata = new byte[data.length];
    int totalread = 0;
    int nread = 0;
    byte[] substr = new byte[16];
    InputStream fin = new ByteArrayInputStream(data);
    CipherInputStream cin = new CipherInputStream(fin, mDecipher);
    while ((nread = cin.read(substr)) > 0) {
        for (int i = 0; i < nread; i++) decdata[totalread+i] = substr[i];
        totalread += nread;
    }
    fin.close();
    return decdata;
}

public byte[] encrypt(byte[] data) throws IllegalBlockSizeException, BadPaddingException, IOException {
    System.out.println("data.length="+data.length);
    byte[] encdata = new byte[data.length+15-(data.length-1)%16];
    System.out.println("encdata.length="+encdata.length);
    int totalread = 0;
    int nread = 0;
    byte[] substr = new byte[16];
    InputStream fin = new ByteArrayInputStream(data);
    CipherInputStream cin = new CipherInputStream(fin, mEcipher);
    while ((nread = cin.read(substr)) > 0 && totalread<data.length) {
        for (int i = 0; i < nread; i++) encdata[totalread+i] = substr[i];
        totalread += nread;
    }
    fin.close();
    return encdata;
}

public static void main(String[] args) throws Exception {
    String inpstr = "Dit is een test.Zit if een mewt.";

    Crypto en = new Crypto("mypassword");
    en.setupEncrypt();
    String iv = encodeHexString(en.getInitVec()).toUpperCase();
    String salt = encodeHexString(en.getSalt()).toUpperCase();
    byte[] inp = inpstr.getBytes();
    byte[] enc = en.encrypt(inp);
    System.out.println("In: "+Arrays.toString(inp));
    System.out.println("En: "+Arrays.toString(enc));

    Crypto dc = new Crypto("mypassword");
    dc.setupDecrypt(iv, salt);
    byte[] oup = dc.decrypt(enc);
    System.out.println("En: "+Arrays.toString(enc));
    System.out.println("Ou: "+Arrays.toString(oup));
}

public static final String DEFAULT_CHARSET_NAME = "UTF_8";
private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

private static byte[] decodeHex(char[] data) {
    int len = data.length;
    if ((len & 0x01) != 0) {
        throw new UnsupportedOperationException("Odd number of characters.");
    }
    byte[] out = new byte[len >> 1];

    // two characters form the hex value.
    for (int i = 0, j = 0; j < len; i++) {
        int f = toDigit(data[j], j) << 4;
        j++;
        f = f | toDigit(data[j], j);
        j++;
        out[i] = (byte) (f & 0xFF);
    }

    return out;
}

private static char[] encodeHex(byte[] data) {
    return encodeHex(data, true);
}

private static char[] encodeHex(byte[] data, boolean toLowerCase) {
    return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}

private static char[] encodeHex(byte[] data, char[] toDigits) {
    int l = data.length;
    char[] out = new char[l << 1];
    // two characters form the hex value.
    for (int i = 0, j = 0; i < l; i++) {
        out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
        out[j++] = toDigits[0x0F & data[i]];
    }
    return out;
}

private static String encodeHexString(byte[] data) {
    return new String(encodeHex(data));
}

private static int toDigit(char ch, int index) {
    int digit = Character.digit(ch, 16);
    if (digit == -1) {
        throw new UnsupportedOperationException("Illegal hexadecimal character " + ch + " at index " + index);
    }
    return digit;
}

}

1 个答案:

答案 0 :(得分:1)

您正在使用PKCS#5填充,这意味着您的输出数据始终大于输入数据。如果输入数据是块对齐的(即16个字节的倍数),则会增加16个字节的填充。您的代码假设它们的长度相等。

我已经纠正并简化了您的加密和解密方法。使用ByteArrayOutputStream可以避免在两种情况下都需要知道输出的预期大小。我冒昧地使用try-with-resources语句,如果您使用的是Java 6或更低版本,则必须编辑它们。

public byte[] decrypt(byte[] data) throws IllegalBlockSizeException,
    BadPaddingException, IOException {

  ByteArrayOutputStream bos = new ByteArrayOutputStream();    
  int nread = 0;
  byte[] substr = new byte[16];
  try (InputStream fin = new ByteArrayInputStream(data);
      CipherInputStream cin = new CipherInputStream(fin, mDecipher)) {
    while ((nread = cin.read(substr)) > 0) {
      bos.write(substr, 0, nread);
    }

    return bos.toByteArray();
  }
}

public byte[] encrypt(byte[] data) throws IllegalBlockSizeException,
    BadPaddingException, IOException {
  System.out.println("data.length=" + data.length);
  ByteArrayOutputStream bos = new ByteArrayOutputStream();

  int nread = 0;
  byte[] substr = new byte[16];
  try (ByteArrayInputStream fin = new ByteArrayInputStream(data);
      CipherInputStream cin = new CipherInputStream(fin, mEcipher)) {
    while ((nread = cin.read(substr)) > 0) {
      bos.write(substr, 0, nread);
    }
    return bos.toByteArray();
  }
}