python加密解密:前16个字符是乱码

k3fezbri  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(354)

我试图解密一个用java加密的文件 AES/CBC/PKCS5Padding . 但是,我在完全解密文件时遇到了一些问题。我想我遗漏了一些简单的东西。我对加密还很陌生,所以很有可能:

import base64
import re

from Crypto import Random
from Crypto.Cipher import AES

# generate the key

passphrase = "my_secret_passphrase"
key = passphrase[0:16].encode()

# load in the encrypted file as bytes

file_name = "tbd/enc.csv"
with open(file_name, "rb") as in_file:
    encrypted_bytes = in_file.read()

# initialize the cipher

iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)

# use the cipher to decrypt the bytes

decrypted_bytes = cipher.decrypt(encrypted_bytes)

# write to an out_file

file = re.sub("\.csv", "_decoded.csv", file)
with open(file, "wb") as binary_file:
    binary_file.write(decrypted_bytes)

下面是我得到的第一行文字:

M¢p†‘GW§'tÄ%èéired Date,Employees - Id,First,Employees - Middle,Last,Employees - Preferred Name,Job Title,Employees - Marital Status,Employees - Trade Code,...

我注意到前16个字符完全是胡言乱语,所以我知道我很接近。我遗漏了什么导致前16个字符无法正确解码?奇怪的是,我们能够用java解密整个文件,所以我们知道python实现有问题。
奖励回合:用于加密文件的java代码(我既不编码也不懂java):

package rpa.ipaengine.bots.cmic.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestFile {
  private static Logger LOGGER = LoggerFactory.getLogger(TestFile.class);
  public static void main(String[] args) {
    LOGGER.info("running");
    try {
      Security.setProperty("crypto.policy", "unlimited");
      encryptedFile("my_secret_passphrase", "/tbd/enc.csv", "tbd/dec.csv");
      decryptedFile("my_secret_passphrase", "/tbd/enc.csv", "tbd/dec.csv");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void encryptedFile(String secretKey, String fileInputPath, String fileOutPath)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException,
    BadPaddingException {
    try {
      byte[] raw1 = "my_secret_passphrase"
        .getBytes();
      byte[] raw = new String(raw1, 0, 16).getBytes();
      Key skeySpec = new SecretKeySpec(raw, "AES");
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      byte[] iv = new byte[cipher.getBlockSize()];
      IvParameterSpec ivParams = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParams);
      var fileInput = new File(fileInputPath);
      var inputStream = new FileInputStream(fileInput);
      var inputBytes = new byte[(int) fileInput.length()];
      inputStream.read(inputBytes);
      var outputBytes = cipher.doFinal(inputBytes);
      var fileEncryptOut = new File(fileOutPath);
      var outputStream = new FileOutputStream(fileEncryptOut);
      outputStream.write(outputBytes);
      inputStream.close();
      outputStream.close();
      System.out.println("File successfully encrypted!");
      System.out.println("New File: " + fileOutPath);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void decryptedFile(String secretKey, String fileInputPath, String fileOutPath)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException,
    BadPaddingException, InvalidAlgorithmParameterException {
    try {
      byte[] raw1 = "my_secret_passphrase"
        .getBytes();
      byte[] raw = new String(raw1, 0, 16).getBytes();
      Key skeySpec = new SecretKeySpec(raw, "AES");
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      byte[] iv = new byte[cipher.getBlockSize()];
      IvParameterSpec ivParams = new IvParameterSpec(iv);
      cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParams);
      var fileInput = new File(fileInputPath);
      var inputStream = new FileInputStream(fileInput);
      var inputBytes = new byte[(int) fileInput.length()];
      inputStream.read(inputBytes);
      byte[] outputBytes = cipher.doFinal(inputBytes);
      var fileEncryptOut = new File(fileOutPath);
      var outputStream = new FileOutputStream(fileEncryptOut);
      outputStream.write(outputBytes);
      inputStream.close();
      outputStream.close();
      System.out.println("File successfully decrypted!");
      System.out.println("New File: " + fileOutPath);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
f0ofjuux

f0ofjuux1#

使用 bytes(16) 正如马特所说,我能够完全解读文本:

import base64
import re

from Crypto import Random
from Crypto.Cipher import AES

# generate the key

passphrase = "my_secret_passphrase"
key = passphrase[0:16].encode()

# load in the encrypted file as bytes

file_name = "tbd/enc.csv"
with open(file_name, "rb") as in_file:
    encrypted_bytes = in_file.read()

# initialize the cipher

iv = bytes(16)
cipher = AES.new(key, AES.MODE_CBC, iv)

# use the cipher to decrypt the bytes

decrypted_bytes = cipher.decrypt(encrypted_bytes)

# write to an out_file

file = re.sub("\.csv", "_decoded.csv", file)
with open(file, "wb") as binary_file:
    binary_file.write(decrypted_bytes)

文件的第一行:

Employees - Hired Date,Employees - Id,First,Employees - Middle...

但是,michael提供了一些关于用于加密文件的java代码的有效注解。我会和我的团队一起确保它更安全。

相关问题