问题标签 [pycryptodome]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
4175 浏览

python - PyCryptodome 错误:MAC 检查失败

我正在使用 Python 3 中的 Pycryptodome 开发一个加密程序。我正在尝试加密一个(字节)字符串,然后对其进行解密并验证 MAC 标签。当我验证它时,会引发错误。

这是代码:

这是错误:

我查看了文档,在我看来一切都很好。为什么你认为程序会这样?任何帮助,将不胜感激。

0 投票
1 回答
1161 浏览

python - 启动 CBC 模式时出现错误 65537

我一直在使用该PyCryptoDome库来加密客户端和服务器之间的一些通信。这是协议的运行方式。

  1. 服务器向客户端发送一个 RSA 公钥。
  2. 客户端生成 AES 密钥
  3. 客户端对其进行加密。使用公钥加密它(使用PKCS1_OAEP加密功能)
  4. 加密的密钥被发送到服务器。
  5. 服务器解密密钥
  6. 服务器和客户端切换到 AES-CBC 来加密通信。

之后,当客户端发送消息时,服务器对其进行解密,将其用于需要处理的事情,然后将其发回。一切顺利,直到客户端尝试解密服务器发回的消息。接收消息的线程停止,原因是:

启动 CBC 模式时出现错误 65537

使用不同的 AES 模式时会出现不同的错误代码。

这是我修改后的AESCipher类:

客户端接收端(导致错误的端):

0 投票
1 回答
574 浏览

python - 使用 PyCryptodome 解密 QR 码时出错

我正在实现一个可以加密和解密二维码内容的python包。我制作了一个名为 的模块rsa_module.py,用于对消息进行加密和解密,如下所示:

如果我运行此脚本,则消息将被成功加密和解密。但是,当我在另一个模块中使用相同的函数时,从 QR 码中解密消息,我得到一个错误。该二维码解密模型名称为decrypt_qr.py,代码如下:

如果我运行decrypt_qr.py,我会收到以下错误:

但是,如果我rsa_module.py只使用传递的消息运行它,那么它确实会正确解密。谁能建议我哪里出错了?

名为 的加密模块encrypt_qr.py如下:

如果我运行encrypt_qr.py脚本,则 qr 代码会正确生成并包含加密的字节流,然后在解密脚本 ( decrypt_qr.py) 中使用。

0 投票
0 回答
1600 浏览

python - 加密导致 TypeError 的 Pyinstaller 无法传递给 C 代码

我想编译一个加密的 exe(以防止“随意”反编译)。

当我不使用加密时,我可以使用规范文件在一个 exe 文件中使用 Pyinstaller v2.1 进行编译。

但是,当我将以下行添加到 .spec 文件以引入加密(pycryptodome)时:

我最终得到这个错误: File "c:\users\username\test-env\lib\site-packages\Crypto\Util\_raw_api.py", line 151, 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

导致错误的日志是: 90328 INFO: Appending 'datas' from .spec 90371 INFO: checking PYZ 90372 INFO: Building PYZ because out00-PYZ.toc is non existent 90373 INFO: Building PYZ (ZlibArchive) C:\Users\choom.000\Documents\masstools_clean\obf\build\graphms\out00-PYZ.pyz Traceback (most recent call last): File "C:\Users\choom.000\test-env\Scripts\pyinstaller-script.py", line 11, in <module> load_entry_point('PyInstaller==3.3.1', 'console_scripts', 'pyinstaller')() File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\__main__.py", line 94, in run run_build(pyi_config, spec_file, **vars(args)) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\__main__.py", line 46, in run_build PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\building\build_main.py", line 791, in main build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build')) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\building\build_main.py", line 737, in build exec(text, spec_namespace) File "<string>", line 20, in <module> File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\building\api.py", line 98, in __init__ self.__postinit__() File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\building\datastruct.py", line 161, in __postinit__ self.assemble() File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\building\api.py", line 133, in assemble pyz = ZlibArchiveWriter(self.name, toc, code_dict=self.code_dict, cipher=self.cipher) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\archive\writers.py", line 184, in __init__ super(ZlibArchiveWriter, self).__init__(archive_path, logical_toc) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\archive\writers.py", line 59, in __init__ self._add_from_table_of_contents(logical_toc) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\archive\writers.py", line 84, in _add_from_table_of_contents self.add(toc_entry) # The guts of the archive. File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\archive\writers.py", line 213, in add obj = self.cipher.encrypt(obj) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\archive\pyz_crypto.py", line 58, in encrypt return iv + self.__create_cipher(iv).encrypt(data) File "c:\users\choom.000\test-env\lib\site-packages\PyInstaller\archive\pyz_crypto.py", line 64, in __create_cipher return self._aesmod.new(self.key, self._aesmod.MODE_CFB, iv) File "c:\users\choom.000\test-env\lib\site-packages\Crypto\Cipher\AES.py", line 200, in new return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs) File "c:\users\choom.000\test-env\lib\site-packages\Crypto\Cipher\__init__.py", line 55, in _create_cipher return modes[mode](factory, **kwargs) File "c:\users\choom.000\test-env\lib\site-packages\Crypto\Cipher\_mode_cfb.py", line 230, in _create_cfb_cipher cipher_state = factory._create_base_cipher(kwargs) File "c:\users\choom.000\test-env\lib\site-packages\Crypto\Cipher\AES.py", line 100, in _create_base_cipher result = start_operation(c_uint8_ptr(key), File "c:\users\choom.000\test-env\lib\site-packages\Crypto\Util\_raw_api.py", line 151, 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

