gpt4 book ai didi

java - 当长度可被 16 整除时,CipherInputStream 跳过最后一个字节

转载 作者:行者123 更新时间:2023-11-30 11:17:54 25 4
gpt4 key购买 nike

我一直在研究 Java 中的加密,并且遇到了奇怪的行为。使用

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

其中 mEcipher 是经过良好初始化的 AES 密码,当数组的长度是 16 的倍数时,它始终有效除外。然后加密会跳过最后 16 个字节。我必须通过添加额外的 16 个 0 来解决它,这不是本意,因为加密是按 16 字节的 block 完成的,因此不需要最后一个字节为 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;
}

}

最佳答案

您正在使用 PKCS #5 填充,这意味着您的输出数据将始终大于您的输入数据。如果您的输入数据是 block 对齐的(即十六字节的倍数),您将添加十六字节的填充。您的代码假定它们的长度相等。

我已经更正并简化了您的加密和解密方法。使用 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();
}
}

关于java - 当长度可被 16 整除时,CipherInputStream 跳过最后一个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24121346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com