编程语言
首页 > 编程语言> > python EDS加密和弱密钥验证

python EDS加密和弱密钥验证

作者:互联网

import math
import numpy as np
from pyDes import *
pc1 = [56, 48, 40, 32, 24, 16,  8,
		  0, 57, 49, 41, 33, 25, 17,
		  9,  1, 58, 50, 42, 34, 26,
		 18, 10,  2, 59, 51, 43, 35,
		 62, 54, 46, 38, 30, 22, 14,
		  6, 61, 53, 45, 37, 29, 21, 
		 13,  5, 60, 52, 44, 36, 28,
		 20, 12,  4, 27, 19, 11,  3
	]
pc2 = [
		13, 16, 10, 23,  0,  4,
		 2, 27, 14,  5, 20,  9,
		22, 18, 11,  3, 25,  7,
		15,  6, 26, 19, 12,  1,
		40, 51, 30, 36, 46, 54,
		29, 39, 50, 44, 32, 47,
		43, 48, 38, 55, 33, 52,
		45, 41, 49, 35, 28, 31
	]
left_rotations = [
		1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
	]
def create_sub_keys(a):
    print("密钥:",a)
    k=bin(int(a,16))
    k=k.split("b")[-1]
    while(len(k)<64):
        k='0'+k
    print("补全:",k)
    
    key=[]
    for l in range(16):
        sub=""
        subk=""
        for i in range(56):
            subk+=k[pc1[i]]
        kl=subk[0:28]
        kr=subk[28:]
        kl=kl[left_rotations[l]:]+kl[0:left_rotations[l]]
        kr=kr[left_rotations[l]:]+kr[0:left_rotations[l]]
        subk=kl+kr
        for j in range(48):
            sub+=subk[pc2[j]]
        key.append(sub)
    print("48位:\n",np.array(key))
    return
def desEncrypt(key=b"DESCRYPT",data = b"This message will be encrypted."):
    
    while(len(data)%8!=0):
        data+=b'a'
    # t=des(key, CBC, "\0\0\0\0\0\0\0\0")
    t=des(key)
    mi=t.encrypt(data)
    again=t.encrypt(mi)
    ming=t.decrypt(mi)
    print("数据:",data)
    print("密钥:",key)
    print("密文:",mi)
    print("密文再加密:",again)
    print("明文:",ming)
if __name__=="__main__":
    a=b"DESCRYPT"
    keyList=[
        "0101010101010101",
        "1F1F1F1F0F0F0E0E",
        "E0E0E0E0F1F1F1F1",
        "FEFEFEFEFEFEFEFE",

        "01FE01FE01FE01FE",
        "01E001E001F101F1",
        "FE01FE01FE01FE01",
        "E001E001F101F101"
    ]
    for i in keyList:
        create_sub_keys(i)
        desEncrypt(bytes.fromhex(i))
        print("\n\n")
    desEncrypt(a)
    key=input("\n\n请输入8个字节的十六进制数:\n")
    while(len(key)!=0):
        key=bytes.fromhex(key)
        desEncrypt(data=key)
        key=input("\n\n请输入8个字节的十六进制数:\n")


标签:22,16,python,54,29,36,EDS,密钥,import
来源: https://blog.csdn.net/Alasding/article/details/117266883