我正在尝试加密和解密python中的文本,我知道该怎么做 - 问题是我不想使用一定数量的字母,如16或32.我希望能够使用尽可能多的字母/数字,然后加密文本没有任何错误.
python中的base64会很完美,因为我可以做到这一点,但是当我想做的时候:
password = "password"
encode = base64.b64encode(password)
Run Code Online (Sandbox Code Playgroud)
...它返回一个错误,因为它不是以字节为单位; 它必须像:
encode = base64.b64encode(b'password')
Run Code Online (Sandbox Code Playgroud)
这完全没问题,但我不想这样做.
import base64
password = "hello world"
encoded = base64.b64encode(password.encode("utf-8"))
print(encoded)
decoded = base64.b64decode(encoded)
print(decoded)
Run Code Online (Sandbox Code Playgroud)
这是现在我的代码,它工作正常,但我现在知道我正在使用错误的类型,我需要知道如何使用AES.
Rei*_*ica 19
在Python 3中,您需要将字符串转换为字节,因为base64编码依赖于字符串的编码,而Python 3不会对字符串编码做出假设.看到这个问题.
import base64
# Assuming UTF-8 encoding, change to something else if you need to
base64.b64encode("password".encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
此页面解释了为什么字符串在Python 3中的作用不同:
与2.x情况的最大区别在于,任何在Python 3.0中混合文本和数据的尝试都会引发TypeError,而如果你要在Python 2.x中混合使用Unicode和8位字符串,那么如果8位字符串碰巧只包含7位(ASCII)字节,但如果包含非ASCII值,则会得到UnicodeDecodeError.多年来,这种特定价值的行为造成了许多悲伤的面孔.
而且,就像sberry所说,base64编码不是加密的.如果您确实希望这是安全的,则需要使用AES之类的东西,或者如果您只想安全地存储密码,请使用bcrypt或PBKDF2.
下面是一个使用PyCrypto 使用AES加密某些内容的示例,使用从使用PBKDF2的密码派生的密钥.
#!/usr/bin/env python3
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Protocol.KDF import PBKDF2
def make_key(password, salt = None):
if salt is None:
# Generate a key from the password
salt = Random.new().read(8)
# You probably want to adjust the number of iterations
# based on your target platform and willingness to wait.
# Somewhere around 10,000 will give you reasonable security.
# If you don't mind the wait, 100,000 is better.
# If you have a really fast computer, or are willing to wait a long
# time, feel free to set it even higher.
key = PBKDF2(password, salt, AES.block_size, 100000)
return (key, salt)
def encrypt(message, key):
# The IV should always be random
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
ciphertext = cipher.encrypt(message.encode("utf-8"))
return (ciphertext, iv)
def decrypt(ciphertext, key, iv):
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = cipher.decrypt(ciphertext).decode("utf-8")
return msg
def main():
# Encryption
password = "correct horse battery staple"
message = "Super secret information that shouldn't be seen by attackers"
key, salt = make_key(password)
ciphertext, iv = encrypt(message, key)
print(b"The ciphertext is: " + ciphertext)
# Decryption
# In normal cases, you now need to store the salt and iv somewhere
# Usually you prepend them to the ciphertext
# I don't feel like doing that, so we'll just assume that I got the salt
# and IV somehow.
key, _ = make_key(password, salt)
cleartext = decrypt(ciphertext, key, iv)
print("The cleartext is: " + cleartext)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
只使用这样的AES提供机密性(攻击者无法在没有密码的情况下读取消息),但不是完整性(攻击者可以将数据插入到密文中,您可以注意到的唯一方法是它可能会解密为垃圾) .为了防止这种情况,您还可以使用消息验证代码来确保密码没有被没有密码的人更改.
我认为这是一个有趣的练习,所以我在BitBucket回购中添加了一个更完整的例子.它添加了一个HMAC,并从JSON文件读取和写入.
| 归档时间: |
|
| 查看次数: |
20133 次 |
| 最近记录: |