コード例 #1
0
def main():
    f = open('ciphertext.txt','w')
    key = int(sys.argv[2],16)  
    myBlockEncryptCipher = blockCipher(key)
    myBlockEncryptCipher.blocks = readInput()

    for block in myBlockEncryptCipher.blocks:
    	myBlockEncryptCipher.block = int(''.join(block),16)	    
    	myBlockEncryptCipher.whitening()
    	myBlockEncryptCipher.subkeyGenerator()
    	for i in range (0,20):
    		myBlockEncryptCipher.fFunction()
    		temp = myBlockEncryptCipher.rightRotate16bitsR2()
    		myBlockEncryptCipher.r2 = myBlockEncryptCipher.r0
    		myBlockEncryptCipher.r0 = temp
    		r3_ = myBlockEncryptCipher.leftRotate16bitsR3()
    		temp = r3_ ^ myBlockEncryptCipher.F1
    		myBlockEncryptCipher.r3 = myBlockEncryptCipher.r1
    		myBlockEncryptCipher.r1 = temp
    		#print "round", myBlockEncryptCipher.round, format(myBlockEncryptCipher.r0,"04x"),format	(myBlockEncryptCipher.r1,"04x"),format(myBlockEncryptCipher.r2,"04x"),format(myBlockEncryptCipher.r3,"04x")
    		myBlockEncryptCipher.round += 1
   
    	ciphertext = myBlockEncryptCipher.outputWhitening()
    	#print 'Ciphertext after 20 rounds',format(myBlockEncryptCipher.C0,"04x"),format(myBlockEncryptCipher.C1,"04x"),format(myBlockEncryptCipher.C2,"04x"),format(myBlockEncryptCipher.C3,"04x")
	f.write(format(myBlockEncryptCipher.C0,"04x")+format(myBlockEncryptCipher.C1,"04x")+format(myBlockEncryptCipher.C2,"04x")+format(myBlockEncryptCipher.C3,"04x"))
コード例 #2
0
def main():
	f = open('decrypted.txt','w')
	key = int(sys.argv[2],16)
    	myBlockDecryptCipher = blockCipher(key)
    	myBlockDecryptCipher.blocks = readInput()

   	for block in myBlockDecryptCipher.blocks:
		myBlockDecryptCipher.block = int(''.join(block),16)
    		myBlockDecryptCipher.whitening()
		myBlockDecryptCipher.subkeyGenerator()
		myBlockDecryptCipher.round = 19
		for i in range(0,20):				
			myBlockDecryptCipher.fFunction()
			previousR0 = myBlockDecryptCipher.r0
                	previousR1 = myBlockDecryptCipher.r1
			myBlockDecryptCipher.r0 = myBlockDecryptCipher.leftRotate16bitsR2() ^ 	myBlockDecryptCipher.F0
			myBlockDecryptCipher.r1 = myBlockDecryptCipher.rightRotate16bitsR3()
			myBlockDecryptCipher.r2 = previousR0
			myBlockDecryptCipher.r3 = previousR1
		
			#print "round", myBlockDecryptCipher.round, format(myBlockDecryptCipher.r0,"04x"),format(myBlockDecryptCipher.r1,"04x"),format(myBlockDecryptCipher.r2,"04x"),format(myBlockDecryptCipher.r3,"04x")
			myBlockDecryptCipher.round -= 1

		plaintext = myBlockDecryptCipher.outputWhitening()
		#print 'Plaintext after 20 rounds',format(myBlockDecryptCipher.C0,"04x"),format(myBlockDecryptCipher.C1,"04x"),format(myBlockDecryptCipher.C2,"04x"),format(myBlockDecryptCipher.C3,"04x")	
		#print format(myBlockDecryptCipher.C0,"04x").decode("hex"),format(myBlockDecryptCipher.C1,"04x").decode("hex"),format(myBlockDecryptCipher.C2,"04x").decode("hex"),format(myBlockDecryptCipher.C3,"04x").decode("hex")
		f.write(format(myBlockDecryptCipher.C0,"04x").decode("hex")+format(myBlockDecryptCipher.C1,"04x").decode("hex")+format(myBlockDecryptCipher.C2,"04x").decode("hex")+format(myBlockDecryptCipher.C3,"04x").decode("hex"))