没有额外模块的Python AES加密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25261647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:58:36  来源:igfitidea点击:

Python AES encryption without extra module

pythonaespython-3.4

提问by Anton

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.Cryptographyreference.

是否可以在不安装额外模块的情况下使用 AES 加密/解密数据?我需要从 发送/接收数据C#,这是用System.Security.Cryptography参考加密的。

UPDATEI 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.3while my version is 3.4.

更新我尝试使用 PyAES,但这太旧了。我更新了一些东西来使它工作,但它没有。我也无法安装,因为它是最新版本,3.3而我的版本是3.4.

回答by enrico.bacis

The available Cryptographic Services available in the Standard Library are those. As you can see AESis not listed, but is suggest to use pycryptowhich is an extra module.

标准库中可用的加密服务是那些. 如您所见AES,未列出,但建议使用pycryptowhich 是一个额外的模块

You just have to install it using pip, or easy_installand then as showed in pycrypto's page:

您只需要使用pipeasy_install安装它,然后如pycrypto的页面所示:

from Crypto.Cipher import AES
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
message = "The answer is no"
print obj.encrypt(message)

The only other way without using an extra module would be to code the function yourself, but what's the difference of downloading an extra module and use that instead?

不使用额外模块的唯一另一种方法是自己编写函数代码,但是下载额外模块并使用它有什么区别?

If you want a pure Python implementation of AES that you can download and import check pyaes.

如果您想要 AES 的纯 Python 实现,您可以下载并导入 check pyaes

回答by MSmedberg

To add to @enrico.bacis' answer: AES is not implemented in the standard library. It is implemented in the PyCrypto library, which is stable and well tested. If you need AES, add PyCrypto as a dependency of your code.

要添加到@enrico.bacis 的答案中:标准库中未实现 AES。它在 PyCrypto 库中实现,该库稳定且经过良好测试。如果您需要 AES,请将 PyCrypto 添加为您的代码的依赖项。

While the AES primitives are, in theory, simple enough that you could write an implementation of them in pure Python, it is stronglyrecommended that you not do so. This is the first rule of crypto: don't implement it yourself. In particular, if you're just rolling your own crypto library, you'll almost certainly leave yourself open to some kind of side-channel attack.

虽然理论上 AES 原语足够简单,您可以用纯 Python 编写它们的实现,但强烈建议您不要这样做。这是加密的第一条规则:不要自己实现。特别是,如果您只是推出自己的加密库,您几乎肯定会让自己受到某种旁​​道攻击。

回答by snowcrash09

Here is a self-contained implementation of AES compatible with Python 3.

这是与 Python 3 兼容的 AES 的自包含实现

Example usage:

用法示例:

aesmodal = AESModeOfOperation() 
key = [143,194,34,208,145,203,230,143,177,246,97,206,145,92,255,84]
iv = [103,35,148,239,76,213,47,118,255,222,123,176,106,134,98,92]

size = aesmodal.aes.keySize["SIZE_128"]

mode,orig_len,ciphertext = aesmodal.encrypt("Hello, world!", aesmodal.modeOfOperation["OFB"], key, size, iv)
print(ciphertext)
plaintext = aesmodal.decrypt(ciphertext, orig_len, mode, key, size, iv)
print(plaintext)

回答by Alecz

PYAES should work with any version of Python3.x. No need to modify the library.

PYAES 应该适用于任何版本的 Python3.x。无需修改库。

Here is a complete working example for pyaes CTR mode for Python3.x (https://github.com/ricmoo/pyaes)

这是 Python3.x ( https://github.com/ricmoo/pyaes) pyaes CTR 模式的完整工作示例

import pyaes

# A 256 bit (32 byte) key
key = "This_key_for_demo_purposes_only!"
plaintext = "Text may be any length you wish, no padding is required"

# key must be bytes, so we convert it
key = key.encode('utf-8')

aes = pyaes.AESModeOfOperationCTR(key)    
ciphertext = aes.encrypt(plaintext)

# show the encrypted data
print (ciphertext)

# DECRYPTION
# CRT mode decryption requires a new instance be created
aes = pyaes.AESModeOfOperationCTR(key)

# decrypted data is always binary, need to decode to plaintext
decrypted = aes.decrypt(ciphertext).decode('utf-8')

# True
print (decrypted == plaintext)

Let me know if you get any errors

如果您有任何错误,请告诉我

回答by Vlad Bezden

I'm using Cryptographylibrary.

我正在使用密码学库。

Cryptography is an actively developed library that provides cryptographic recipes and primitives. It supports Python 2.6-2.7, Python 3.3+ and PyPy.

密码学是一个积极开发的库,提供密码配方和原语。它支持 Python 2.6-2.7、Python 3.3+ 和 PyPy。

Here is an exampleof how to use that library:

以下是如何使用该库的示例

>>> 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'

回答by Saurabh Chandra Patel

Python 3.6 with cryptography module

带有加密模块的 Python 3.6

from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b"my deep dark secret")
print(token)


f.decrypt(token)