java - java中解密加密的二进制文件(AES_CTR模式)

标签 java python pycrypto

我知道这个问题以前曾被问过。我只需要一个指示来完成这些代码。如果有人能指出我的代码中的问题,那将非常有帮助

这是解密的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 中找到了一些想法。但不太能理解。

最佳答案

您配置此密码的方式非常不安全。 请勿使用此代码。

通过设置:

counter = lambda : iv

在 Python 中初始化 AES 密码时,您已强制 AES-CTR 对每个 block 使用完全相同的计数器。这将密码从 AES-CTR 简化为简单的滚动 XOR。观察:

>>> key = "abcd1234abcd1234"
>>> 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)
>>> encryptor.encrypt("example block 01")
'\xda\x7f\x00\xbdp\x1c\xe4\xf3=|\x88\xe2\xe5%\x80\xab'
>>> encryptor.encrypt("example block 01")
'\xda\x7f\x00\xbdp\x1c\xe4\xf3=|\x88\xe2\xe5%\x80\xab'
>>> encryptor.encrypt("example block 02")
'\xda\x7f\x00\xbdp\x1c\xe4\xf3=|\x88\xe2\xe5%\x80\xa8'

请注意:

  1. 重复加密同一 block 会产生相同的输出。

  2. 加密相似的 block 会产生相似的输出。 (从技术上讲,a XOR b = E(a) XOR E(b)。)

要解决此问题,您需要使用已导入的 Counter 类:

encryptor = AES.new(key, AES.MODE_CTR, counter=Counter.new(128))

正确配置 AES-CTR 后,您现有的 Java 代码应该能够解密它。

但是请注意,如果重复使用同一 key 来加密多个文件,此模式仍然不安全。您必须为每个文件使用不同的 key 。

关于java - java中解密加密的二进制文件(AES_CTR模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52909754/

相关文章:

python - Python 2.7 "wide-build"usc4 是否与某些库不兼容?

python - 解密通过其他来源加密的 3DES 数据

java - 如何检索保存为 blob 的图像

java - Apache POI 或 java.io 是否支持非英文字符?

java - 将两个 byteBuffer 连接成一个

python - 如何从与当前登录用户关联的模型导入和编辑变量

java - JMS session 线程影响

python - Python 中的 TDD - 我们应该测试辅助函数吗?

python - 为什么 Pandas 串联 (pandas.concat) 的内存效率如此之低?

MySQL 未正确存储从 PyCrypto 库生成的密文