似乎这是“str”类的“key”的问题。我试图在 .spec 文件中更改 key = 'a123456712345678' 或 u'1234567812345678' 但仍然有问题。

0 投票
1 回答
709 浏览

python - 在 Windows Python 上加密解密编码

我正在尝试一个非常基本的用例来加密和解密示例字符串。下面是我的方法。我正在使用 pycryptodome 进行加密。

我无法重新生成原始字符串。我得到如下乱码o / ps。我尝试了不同的编码,就像我在 Windows 10 上一样。但没有一个给我原始字符串。我在这里错过了什么吗?我对 python 很陌生。所以如果我犯了一个错误,请告诉我。

0 投票
2 回答
1190 浏览

python - 在Python中解密后pycryprodome AES CBC不匹配

我的目标是拥有一个非常简单的 AES 128 CBC 方案,它加密明文,然后根据 Python 中的给定密钥对其进行解密。我正在使用 pycryptodome 框架,但找不到任何带有 AES CBC 方案示例的文档。

以下是我的代码。解密后的数据与加密前的数据不同。如果有人可以帮助我确定这里出了什么问题,那就太好了。

0 投票
1 回答
435 浏览

python-2.7 - 无法使用 pycryptodome 解密河豚 CTR 文件

我正在尝试恢复使用河豚的旧纯 python 实现加密的文件。

旧代码依赖于单个 blofish.py 文件(版权所有 (C) 2002 Michael Gilfix )

旧数据被加密执行以下操作:

该代码没有初始化河豚的现代实现所需的随机数,因此我无法将其移植到 pycryptodome 函数

我能找到的唯一建议是在 iv 设置为 0 的 initCTR 函数内部(即使 CTR 模式没有 IV)

有人能帮我吗?

0 投票
1 回答
3964 浏览

java - Pycrypto RSA PKCS1 OAEP SHA256 与 Java 的互操作性

我在 Python + Pycryptodome (Pycrypto fork) 中使用以下代码使用 RSA PKCS#1 OAEP SHA256 ( RSA/ECB/OAEPWithSHA-256AndMGF1Padding) 加密消息:

和下面的Java代码来解密它:

但是,我得到:

0 投票
2 回答
3549 浏览

java - Pycrypto AES GCM 加密和 Java 解密

我正在使用 Pycryptodome(一个 PyCrypto 分支)来创建 AES-GCM 密文。我使用以下 Python 代码进行加密:

然后我将其传递给 Java 进行解密:

但是,我收到以下错误:

0 投票
1 回答
122 浏览

aes - 如何减少使用混合加密加密的消息的长度

我一直在寻找一个好的加密方案来加密我的消息,我发现混合加密对大小消息都有好处。但我对输出密码消息的长度有疑问,它很大。

如果输入是“hello”,那么输出消息的长度将为 586,如果消息更大,则为两倍

这是我使用的加密功能: