加解密算法

1.引用

import os
import random
import pyDes
from gmssl import sm4

2.一次一密

3.移位

4.DES

5.SM4

import os
import random
import pyDes
from gmssl import sm4

#一次一密加密
def one_time_pad_encrypt_decrypt(src_file, key_file, dst_file):
    # 加密/解密过程与密钥异或
    with open(src_file, 'rb') as sf, open(key_file, 'rb') as kf:
        src_data = bytearray(sf.read())
        key_data = bytearray(kf.read())

        # 生成随机密钥
        if len(src_data) > len(key_data):
            key_data = os.urandom(len(src_data))
            with open(key_file, 'wb') as kf:
                kf.write(key_data)

        # 异或
        cipher_data = bytearray(len(src_data))
        for i in range(len(src_data)):
            cipher_data[i] = src_data[i] ^ key_data[i]

    # 将结果写回到文件
    with open(dst_file, 'wb') as df:
        df.write(cipher_data)

#移位加密
def shift_encrypt_decrypt(src_file, key_file, dst_file):
    # 随机生成移位密钥
    k = random.randint(1, 255).to_bytes(1, byteorder='big')

    # 加密/解密过程与密钥异或
    with open(src_file, 'rb') as sf:
        src_data = bytearray(sf.read())

        cipher_data = bytearray(len(src_data))
        for i in range(len(src_data)):
            cipher_data[i] = (src_data[i] + k[0]) % 256

    # 将结果写回到文件
    with open(dst_file, 'wb') as df:
        df.write(cipher_data)

    # 将移位密钥写入key_file中
    with open(key_file, 'wb') as kf:
        kf.write(k)

#DES加密
def des_encrypt_decrypt(src_file, key_file, dst_file):
    key = os.urandom(8)
    # 创建DES对象
    desObj = pyDes.des(key, pyDes.ECB, padmode=pyDes.PAD_PKCS5)
    # 加密文件
    with open(src_file, 'rb') as sf:
        plaintext = sf.read()
    ciphertext = desObj.encrypt(plaintext)
    with open(dst_file, 'wb') as df:
        df.write(ciphertext)
    # 将DES密钥写入key_file中
    with open(key_file, 'wb') as kf:
        kf.write(key)


def sm4_encrypt_decrypt(src_file, key_file, dst_file):
    # 随机生成SM4密钥
    key = os.urandom(16)
    # 实例化sm4
    sm4Alg = sm4.CryptSM4()
    sm4Alg.set_key(key, sm4.SM4_ENCRYPT)  # 设置密钥
    with open(src_file, 'rb') as sf:
        src_data = sf.read()
    enRes = sm4Alg.crypt_ecb(src_data)
    # 将结果写回到文件
    with open(dst_file, 'wb') as df:
        df.write(enRes)
    # 将SM4密钥写入key_file中
    with open(key_file, 'wb') as kf:
        kf.write(key)


def one_time_pad_decrypt(src_file, key_file, dst_file):
    # 加密/解密过程与密钥异或
    with open(src_file, 'rb') as sf, open(key_file, 'rb') as kf:
        cipher_data = bytearray(sf.read())
        key_data = bytearray(kf.read())

        # 检查密钥长度是否与加密文件相同
        if len(cipher_data) > len(key_data):
            raise ValueError('Key length is shorter than cipher length.')

        # 异或
        src_data = bytearray(len(cipher_data))
        for i in range(len(cipher_data)):
            src_data[i] = cipher_data[i] ^ key_data[i]

    # 将结果写回到文件
    with open(dst_file, 'wb') as df:
        df.write(src_data)


def shift_decrypt(src_file, key_file, dst_file):
    # 从key_file中读取密钥
    with open(key_file, 'rb') as kf:
        k = kf.read(1)

    # 加密/解密过程与密钥异或
    with open(src_file, 'rb') as sf:
        cipher_data = bytearray(sf.read())

        src_data = bytearray(len(cipher_data))
        for i in range(len(cipher_data)):
            src_data[i] = (cipher_data[i] - k[0]) % 256

    # 将结果写回到文件
    with open(dst_file, 'wb') as df:
        df.write(src_data)


def des_decrypt(src_file, key_file, dst_file):
    # 从密钥文件中读取密钥
    with open(key_file, 'rb') as kf:
        key = kf.read()

    # 创建DES对象
    desObj = pyDes.des(key, pyDes.ECB, padmode=pyDes.PAD_PKCS5)

    # 解密文件
    with open(src_file, 'rb') as sf:
        ciphertext = sf.read()
    plaintext = desObj.decrypt(ciphertext)
    with open(dst_file, 'wb') as df:
        df.write(plaintext)


def sm4_decrypt(src_file, key_file, dst_file):
    sm4Alg = sm4.CryptSM4()
    # 从密钥文件中读取密钥
    with open(key_file, 'rb') as kf:
        key = kf.read()
    # 实例化sm4
    sm4Alg.set_key(key, sm4.SM4_DECRYPT)
    # 解密
    with open(src_file, 'rb') as sf:
        cipher_data = sf.read()
    deRes = sm4Alg.crypt_ecb(cipher_data)
    # 将解密后的二进制数据写入文件
    with open(dst_file, 'wb') as df:
        df.write(deRes)


choice = input("请选择要执行的操作:\n1. 一次一密加密\n2. 移位加密\n3. DES加密\n4. SM4加密\n在数字前面加上负号为解密操作\n按其他任意键退出:\n")

if choice == '1':
    one_time_pad_encrypt_decrypt('yuanshi/src.txt', 'miyao/key.bin', 'miwen/dst.bin')
elif choice == '-1':
    one_time_pad_decrypt('miwen/dst.bin', 'miyao/key.bin', 'yuanshi/src_decrypt.txt')
elif choice == '2':
    shift_encrypt_decrypt('yuanshi/src.txt', 'miyao/key.bin', 'miwen/dst.bin')
elif choice == '-2':
    shift_decrypt('miwen/dst.bin', 'miyao/key.bin', 'yuanshi/src_decrypt.txt')
elif choice == '3':
    des_encrypt_decrypt('yuanshi/src.txt', 'miyao/key.bin', 'miwen/dst.bin')
elif choice == '-3':
    des_decrypt('miwen/dst.bin', 'miyao/key.bin', 'yuanshi/src_decrypt.txt')
elif choice == '4':
    sm4_encrypt_decrypt('yuanshi/src.txt', 'miyao/key.bin', 'miwen/dst.bin')
elif choice == '-4':
     sm4_decrypt('miwen/dst.bin', 'miyao/key.bin', 'yuanshi/src_decrypt.txt')
else:
    print("无效的选择")


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值