如何在 python 中加密和解密字符串?
- 2025-03-20 08:46:00
- admin 原创
- 37
问题描述:
我一直在寻找如何加密和解密字符串的方法。但大多数都是在 2.7 中,而使用 3.2 的任何内容都不允许我将其打印或添加到字符串中。
所以我想做的事情如下:
mystring = "Hello stackoverflow!"
encoded = encode(mystring,"password")
print(encoded)
jgAKLJK34t3g(一堆随机字母)
decoded = decode(encoded,"password")
print(decoded)
你好,stackoverflow!
有没有办法使用 python 3.X 来做到这一点,并且当字符串被编码时它仍然是一个字符串,而不是任何其他变量类型。
解决方案 1:
我在 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:
看看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
'xd6x83x8dd!VTx92xaa`Ax05xe0x9bx8bxf1'
>>> 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
。
解决方案 3:
尝试一下:
需要Python 加密工具包 ( pycrypto )
$ pip install pycrypto
pycrypto
包已经过时,自 2014 年以来就没有再维护过。有一个名为的替代包pycryptodome
。
$ pip install pycryptodome
下面的代码在python 3.8上完美运行
代码:
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
class Crypt:
def __init__(self, salt='SlTKeYOpHygTYkP3'):
self.salt = salt.encode('utf8')
self.enc_dec_method = 'utf-8'
def encrypt(self, str_to_enc, str_key):
try:
aes_obj = AES.new(str_key.encode('utf-8'), AES.MODE_CFB, self.salt)
hx_enc = aes_obj.encrypt(str_to_enc.encode('utf8'))
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.encode('utf8'), 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}')
解决方案 4:
你可以使用库轻松完成此操作cryptocode
。安装方法如下:
pip install cryptocode
加密消息(示例代码):
import cryptocode
encoded = cryptocode.encrypt("mystring","mypassword")
## And then to decode it:
decoded = cryptocode.decrypt(encoded, "mypassword")
文档可以在这里找到
解决方案 5:
加密数据
首先,我们需要安装加密库:
pip3 install cryptography
从加密库中,我们需要导入
Fernet
并开始生成密钥——该密钥是对称加密/解密所必需的。为了生成密钥,我们调用该
generate_key()
方法。
+ 我们只需要执行一次上述方法就可以生成一个密钥。
您需要将此密钥保存在安全的地方。如果您丢失了密钥,您将无法解密使用此密钥加密的数据。
生成密钥后,我们需要使用以下命令加载密钥
load_key()
加密消息
这是一个三步过程:
对消息进行编码
初始化 Fernet 类
将编码的消息传递给
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=
解决方案 6:
虽然它很老了,但我还是想到分享另一个想法来做到这一点:
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'xcbx0bx8cxdc#
xddx80xa6|xacux1dEg;x8exa2xafx80xeax95x80x02x13x1aemxcbxf40xdb'
> Hello stackoverflow!
解决方案 7:
您可以按如下方式使用 Fernet:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
encrypt_value = f.encrypt(b"YourString")
f.decrypt(encrypt_value)
解决方案 8:
这是一个类似于一次性密码本的简单算法……用于说明如何使用一个算法str
来加密另一个算法,然后解密密文。
def encode(secret: str, key: str) -> str:
"""use `key` to encrypt `secret`"""
encoded_secret_arr = []
for idx, c in enumerate(secret):
encoded_secret_arr.append(chr(ord(c) + ord(key[idx % len(key)])))
return "".join(encoded_secret_arr)
def decode(encoded_secret: str, key: str) -> str:
"""use `key` to decrypt encoded_secret)"""
secret_arr = []
for idx, c in enumerate(encoded_secret):
secret_arr.append(chr(ord(c) - ord(key[idx % len(key)])))
return "".join(secret_arr)
assert decode(encode("secret", "key"), "key") == "secret"
解决方案 9:
对于加密
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
}
对于解密:
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
}
扫码咨询,免费领取项目管理大礼包!