示例#1
0
文件: main.py 项目: worshipone/DES
def test_OFB(args):
    plain,key,iv = rw.read(args)
    readname = "b.txt"
    writename = "temp.txt"
    perplain = "1"
    start = time.time()
    with open(readname,'r') as f1:
        with open(writename,"w") as f2:
            while(len(perplain)!=0):
                perplain = f1.read(8)
                if len(perplain)==0:
                    break
                if len(perplain)!=8:
                    while(len(perplain)!=8):
                        perplain = "0"+perplain
                temp=DES(iv,key)[:8]
                ox = bin(int(temp,2)^int(perplain,2))[2:]
                while(len(ox)!=8):
                    ox = "0"+ox
                iv = iv[8:]+temp
                f2.write(ox)   
    end = time.time()
    with open("recording.txt","a") as f:
        f.write("50MB en_decode for 50 times by OFB costs "+str((end-start)*100*50)+"ms\n")
    print("over")
示例#2
0
def test_CBC(args):
    plain, key, iv = rw.read(args)
    readname = "b.txt"
    writename = "temp.txt"
    perplain = "1"
    start = time.time()
    with open(readname, 'r') as f1:
        with open(writename, "w") as f2:
            while (len(perplain) != 0):
                perplain = f1.read(64)
                if len(perplain) == 0:
                    break
                if len(perplain) != 64:
                    while (len(perplain) != 64):
                        perplain = "0" + perplain
                ox = bin(int(iv, 2) ^ int(perplain, 2))[2:]
                while (len(ox) != 64):
                    ox = "0" + ox
                iv = DES(ox, key)
                f2.write(iv)
    end = time.time()
    with open("recording.txt", "a") as f:
        f.write("50MB en_decode for 50 times by CBC costs " +
                str((end - start) * 100 * 50) + "ms\n")
    print("over")
示例#3
0
def show(args):
    plain, key, iv = rw.read(args)
    mode = args.mode
    if mode == "all":
        ecb_cipher = ECB(plain, key)
        ecb_plain = ECB(ecb_cipher, key, 0)
        print("ecb_cipher is", hex(int(ecb_cipher, base=2)).upper())
        ctf.words.append("ecb_cipher is" +
                         str(hex(int(ecb_cipher, base=2)).upper()) + "\n")
        print("ecb_plain  is", hex(int(ecb_plain, base=2)).upper())
        cbc_cipher = CBC(plain, key, iv)
        cbc_plain = de_CBC(cbc_cipher, key, iv)
        print("cbc_cipher is", hex(int(cbc_cipher, base=2)).upper())
        ctf.words.append("cbc_cipher is" +
                         str(hex(int(cbc_cipher, base=2)).upper()) + "\n")
        print("cbc_plain is", hex(int(cbc_plain, base=2)).upper())
        cfb_cipher = CFB(plain, key, iv)
        cfb_plain = CFB(cfb_cipher, key, iv)
        print("cfb_cipher is", hex(int(cfb_cipher, base=2)).upper())
        ctf.words.append("cfb_cipher is" +
                         str(hex(int(cfb_cipher, base=2)).upper()) + "\n")
        print("cfb_plain is", hex(int(cfb_plain, base=2)).upper())
        ofb_cipher = OFB(plain, key, iv)
        ofb_plain = OFB(ofb_cipher, key, iv)
        print("ofb_cipher is", hex(int(ofb_cipher, base=2)).upper())
        ctf.words.append("ofb_cipher is" +
                         str(hex(int(ofb_cipher, base=2)).upper()) + "\n")
        print("ofb_plain is", hex(int(ofb_plain, base=2)).upper())
    elif mode == "ECB":
        ecb_cipher = ECB(plain, key)
        ecb_plain = ECB(ecb_cipher, key, 0)
        print("ecb_cipher is", hex(int(ecb_cipher, base=2)).upper())
        ctf.words.append("ecb_cipher is" +
                         str(hex(int(ecb_cipher, base=2)).upper()) + "\n")
        print("ecb_plain  is", hex(int(ecb_plain, base=2)).upper())
    elif mode == "CBC":
        cbc_cipher = CBC(plain, key, iv)
        cbc_plain = de_CBC(cbc_cipher, key, iv)
        print("cbc_cipher is", hex(int(cbc_cipher, base=2)).upper())
        ctf.words.append("cbc_cipher is" +
                         str(hex(int(cbc_cipher, base=2)).upper()) + "\n")
        print("cbc_plain is", hex(int(cbc_plain, base=2)).upper())
    elif mode == "CFB":
        cfb_cipher = CFB(plain, key, iv)
        cfb_plain = CFB(cfb_cipher, key, iv)
        print("cfb_cipher is", hex(int(cfb_cipher, base=2)).upper())
        ctf.words.append("cfb_cipher is" +
                         str(hex(int(cfb_cipher, base=2)).upper()) + "\n")
        print("cfb_plain is", hex(int(cfb_plain, base=2)).upper())
    elif mode == "OFB":
        ofb_cipher = OFB(plain, key, iv)
        ofb_plain = OFB(ofb_cipher, key, iv)
        print("ofb_cipher is", hex(int(ofb_cipher, base=2)).upper())
        ctf.words.append("ofb_cipher is" +
                         str(hex(int(ofb_cipher, base=2)).upper()) + "\n")
        print("ofb_plain is", hex(int(ofb_plain, base=2)).upper())
    else:
        print("please input right modes")
示例#4
0
文件: main.py 项目: worshipone/DES
def test_ECB(args):
    plain,key,iv = rw.read(args)
    readname = "b.txt"
    writename = "temp.txt"
    de_name = "detemp.txt" 
    perplain = "1"
    start=time.time()
    with open(readname,'r') as f1:
        with open(writename,'w')as f2:
            while(len(perplain)!=0):        
                perplain = f1.read(64)
                f2.write(ECB(perplain,key,flag=1))
    perplain = "1"
    with open(writename,'r') as f3:
        with open(de_name,'w')as f4:
            while(len(perplain)!=0):        
                perplain = f3.read(64)
                f4.write(ECB(perplain,key,flag=0))
    end=time.time()
    with open("recording.txt","a") as f:
        f.write("50MB en_decode for 50 times by ECB costs "+str((end-start)*50*50)+"ms\n")
    print("over")