gpt4 book ai didi

java - 查找加密 key 中丢失的字节 : Java

转载 作者:行者123 更新时间:2023-12-01 21:18:17 25 4
gpt4 key购买 nike

所以我的作业的第二部分是找到加密 key 丢失的字节。作为背景,我对此不太熟悉,所以我不知道从哪里开始。我环顾四周,似乎找不到起点。我所掌握的信息是加密 key 采用 AES/ECB key : {'__' '7e' '15' '16' '28' 'ae' 'd2' 'a6' 'ab' 'f7' '15' '88' '09' 'cf' '4f' '3c'};

我所做的尝试是循环遍历 ACII 值 (0-255),然后获取值的字节并将它们附加到 key 的字节数组中。之后我会尝试正常解密文件并输出新文件以希望看到图片。然而我什么也没看到。你能指出我哪里出错了吗?

byte[] convertHexString = DatatypeConverter.parseHexBinary(key);
String newKey = new String(convertHexString);
byte[] keyByte = newKey.getBytes();
String[] asciiArray = new String[256];
FileInputStream file = new FileInputStream(path);
Cipher aesCipher = Cipher.getInstance(transformation);
for(int i = 0;i<256; i++){
arrayInts[i] = Character.toString((char)i);
byte[] b = asciiArray [i].getBytes();
byte[] result = new byte[b.length + keyByte.length];
System.arraycopy(b, 0, result, 0, b.length);
System.arraycopy(keyByte, 0, result, b.length, keyByte.length);
FileOutputStream out = new FileOutputStream("AESencrypt_view" + String.valueOf(i)+".jpg");
SecretKeySpec key1 = new SecretKeySpec(result,"AES");
aesCipher.init(Cipher.DECRYPT_MODE, key1);
CipherOutputStream outSt = new CipherOutputStream(out,aesCipher);
byte[] buf = new byte[1024];
int read;
while((read=file.read(buf))!=-1){
outSt.write(buf, 0, read);

}
//file.close();
out.flush();
outSt.flush();
}

最佳答案

您的代码有很多问题。

不要在字符串中存储二进制数据

这个

byte[] convertHexString = DatatypeConverter.parseHexBinary(key);
String newKey = new String(convertHexString);
byte[] keyByte = newKey.getBytes();

应该减少到

byte[] keyByte = DatatypeConverter.parseHexBinary(key);

注意 NullPointerException

这个

String[] asciiArray = new String[256];
...
asciiArray[i].getBytes();

实际上是一个NullPointerException,因为asciiArray[i]从未初始化。当您创建非基本类型数组时,该数组始终使用所有 null 值进行初始化。

FileInputStream 仅生成一次文件内容

你有代码

FileInputStream file = new FileInputStream(path);

在循环之外,但您正在循环内部读取文件,直到完全读取为止。问题是这仅适用于第一次迭代。在下一次迭代(i == 1)中,没有数据可读取,因此无需解密。

您应该在 for 循环之前将文件读入 byte[] 中,或者在 for 循环内初始化流,以便每次都能读取文件。

监听错误

CipherInputStreamCipherOutputStream 隐藏了一些异常。特别是,ECB 模式是一种非流分组密码模式,因此它必须具有某种填充。通常,此填充来自 PKCS#5 (= PKCS#7)。您应该直接使用 Cipher 及其 update 方法。当您完成数据写入后,您可以调用 doFinal 方法,如果 key 错误,您将收到一个异常,您可以以 255/256 的概率(大约 1)捕获该异常.

让关键迭代变得更容易

byte[] keyByte = DatatypeConverter.parseHexBinary("007e151628aed2a6abf7158809cf4f3c");

for(int i = 0; i < 256; i++){
keyByte[0] = (byte)i;
...
}

就是这样。您不需要更多内容来更改 key 的第一个字节。

<小时/>

一些示例代码:

byte[] keyByte = DatatypeConverter.parseHexBinary("007e151628aed2a6abf7158809cf4f3c");
Cipher aesCipher = Cipher.getInstance(transformation);
byte[] buf = new byte[1024];

for(int i = 0; i < 256; i++){
keyByte[0] = (byte)i;

FileInputStream inFileStream = new FileInputStream(path);
File outFile = new File("AESencrypt_view" + String.valueOf(i)+".jpg");
FileOutputStream outFileStream = new FileOutputStream(outFile);

SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
aesCipher.init(Cipher.DECRYPT_MODE, keySpec);

int read;
while((read = inFileStream.read(buf)) != -1){
outFileStream.write(aesCipher.update(buf, 0, read));
}
inFileStream.close();

try {
outFileStream.write(aesCipher.doFinal());
outFileStream.close();
}
catch(BadPaddingException e) {
// obviously a wrong key or broken ciphertext
outFileStream.close();
outFile.delete();
}
}

如果文件很小,则不必在每次迭代中一遍又一遍地读取它,可以在for循环之前读取一次。

关于java - 查找加密 key 中丢失的字节 : Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39577828/

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