我使用以下内容来加密Java中的文件:


   public static byte[] hexStringToByteArray(String s) {

      int len = s.length();
      byte[] data = new byte[len / 2];
      for (int i = 0; i < len; i += 2)
      {   
         data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
      }
      return data;

   } 

   public static void encExtract ( String zipName, String encKey )
   {

      try{
         byte[] KeyByte = hexStringToByteArray( encKey );
         SecretKey secKey =  new SecretKeySpec(KeyByte, 0, KeyByte.length, "AES");

         Cipher desCipher;
         desCipher = Cipher.getInstance("AES");

         File zipFile = new File(zipName);
         byte[] indata = java.nio.file.Files.readAllBytes(zipFile.toPath());
         System.out.println("             Encyption size " + indata.length );

         desCipher.init(Cipher.ENCRYPT_MODE, secKey);
         byte[] textEncrypted = desCipher.doFinal(indata);

         try (FileOutputStream outputStream = new FileOutputStream(zipName+".enc")) {
               outputStream.write(textEncrypted);
               System.out.println("             Encypted size " + textEncrypted.length );
         }

      }catch(Exception e)
      {
         e.printStackTrace(System.out);
      }
   }

加密文件将发送到的系统将使用Python对其进行解密.我正在try 测试这一点并构建了以下Python脚本:

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES


encFH = open('the_enc_file.zip.enc', 'rb')
encData = bytearray(encFH.read())

encKey = "__hello123__world123__again123__"

iv = encData[:AES.block_size]
cipher = AES.new(encKey, AES.MODE_CBC, iv)
decdata = cipher.decrypt(encData[AES.block_size:])

f = open("the_dec_file.zip", "wb")
f.write(decdata)
f.close()

当我运行上面的Python时,我得到错误:

Traceback (most recent call last):
  File "thescript.py", line 13, in <module>
    cipher = AES.new(encKey, AES.MODE_CBC, iv)
  File "/home/xxx/.local/lib/python3.10/site-packages/Crypto/Cipher/AES.py", line 228, in new
    return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "/home/xxx/.local/lib/python3.10/site-packages/Crypto/Cipher/__init__.py", line 79, in _create_cipher
    return modes[mode](factory, **kwargs)
  File "/home/xxx/.local/lib/python3.10/site-packages/Crypto/Cipher/_mode_cbc.py", line 274, in _create_cbc_cipher
    cipher_state = factory._create_base_cipher(kwargs)
  File "/home/xxx/.local/lib/python3.10/site-packages/Crypto/Cipher/AES.py", line 100, in _create_base_cipher
    result = start_operation(c_uint8_ptr(key),
  File "/home/xxx/.local/lib/python3.10/site-packages/Crypto/Util/_raw_api.py", line 248, in c_uint8_ptr
    raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type <class 'str'> cannot be passed to C code

我想也许与Python中的AES.Mode_CBC和Java中的Cipher. ENSYS PT_Mode不匹配,但我不确定.

推荐答案

AES加密算法期望密钥以字节为单位,但您传递的是字符串作为密钥.试试这个:

encKey ="hello123__world123__again123".encode(' utf-8 ')

with open('the_enc_file.zip.enc', 'rb') as encFH:
    encData = encFH.read()

iv = encData[:AES.block_size]

cipher = AES.new(encKey, AES.MODE_CBC, iv)

decdata = cipher.decrypt(encData[AES.block_size:])

with open("the_dec_file.zip", "wb") as f:
    f.write(decdata)

Python相关问答推荐

如何让 turtle 通过点击和拖动来绘制?

max_of_three使用First_select、second_select、

类型错误:输入类型不支持ufuncisnan-在执行Mann-Whitney U测试时[SOLVED]

如何找到满足各组口罩条件的第一行?

Pandas - groupby字符串字段并按时间范围 Select

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

Julia CSV for Python中的等效性Pandas index_col参数

数据抓取失败:寻求帮助

Python虚拟环境的轻量级使用

梯度下降:简化要素集的运行时间比原始要素集长

Odoo 16使用NTFS使字段只读

在pandas数据框中计算相对体积比指标,并添加指标值作为新列

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion

查看pandas字符列是否在字符串列中

如何强制向量中的特定元素在Gekko中处于优化解决方案中

jsonschema日期格式

在round函数中使用列值

按列表分组到新列中

根据边界点的属性将图划分为子图

基于2级列表的Pandas 切片3级多索引