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

编译环境:pycharm , pycrytodemo 3.9.7

DES

from Crypto.Cipher import DES
import binascii

def 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_bytes

def  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_bytes

def 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 hashlib

print(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 base64

def 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类型数据,需要自己转换成str
        text = cipher.decrypt(base64.b64decode(cipher_text), "ERROR")
        return text

DSA

from Crypto.Random import random
from Crypto.PublicKey import DSA
from Crypto.Hash import SHA

def 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")
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值