使用Python和OpenSSL简单模拟

发布时间:2019-09-26 07:21:27编辑:auto阅读(1983)

    本文使用Python和文件系统模拟保密通信的整个过程

    准备阶段

    本阶段完成系统初始状态模拟,直接看代码

    import os,shutil
    from Crypto.Cipher import DES3,XOR
    def xorencode(data,key):
        mm=XOR.new(key)
        cc=mm.encrypt(data)
        return cc
    def xordecode(data,key):
        cc=XOR.new(key)
        mm=cc.decrypt(data)
        return mm
    print "创建用户文件夹用于模拟通信"
    os.mkdir("Auser")
    os.mkdir("Buser")
    print "用户文件夹Auser、Buser创建完成"
    print "生成主密钥和基本密钥"
    AKMfile=open("Auser/KM",'w+')
    BKMfile=open("Buser/KM",'w+')
    akm=randkey(8)
    AKMfile.write(akm)
    AKMfile.flush()
    print "用户A的主密钥为"+akm.encode('hex')
    bkm=randkey(8)
    BKMfile.write(bkm)
    AKMfile.flush()
    print "用户B的主密钥为"+bkm.encode('hex')
    #生成共享密钥
    ABkey=randkey(32)
    print "共享密钥为"+ABkey.encode('hex')
    #用户A加密存储
    ABkeyA=xorencode(ABkey,akm)
    ABkeyfileA=open("Auser/KEK",'w+')
    ABkeyfileA.write(ABkeyA)
    ABkeyfileA.flush()
    #用户B加密存储
    ABkeyB=xorencode(ABkey,bkm)
    ABkeyfileB=open("Buser/KEK",'w+')
    ABkeyfileB.write(ABkeyB)
    ABkeyfileB.flush()
    print "模拟准备完成"

    用户A产生并发送消息

    from OpenSSL.rand import bytes as randkey
    import os,shutil
    from Crypto.Cipher.AES import *
    from Crypto.Cipher import DES3,XOR
    def AESencode(data, key, IV):  # AES加密
        addlen = 16 - len(data) % 16
        data = data + addlen * chr(addlen)
        tocode = new(key, 2, IV)
        encode = tocode.encrypt(data)
        return encode
    
    def AESdecode(code, key, IV):  # AES解密
        tomm = new(key, 2, IV)
        mm = tomm.decrypt(code)
        return mm
    def des3encode(data,key,IV):
        # MODE_CBC = 2
        # MODE_CFB = 3
        # MODE_CTR = 6
        # MODE_ECB = 1
        # MODE_OFB = 5
        # MODE_OPENPGP = 7
        # MODE_PGP = 4
        mm=DES3.new(key,3,IV)
        cc=mm.encrypt(data)
        return cc
    
    def des3encode(data,key,IV):
        addlen = 16 - len(data) % 16
        data = data + addlen * chr(addlen)
        # MODE_CBC = 2
        # MODE_CFB = 3
        # MODE_CTR = 6
        # MODE_ECB = 1
        # MODE_OFB = 5
        # MODE_OPENPGP = 7
        # MODE_PGP = 4
        mm=DES3.new(key,3,IV)
        cc=mm.decrypt(data)
        return cc
    def xorencode(data,key):
        mm=XOR.new(key)
        cc=mm.encrypt(data)
        return cc
    def xordecode(data,key):
        cc=XOR.new(key)
        mm=cc.decrypt(data)
        return mm
    message='''
    你是烧鸡吗?当你收到此加密消息时,我十分激动的告诉你:
    你这门课挂了!
    哈哈哈哈哈哈哈哈哈哈哈!
    '''
    AKMfile=open("Auser/KM",'r')
    akm=AKMfile.read(8)
    ABkeyfileA=open("Auser/KEK",'r')
    ABkey=ABkeyfileA.read(16)
    ABkey=xordecode(ABkey,akm)#解密出加密密钥
    talkkeym=randkey(32)
    talkkeyfile=open("Auser/talkkey",'w')
    talkkeyfile.write(talkkeym)
    talkkeyfile.close()
    talkkeyc=des3encode(talkkeym,ABkey,'01020304')
    messagecc=AESencode(message,talkkeyc,'1234567887654321')
    messagefile=open("Auser/message",'w')
    messagefile.write(messagecc)
    messagefile.close()
    print "消息加密已完成\n消息递送中..........."
    shutil.copyfile("Auser/talkkey","Buser/talkkey")
    shutil.copyfile("Auser/message","Buser/message")
    print "消息传递完成"
    

    用户B收到消息后的操作

    BKMfile=open("Buser/KM",'r')
    bkm=BKMfile.read(8)
    ABkeyfileB=open("Buser/KEK",'r')
    ABkey=ABkeyfileB.read(16)
    ABkey=xordecode(ABkey,bkm)#解密出加密密钥
    talkkeyfile=open("Buser/talkkey",'r')
    talkkeym=talkkeyfile.read(32)
    talkkeyc=des3encode(talkkeym,ABkey,'01020304')
    messagefile=open("Buser/message",'r')
    lens=os.path.getsize("Buser/message")
    messagecc=messagefile.read(lens)
    message=AESdecode(messagecc,talkkeyc,'1234567887654321')
    print message

关键字