对于作业,我们必须解密5个加密的图片文件。 这项任务给了我们关键:{“2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c”}:带有填充文件的参数和 1.文件大小以8个字节表示,其中第一个字节是最重要的字节。 2.填充的大小是使[file_size + padding_size + 8]为16的倍数所需的字节数。 3.为加密传递的字节是:original_file,padding,file_size。
以下是我的代码:
enter code here public class AESEncryption {
public static String algo = "AES";
public static String transformation = "AES/ECB/NoPadding";
public static String key = "2b7e151628aed2a6abf7158809cf4f3c";
public static String path = "AESencrypt_1.jpg";
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException {
//encrypt();
decrypt();
bytes();
}
private static void encrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException{
Cipher aesCipher = Cipher.getInstance(transformation);
byte[] b = key.getBytes();
SecretKeySpec key = new SecretKeySpec(b,"AES");
aesCipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream is = new FileInputStream(path);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream("ecrypted.jpg"), aesCipher);
copy(is, os);
os.close();
}
`
private static void decrypt()throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException{
byte[] b = key.getBytes();
//System.out.println(Arrays.toString(data));
//CipherInputStream is = new CipherInputStream(new FileInputStream("AESencrypt_1.jpg"), aesCipher);
//CipherOutputStream os = new CipherOutputStream("decPic.jpg");
FileInputStream file = new FileInputStream(path);
FileOutputStream out = new FileOutputStream("Decrypted.jpg");
Cipher aesCipher = Cipher.getInstance(transformation);
SecretKeySpec key1 = new SecretKeySpec(b,"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();
}
private static void copy(InputStream cin, OutputStream fos) throws IOException {
// TODO Auto-generated method stub
int i;
byte[] b = new byte[1024];
while((i=cin.read(b))!=-1) {
fos.write(b, 0, i);
}
}
}
我的代码接受输入流并保存新的输出文件,但图像仍然不可读。我不确定我是不是以正确的方式传递密钥,或者我是否需要移除填充以便查看图像。如果我这样做,你能指出我正确的方向吗?谢谢。