无需额外模块的Python AES加密 [英] Python AES encryption without extra module
问题描述
是否可以使用AES加密/解密数据而无需安装额外的模块?我需要从 C#
发送/接收数据,其中的数据已使用 System.Security.Cryptography
引用进行加密。
Is it possible to encrypt/decrypt data with AES without installing extra modules? I need to send/recieve data from C#
, wich is encrypted with the System.Security.Cryptography
reference.
更新
我尝试使用PyAES,但这太旧了。我更新了一些内容以使其正常运行,但没有。
我也无法安装,因为它的最新版本是 3.3
,而我的版本是 3.4
。
UPDATE
I have tried to use PyAES, but that is too old. I updated some things to make that work, but it didn't.
I've also can't install because it latest version is 3.3
while my version is 3.4
.
推荐答案
我正在使用密码学库。
密码学是一个积极开发的库,提供
密码配方和原语。它支持Python 2.6-2.7,
Python 3.3+和PyPy。
Cryptography is an actively developed library that provides cryptographic recipes and primitives. It supports Python 2.6-2.7, Python 3.3+ and PyPy.
>>> import os
>>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
>>> from cryptography.hazmat.backends import default_backend
>>> backend = default_backend()
>>> key = os.urandom(32)
>>> iv = os.urandom(16)
>>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
>>> encryptor = cipher.encryptor()
>>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
>>> decryptor = cipher.decryptor()
>>> decryptor.update(ct) + decryptor.finalize()
'a secret message'
这篇关于无需额外模块的Python AES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!