ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python3 Crypto模块实例解析

2022-05-17 23:01:43  阅读:258  来源: 互联网

标签:AES obj text urandom Crypto 实例 encrypted message python3


一 模块简介 1.简介 python的Crypto模块是安全hash函数(例如SHA256 和RIPEMD160)以及各种主流的加解密算法的((AES, DES, RSA, ElGamal等)的集合。   二 实例解析   1.AES实例
from os import urandom
from Crypto.Cipher import AES

# For Generating cipher text
secret_key = urandom(16)
iv = urandom(16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)

# Encrypt the message
message = b'Xu sheng test me'
print('Original message is: ', message)
encrypted_text = obj.encrypt(message)
print('The encrypted text', encrypted_text)

输出结果:

Original message is:  b'Xu sheng test me'
The encrypted text b'\x80\xaf\x93\xef\xd6\xab\xe4t\xad\xdf\xfbgChSt'

2. AES解密

from os import urandom
from Crypto.Cipher import AES

# For Generating cipher text
secret_key = urandom(16)
iv = urandom(16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)

# Encrypt the message
message = b'Lorem Ipsum text'
print('Original message is: ', message)
encrypted_text = obj.encrypt(message)
print('The encrypted text', encrypted_text)

# Decrypt the message
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(encrypted_text)
print('The decrypted text', decrypted_text.decode('utf-8'))
输出结果:
Original message is:  b'Lorem Ipsum text'
The encrypted text b'\xe9\xfcC\xb3\x132&n\xc7\xdeZe9\xeb\xab\xf3'
The decrypted text Lorem Ipsum text
三 注意事项 1.错误: TypeError: Object type <class 'str'> cannot be passed to C code 解决方案: AES_KEY、IV还有要加密的数据转换成bytes类型就可以了。



标签:AES,obj,text,urandom,Crypto,实例,encrypted,message,python3
来源: https://www.cnblogs.com/dylancao/p/16282771.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有