我一直在寻找如何加密和解密字符串。但大部分都在2.7中,任何使用3.2的东西都不允许我打印它或将它添加到字符串中。
所以我想做的是以下几点:
mystring = "Hello stackoverflow!"
encoded = encode(mystring,"password")
print(encoded)
jgAKLJK34t3g(一堆随机字母)
decoded = decode(encoded,"password")
print(decoded)
Hello stackoverflow!
有没有这样做,使用python 3.X,当字符串被编码时,它仍然是一个字符串,而不是任何其他变量类型。
答案 0 :(得分:28)
看看PyCrypto。它支持Python 3.2并且完全符合您的要求。
从他们的pip网站:
>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'
如果您要加密任意大小的邮件,请使用AES.MODE_CFB
而不是AES.MODE_CBC
。
答案 1 :(得分:23)
我在Windows 7系统和Python 3.5上编译所有最常提到的加密库时遇到了麻烦。
这是最终为我工作的解决方案。
from cryptography.fernet import Fernet
key = Fernet.generate_key() #this is your "password"
cipher_suite = Fernet(key)
encoded_text = cipher_suite.encrypt(b"Hello stackoverflow!")
decoded_text = cipher_suite.decrypt(encoded_text)
答案 2 :(得分:2)
尝试一下:
需要Python密码学工具包(pycrypto)
$ pip install pycrypto
代码:
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
class Crypt:
def __init__(self, salt='SlTKeYOpHygTYkP3'):
self.salt = salt
self.enc_dec_method = 'utf-8'
def encrypt(self, str_to_enc, str_key):
try:
aes_obj = AES.new(str_key, AES.MODE_CFB, self.salt)
hx_enc = aes_obj.encrypt(str_to_enc)
mret = b64encode(hx_enc).decode(self.enc_dec_method)
return mret
except ValueError as value_error:
if value_error.args[0] == 'IV must be 16 bytes long':
raise ValueError('Encryption Error: SALT must be 16 characters long')
elif value_error.args[0] == 'AES key must be either 16, 24, or 32 bytes long':
raise ValueError('Encryption Error: Encryption key must be either 16, 24, or 32 characters long')
else:
raise ValueError(value_error)
def decrypt(self, enc_str, str_key):
try:
aes_obj = AES.new(str_key, AES.MODE_CFB, self.salt)
str_tmp = b64decode(enc_str.encode(self.enc_dec_method))
str_dec = aes_obj.decrypt(str_tmp)
mret = str_dec.decode(self.enc_dec_method)
return mret
except ValueError as value_error:
if value_error.args[0] == 'IV must be 16 bytes long':
raise ValueError('Decryption Error: SALT must be 16 characters long')
elif value_error.args[0] == 'AES key must be either 16, 24, or 32 bytes long':
raise ValueError('Decryption Error: Encryption key must be either 16, 24, or 32 characters long')
else:
raise ValueError(value_error)
用法:
test_crpt = Crypt()
test_text = """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
test_key = 'MyKey4TestingYnP'
test_enc_text = test_crpt.encrypt(test_text, test_key)
test_dec_text = test_crpt.decrypt(test_enc_text, test_key)
print(f'Encrypted:{test_enc_text} Decrypted:{test_dec_text}')
答案 3 :(得分:1)
虽然它已经很老了,但是我想分享另一个想法来做到这一点:
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
password = ("anything")
hash_obj = SHA256.new(password.encode('utf-8'))
hkey = hash_obj.digest()
def encrypt(info):
msg = info
BLOCK_SIZE = 16
PAD = "{"
padding = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PAD
cipher = AES.new(hkey, AES.MODE_ECB)
result = cipher.encrypt(padding(msg).encode('utf-8'))
return result
msg = "Hello stackoverflow!"
cipher_text = encrypt(msg)
print(cipher_text)
def decrypt(info):
msg = info
PAD = "{"
decipher = AES.new(hkey, AES.MODE_ECB)
pt = decipher.decrypt(msg).decode('utf-8')
pad_index = pt.find(PAD)
result = pt[: pad_index]
return result
plaintext = decrypt(cipher_text)
print(plaintext)
输出:
> b'\xcb\x0b\x8c\xdc#\n\xdd\x80\xa6|\xacu\x1dEg;\x8e\xa2\xaf\x80\xea\x95\x80\x02\x13\x1aem\xcb\xf40\xdb'
> Hello stackoverflow!
答案 4 :(得分:1)
首先,我们需要安装加密库:
pip3 install cryptography
从 cryptography 库中,我们需要导入 Fernet
并开始生成密钥 - 此密钥是对称加密/解密所必需的。
要生成密钥,我们调用 generate_key()
方法。
您需要将此密钥保存在安全的地方。如果您丢失了密钥,您将无法解密使用此密钥加密的数据。
生成密钥后,我们需要使用 load_key()
这是一个三步过程:
encrypt()
方法from cryptography.fernet import Fernet
def generate_key():
"""
Generates a key and save it into a file
"""
key = Fernet.generate_key()
with open("secret.key", "wb") as key_file:
key_file.write(key)
def load_key():
"""
Load the previously generated key
"""
return open("secret.key", "rb").read()
def encrypt_message(message):
"""
Encrypts a message
"""
key = load_key()
encoded_message = message.encode()
f = Fernet(key)
encrypted_message = f.encrypt(encoded_message)
print(encrypted_message)
if __name__ == "__main__":
# generate_key() # execute only once
encrypt_message("Hello stackoverflow!")
输出:
b'gAAAAABgLX7Zj-kn-We2BI_c9NQhEtfJEnHUVhVqtiqjkDi5dgJafj-_8QUDyeNS2zsJTdBWg6SntRJOjOM1U5mIxxsGny7IEGqpVVdHwheTnwzSBlgpb80='
要解密消息,我们只需调用 decrypt()
库中的 Fernet
方法。请记住,我们还需要加载密钥,因为解密消息需要密钥。
from cryptography.fernet import Fernet
def load_key():
"""
Load the previously generated key
"""
return open("secret.key", "rb").read()
def decrypt_message(encrypted_message):
"""
Decrypts an encrypted message
"""
key = load_key()
f = Fernet(key)
decrypted_message = f.decrypt(encrypted_message)
print(decrypted_message.decode())
if __name__ == "__main__":
decrypt_message(b'gAAAAABgLX7Zj-kn-We2BI_c9NQhEtfJEnHUVhVqtiqjkDi5dgJafj-_8QUDyeNS2zsJTdBWg6SntRJOjOM1U5mIxxsGny7IEGqpVVdHwheTnwzSBlgpb80=')
输出:
Hello stackoverflow!
您的密码在 secret.key
中,格式类似于以下密码:
B8wtXqwBA_zb2Iaz5pW8CIQIwGSYSFoBiLsVz-vTqzw=
答案 5 :(得分:0)
您可以按以下方式使用Fernet:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
encrypt_value = f.encrypt(b"YourString")
f.decrypt(encrypt_value)
答案 6 :(得分:0)
您可以使用库cryptocode
轻松完成此操作。安装方法如下:
pip install cryptocode
加密消息(示例代码):
import cryptocode
encoded = cryptocode.encrypt("mystring","mypassword")
## And then to decode it:
decoded = cryptocode.decrypt(encoded, "mypassword")
可以找到文档here
答案 7 :(得分:-2)
加密
def encrypt(my_key=KEY, my_iv=IV, my_plain_text=PLAIN_TEXT):
key = binascii.unhexlify('ce975de9294067470d1684442555767fcb007c5a3b89927714e449c3f66cb2a4')
iv = binascii.unhexlify('9aaecfcf7e82abb8118d8e567d42ee86')
padder = PKCS7Padder()
padded_text = padder.encode(my_plain_text)
encryptor = AES.new(key, AES.MODE_CBC, iv, segment_size=128) # Initialize encryptor
result = encryptor.encrypt(padded_text)
return {
"plain": my_plain_text,
"key": binascii.hexlify(key),
"iv": binascii.hexlify(iv),
"ciphertext": result
}
答案 8 :(得分:-3)
解密:
def decrypt(my_key=KEY, my_iv=IV, encryptText=encrypttext):
key = binascii.unhexlify(my_key)
iv = binascii.unhexlify(my_iv)
encryptor = AES.new(key, AES.MODE_CBC, iv, segment_size=128) # Initialize encryptor
result = encryptor.decrypt(binascii.a2b_hex(encryptText))
padder = PKCS7Padder()
decryptText=padder.decode(result)
return {
"plain": encryptText,
"key": binascii.hexlify(key),
"iv": binascii.hexlify(iv),
"decryptedTest": decryptText
}