gpt4 book ai didi

java - ECB 加密的结果可能会有所不同。与CBC的结果相同

转载 作者:行者123 更新时间:2023-11-30 02:36:08 27 4
gpt4 key购买 nike

public class Symmetric1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
// TODO code application logic here
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(new SecureRandom());
SecretKey secretKey = kg.generateKey();

FileInputStream inFile = new FileInputStream("C:/Users/Administrator/Desktop/original.bmp");

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);

FileOutputStream outFile = new FileOutputStream("C:/Users/Administrator/Desktop/ECB_original.bmp");

byte[] input = new byte[64];
int bytesRead;
while((bytesRead = inFile.read(input)) != -1){
byte[] output = cipher.update(input,0,bytesRead);
if(output != null)
outFile.write(output);
}

byte[] output = cipher.doFinal();
if(output != null)
outFile.write(output);

inFile.close();
outFile.flush();
outFile.close();
}
}

我了解到 ECB 加密使图片有点模糊......但我的代码使原始图片像 CBC 加密一样加密。怎么了?

这也是 cbc 加密代码。

public class Symmetric2 {

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

FileInputStream inFile = new FileInputStream("C:/Users/Administrator/Desktop/original.bmp");
FileOutputStream outFile = new FileOutputStream("C:/Users/Administrator/Desktop/CBC_original.bmp");

KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(new SecureRandom());

SecretKey sk = kg.generateKey();

Cipher cp = Cipher.getInstance("DES/CBC/PKCS5Padding");

byte[] ivBytes = new byte[]{
0x00, 0x01,0x02, 0x03, 0x00, 0x00, 0x00, 0x01 };
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

cp.init(Cipher.ENCRYPT_MODE, sk, ivSpec); //CBC방식이므로 인자가 3개이다.

byte[] input = new byte[64]; //getBytes()
int bytesRead;
while((bytesRead = inFile.read(input)) != -1){
byte[] output = cp.update(input,0,bytesRead);
if(output != null)
outFile.write(output);
}
byte[] output = cp.doFinal();
if(output != null)
outFile.write(output);

inFile.close();
outFile.flush();
outFile.close();
}

}

最佳答案

众所周知的pinguin picture有点肮脏。它只加密位图本身,而BMP文件还包含元数据。此外,它依赖于数据的正确对齐。

如果您看到这些示例,那么您肯定不会获得预期的完美结果。你应该看这样一张图片作为例子来证明这一点。

关于java - ECB 加密的结果可能会有所不同。与CBC的结果相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43027584/

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