是否需要在Python中使用密码或私钥加密某些文本? 您当然来对了地方。 AES-256是一种固态对称密码,通常用于为自己加密数据。 换句话说,正在加密数据的同一个人通常也将其解密(请考虑密码管理器 )。

对于本教程,我们将使用Python 3,因此请确保安装pycryptodome ,这将使我们能够访问AES-256的实现:

pip3 install pycryptodomex

AES-256通常要求以16个字节的块提供要加密的数据,您可能已经在其他站点或教程中看到了。 但是,在GCM模式下,AES-256不需要任何特殊填充即可由我们手动完成。

现在我们创建一个简单的crypto(plain_text,password)函数。 此功能使用密码来加密纯文本。 因此,任何有权访问加密文本和密码的人都可以对其解密。

  1. # AES 256 encryption/decryption using pycryptodome library
  2. from base64 import b64encode, b64decode
  3. import hashlib
  4. from Cryptodome.Cipher import AES
  5. import os
  6. from Cryptodome.Random import get_random_bytes
  7. def encrypt (plain_text, password) :
  8. # generate a random salt
  9. salt = get_random_bytes(AES.block_size)
  10. # use the Scrypt KDF to get a private key from the password
  11. private_key = hashlib.scrypt(
  12. password.encode(), salt=salt, n= 2 ** 14 , r= 8 , p= 1 , dklen= 32 )
  13. # create cipher config
  14. cipher_config = AES.new(private_key, AES.MODE_GCM)
  15. # return a dictionary with the encrypted text
  16. cipher_text, tag = cipher_config.encrypt_and_digest(bytes(plain_text, 'utf-8' ))
  17. return {
  18. 'cipher_text' : b64encode(cipher_text).decode( 'utf-8' ),
  19. 'salt' : b64encode(salt).decode( 'utf-8' ),
  20. 'nonce' : b64encode(cipher_config.nonce).decode( 'utf-8' ),
  21. 'tag' : b64encode(tag).decode( 'utf-8' )
  22. }
  23. def decrypt (enc_dict, password) :
  24. # decode the dictionary entries from base64
  25. salt = b64decode(enc_dict[ 'salt' ])
  26. cipher_text = b64decode(enc_dict[ 'cipher_text' ])
  27. nonce = b64decode(enc_dict[ 'nonce' ])
  28. tag = b64decode(enc_dict[ 'tag' ])
  29. # generate the private key from the password and salt
  30. private_key = hashlib.scrypt(
  31. password.encode(), salt=salt, n= 2 ** 14 , r= 8 , p= 1 , dklen= 32 )
  32. # create the cipher config
  33. cipher = AES.new(private_key, AES.MODE_GCM, nonce=nonce)
  34. # decrypt the cipher text
  35. decrypted = cipher.decrypt_and_verify(cipher_text, tag)
  36. return decrypted
  37. def main () :
  38. password = input( "Password: " )
  39. # First let us encrypt secret message
  40. encrypted = encrypt( "The secretest message here" , password)
  41. print(encrypted)
  42. # Let us decrypt using our original password
  43. decrypted = decrypt(encrypted, password)
  44. print(bytes.decode(decrypted))
  45. main()

有关crypto()函数的注意事项

  1. 随机数 :一个随机数(任意值)必须是我们的加密功能使用相同的密钥每次随机和独特的价值。 可以将其视为密码的随机盐。 图书馆为我们提供了安全的随机数。
  2. Scrypt :Scrypt用于从密码生成安全私钥。 这将使攻击者更难于暴力破解我们的加密。
  3. 盐 :每次加密都会使用新的随机盐。 这使得攻击者无法使用预先计算的哈希来破解密码。 ( 见彩虹表 )
  4. 加密参数 :
  1. N是成本因素。 它必须是2的幂,并且它越高,密钥就越安全,但是需要更多的资源来运行。
  2. R是块大小。
  3. P是并行化因子,可用于在多个内核上运行。
  1. Base64 :我们将所有字节类型的数据编码为方便的字符串表示形式的base64
  2. 标签 :在GCM模式下使用AES时,标签用于验证数据。 这样可以确保在解密时没有人不知道我们就无法更改我们的数据。
    crypto()函数需要使用与加密相同的盐,随机数和标记。 为了方便解析,我们使用了字典,但是如果我们想要一串密文,则可以使用诸如salt.nonce.tag.cipher_text之类的方案。Scrypt和AES函数上的配置参数必须与crypto函数相同。 
版权声明:本文为myrj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xkdn/p/16042385.html