经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
python实现AES加密与解密
来源:jb51  时间:2019/3/29 8:38:49  对本文有异议

AES加密方式有五种:ECB, CBC, CTR, CFB, OFB

从安全性角度推荐CBC加密方法,本文介绍了CBC,ECB两种加密方法的python实现

python 在 Windows下使用AES时要安装的是pycryptodome 模块  

pip install pycryptodome

python 在 Linux下使用AES时要安装的是pycrypto模块  

pip install pycrypto

CBC加密需要一个十六位的key(密钥)和一个十六位iv(偏移量)

ECB加密不需要iv

AES CBC 加密的python实现

  1. from Crypto.Cipher import AES
  2. from binascii import b2a_hex, a2b_hex
  3.  
  4.  
  5. # 如果text不足16位的倍数就用空格补足为16位
  6. def add_to_16(text):
  7. if len(text.encode('utf-8')) % 16:
  8. add = 16 - (len(text.encode('utf-8')) % 16)
  9. else:
  10. add = 0
  11. text = text + ('\0' * add)
  12. return text.encode('utf-8')
  13.  
  14.  
  15. # 加密函数
  16. def encrypt(text):
  17. key = '9999999999999999'.encode('utf-8')
  18. mode = AES.MODE_CBC
  19. iv = b'qqqqqqqqqqqqqqqq'
  20. text = add_to_16(text)
  21. cryptos = AES.new(key, mode, iv)
  22. cipher_text = cryptos.encrypt(text)
  23. # 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串
  24. return b2a_hex(cipher_text)
  25.  
  26.  
  27. # 解密后,去掉补足的空格用strip() 去掉
  28. def decrypt(text):
  29. key = '9999999999999999'.encode('utf-8')
  30. iv = b'qqqqqqqqqqqqqqqq'
  31. mode = AES.MODE_CBC
  32. cryptos = AES.new(key, mode, iv)
  33. plain_text = cryptos.decrypt(a2b_hex(text))
  34. return bytes.decode(plain_text).rstrip('\0')
  35.  
  36.  
  37. if __name__ == '__main__':
  38. e = encrypt("hello world") # 加密
  39. d = decrypt(e) # 解密
  40. print("加密:", e)
  41. print("解密:", d)

AES ECB加密的python实现

  1. """
  2. ECB没有偏移量
  3. """
  4. from Crypto.Cipher import AES
  5. from binascii import b2a_hex, a2b_hex
  6.  
  7.  
  8. def add_to_16(text):
  9. if len(text.encode('utf-8')) % 16:
  10. add = 16 - (len(text.encode('utf-8')) % 16)
  11. else:
  12. add = 0
  13. text = text + ('\0' * add)
  14. return text.encode('utf-8')
  15.  
  16.  
  17. # 加密函数
  18. def encrypt(text):
  19. key = '9999999999999999'.encode('utf-8')
  20. mode = AES.MODE_ECB
  21. text = add_to_16(text)
  22. cryptos = AES.new(key, mode)
  23.  
  24. cipher_text = cryptos.encrypt(text)
  25. return b2a_hex(cipher_text)
  26.  
  27.  
  28. # 解密后,去掉补足的空格用strip() 去掉
  29. def decrypt(text):
  30. key = '9999999999999999'.encode('utf-8')
  31. mode = AES.MODE_ECB
  32. cryptor = AES.new(key, mode)
  33. plain_text = cryptor.decrypt(a2b_hex(text))
  34. return bytes.decode(plain_text).rstrip('\0')
  35.  
  36.  
  37. if __name__ == '__main__':
  38. e = encrypt("hello world") # 加密
  39. d = decrypt(e) # 解密
  40. print("加密:", e)
  41. print("解密:", d)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号