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")

你可能感兴趣的:(密码学)