【发布时间】:2018-10-20 20:14:47
【问题描述】:
我知道以前有人问过这个问题。我只需要一个方向来完成这些代码。如果有人能指出我的代码中的问题,那将非常有帮助
这是用于解密的Java代码
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
class Decode{
public static void main(String []args){
try{
Decode.decrypt();
System.out.println("Decrypted");
}catch(Exception e){
System.out.println(e);
}
}
public static void decrypt() throws Exception {
byte[] initialIV;
final byte[] buf = new byte[128];
final Cipher c = Cipher.getInstance("AES/CTR/NoPadding");
final InputStream is = new FileInputStream("/home/neki/python/encVideo.mp4");
byte[] buffer = new byte[16];
is.read(buffer);
c.init(Cipher.DECRYPT_MODE,new SecretKeySpec("1234567890123456".getBytes(), "AES"),new IvParameterSpec(buffer));
final OutputStream os = new CipherOutputStream(new FileOutputStream("/home/neki/python/javaDecVideo.mp4"), c);
while (true) {
int n = is.read(buf);
if (n == -1) break;
os.write(buf, 0, n);
}
os.close(); is.close();
}
}
}
这是加密文件的python代码
import os, random, struct
from Crypto.Cipher import AES
from os import urandom
from Crypto.Util import Counter
def encrypt_file(key, in_filename, out_filename=None, chunksize=128):
if not out_filename:
out_filename = in_filename + '.enc'
iv = '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
encryptor = AES.new(key, AES.MODE_CTR, counter = lambda : iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
# outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(encryptor.encrypt(chunk))
encrypt_file("1234567890123456".encode(),"/home/neki/python/Eduaid.mp4","/home/neki/python/encVideo.mp4")
我还在stackoverflow中找到了一些想法。但不能很好理解。
【问题讨论】: