最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

pycryptodemo实现DES,AES, RC4,Hash,RSA,DSA

互联网 admin 20浏览 0评论

pycryptodemo实现DES,AES, RC4,Hash,RSA,DSA

编译环境:pycharm , pycrytodemo 3.9.7

DES

from Crypto.Cipher import DES
import binasciidef main():# 这是密钥key = b'abcdefgh'   # key需为8字节长度.# 生成DES对象des = DES.new(key, DES.MODE_ECB)# 需要加密的数据text =input('input the message:')     # 被加密的数据需要为8字节的倍数.if len(text) % 8 != 0:text = text + (8 - (len(text) % 8)) * '0'# 加密encrypto_text = des.encrypt(text.encode())print('encrypto_text:',encrypto_text)#解密decrrpto_text = des.decrypt(encrypto_text)
print('encrypto_text:',decrrpto_text.decode())

AES

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Util.Padding import unpad
from Crypto.Random import get_random_bytesdef  main():data = input("please input you message: ")data = bytes(data,'utf-8')
# 随机生成16字节(即128位)的加密密钥key = get_random_bytes(16)print("key: %s"%(key))
# 实例化加密套件,使用CBC模式cipher = AES.new(key, AES.MODE_CBC)
# 对内容进行加密,pad函数用于分组和填充encrypted_data = cipher.encrypt(pad(data, AES.block_size))print("encrypted_data: %s"%(encrypted_data))# 实例化解密套件cipher1 = AES.new(key, AES.MODE_CBC, cipher.iv)
# 解密,unpad函数用于分组和去填充decrypted_data = unpad(cipher1.decrypt(encrypted_data), AES.block_size)print("decrypted_data: %s"%(decrypted_data.decode('utf-8')))if __name__=='__main__':
main()

RC4

from Crypto.Cipher import ARC4
from Crypto.Hash import SHA
from Crypto.Random import get_random_bytesdef main():data = input("please input the message you want encrypt: ")data=bytes(data,'gbk')# 随机生成16字节(即128位)作为密钥key = get_random_bytes(16)print("key: %s"% (key))# 实例化加密套件cipher = ARC4.new(key)# 加密encrypted_data = cipher.encrypt(data)print("encrypted_data: %s" % (encrypted_data))# 实例解密套件cipher1 = ARC4.new(key)# 解密decrypted_data = cipher1.decrypt(encrypted_data)print("decrypted_date: %s"%(decrypted_data.decode('gbk') ))if __name__=='__main__':
main()

Hash

import hashlibprint(hashlib.algorithms_available)  # 支持的单向加密算法
text = input('please input the message:')# 待加密信息
hl = hashlib.md5()  # 创建md5对象,也可以hl = hashlib.new("md5")#md5加密
hl = hashlib.md5()  # 创建md5对象,也可以hl = hashlib.new("md5")
hl.update(text.encode('utf-8' ))  # 转换为bytes
hash_text = hl.hexdigest().upper()  # hexdigest() 加密过程:以十六进制字符串形式输出
print('md5_text :' ,hash_text)#sha1
h2 = hashlib.sha1()  # 创建sha1对象
h2.update(text.encode('utf-8' ))  # 转换为bytes
hash_text = h2.hexdigest().upper()  # hexdigest() 加密过程:以十六进制字符串形式输出
print('sha1_text :' ,hash_text)

RSA

from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64def main():
#输入message = input("please input you id: ")
#初始化生成公式钥对保存在文件中generate()
#加密with open('master-public.pem', "r") as f:key = f.read()cipher_text=rsa_encrypt(message,key)print("cipher_text: %s"% (cipher_text))
#解密with open('master-private.pem') as f:key = f.read()text=rsa_decrypt(cipher_text,key)print("text: %s"% (text.decode('utf-8')))#初始化
def generate():# 伪随机数生成器random_generator = Random.new().read# rsa算法生成实例rsa = RSA.generate(1024, random_generator)# 获得私钥并保存private_pem = rsa.exportKey()with open('master-private.pem', 'wb') as f:f.write(private_pem)# 过的公钥并保存public_pem = rsa.publickey().exportKey()with open('master-public.pem', 'wb') as f:f.write(public_pem)
Return#加密
def rsa_encrypt(message,key):rsakey = RSA.importKey(key)  # 导入读取到的公钥cipher = PKCS1_v1_5.new(rsakey)  # 生成对象# 通过生成的对象加密message明文,注意,在python3中加密的数据必须是bytes类型的数据,不能是str类型的数据cipher_text = base64.b64encode(cipher.encrypt(message.encode("utf-8")))return cipher_text#解密
def rsa_decrypt(cipher_text,key):rsakey = RSA.importKey(key)  # 导入读取到的私钥cipher = PKCS1_v1_5.new(rsakey)  # 生成对象# 将密文解密成明文,返回的是一个bytes类型数据,需要自己转换成strtext = cipher.decrypt(base64.b64decode(cipher_text), "ERROR")return text

DSA

from Crypto.Random import random
from Crypto.PublicKey import DSA
from Crypto.Hash import SHAdef dsa():message = input('please input your message:')key = DSA.generate(1024)h = SHA.new(message.encode()).hexdigest()k = random.StrongRandom().randint(1, key.q-1)#签名sig = key._sign(int(h,16), k)print('Signature:',list(key._sign(int(h,16), k)))if key._verify(int(h,16), sig):print("Signature verification is true")else:print("Sorry,signature verification is false")

pycryptodemo实现DES,AES, RC4,Hash,RSA,DSA

编译环境:pycharm , pycrytodemo 3.9.7

DES

from Crypto.Cipher import DES
import binasciidef main():# 这是密钥key = b'abcdefgh'   # key需为8字节长度.# 生成DES对象des = DES.new(key, DES.MODE_ECB)# 需要加密的数据text =input('input the message:')     # 被加密的数据需要为8字节的倍数.if len(text) % 8 != 0:text = text + (8 - (len(text) % 8)) * '0'# 加密encrypto_text = des.encrypt(text.encode())print('encrypto_text:',encrypto_text)#解密decrrpto_text = des.decrypt(encrypto_text)
print('encrypto_text:',decrrpto_text.decode())

AES

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Util.Padding import unpad
from Crypto.Random import get_random_bytesdef  main():data = input("please input you message: ")data = bytes(data,'utf-8')
# 随机生成16字节(即128位)的加密密钥key = get_random_bytes(16)print("key: %s"%(key))
# 实例化加密套件,使用CBC模式cipher = AES.new(key, AES.MODE_CBC)
# 对内容进行加密,pad函数用于分组和填充encrypted_data = cipher.encrypt(pad(data, AES.block_size))print("encrypted_data: %s"%(encrypted_data))# 实例化解密套件cipher1 = AES.new(key, AES.MODE_CBC, cipher.iv)
# 解密,unpad函数用于分组和去填充decrypted_data = unpad(cipher1.decrypt(encrypted_data), AES.block_size)print("decrypted_data: %s"%(decrypted_data.decode('utf-8')))if __name__=='__main__':
main()

RC4

from Crypto.Cipher import ARC4
from Crypto.Hash import SHA
from Crypto.Random import get_random_bytesdef main():data = input("please input the message you want encrypt: ")data=bytes(data,'gbk')# 随机生成16字节(即128位)作为密钥key = get_random_bytes(16)print("key: %s"% (key))# 实例化加密套件cipher = ARC4.new(key)# 加密encrypted_data = cipher.encrypt(data)print("encrypted_data: %s" % (encrypted_data))# 实例解密套件cipher1 = ARC4.new(key)# 解密decrypted_data = cipher1.decrypt(encrypted_data)print("decrypted_date: %s"%(decrypted_data.decode('gbk') ))if __name__=='__main__':
main()

Hash

import hashlibprint(hashlib.algorithms_available)  # 支持的单向加密算法
text = input('please input the message:')# 待加密信息
hl = hashlib.md5()  # 创建md5对象,也可以hl = hashlib.new("md5")#md5加密
hl = hashlib.md5()  # 创建md5对象,也可以hl = hashlib.new("md5")
hl.update(text.encode('utf-8' ))  # 转换为bytes
hash_text = hl.hexdigest().upper()  # hexdigest() 加密过程:以十六进制字符串形式输出
print('md5_text :' ,hash_text)#sha1
h2 = hashlib.sha1()  # 创建sha1对象
h2.update(text.encode('utf-8' ))  # 转换为bytes
hash_text = h2.hexdigest().upper()  # hexdigest() 加密过程:以十六进制字符串形式输出
print('sha1_text :' ,hash_text)

RSA

from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64def main():
#输入message = input("please input you id: ")
#初始化生成公式钥对保存在文件中generate()
#加密with open('master-public.pem', "r") as f:key = f.read()cipher_text=rsa_encrypt(message,key)print("cipher_text: %s"% (cipher_text))
#解密with open('master-private.pem') as f:key = f.read()text=rsa_decrypt(cipher_text,key)print("text: %s"% (text.decode('utf-8')))#初始化
def generate():# 伪随机数生成器random_generator = Random.new().read# rsa算法生成实例rsa = RSA.generate(1024, random_generator)# 获得私钥并保存private_pem = rsa.exportKey()with open('master-private.pem', 'wb') as f:f.write(private_pem)# 过的公钥并保存public_pem = rsa.publickey().exportKey()with open('master-public.pem', 'wb') as f:f.write(public_pem)
Return#加密
def rsa_encrypt(message,key):rsakey = RSA.importKey(key)  # 导入读取到的公钥cipher = PKCS1_v1_5.new(rsakey)  # 生成对象# 通过生成的对象加密message明文,注意,在python3中加密的数据必须是bytes类型的数据,不能是str类型的数据cipher_text = base64.b64encode(cipher.encrypt(message.encode("utf-8")))return cipher_text#解密
def rsa_decrypt(cipher_text,key):rsakey = RSA.importKey(key)  # 导入读取到的私钥cipher = PKCS1_v1_5.new(rsakey)  # 生成对象# 将密文解密成明文,返回的是一个bytes类型数据,需要自己转换成strtext = cipher.decrypt(base64.b64decode(cipher_text), "ERROR")return text

DSA

from Crypto.Random import random
from Crypto.PublicKey import DSA
from Crypto.Hash import SHAdef dsa():message = input('please input your message:')key = DSA.generate(1024)h = SHA.new(message.encode()).hexdigest()k = random.StrongRandom().randint(1, key.q-1)#签名sig = key._sign(int(h,16), k)print('Signature:',list(key._sign(int(h,16), k)))if key._verify(int(h,16), sig):print("Signature verification is true")else:print("Sorry,signature verification is false")
发布评论

评论列表 (0)

  1. 暂无评论