Try CoCalc Published Files Email HelpDocumentationSign In
Path: Simplified DES.sagews
Views: 202
License: none
Image: ubuntu2004
Project: MATH 307
Edit | Raw | Embed | Download
import numpy Sbox1 = numpy.mat([["101","010","001","110","011","100","111","000"],["001","100","110","010","000","111","101","011"]]) Sbox2 = numpy.mat([["100","000","110","101","111","001","011","010"],["101","011","000","111","110","010","001","100"]]) S1={"0110" : "111", "1110" : "101", "0000" : "101", "1001" : "100", "1100" : "000", "0010" : "001", "0111" : "000", "1111" : "011", "1010" : "110", "0100" : "011", "0101" : "100", "1101" : "111", "0001" : "010", "1000" : "001", "1011" : "010", "0011" : "110"} S2={"0110" : "011", "1110" : "001", "0000" : "100", "1001" : "011", "1100" : "110", "0010" : "110", "0111" : "010", "1111" : "100", "1010" : "000", "0100" : "111", "0101" : "001", "1101" : "010", "0001" : "000", "1000" : "101", "1011" : "111", "0011" : "101"} def EightBitsKey (key, i): iBit = (i - 1) % 9 + 1 a = key + key #print a[iBit:iBit+8] return a[iBit:iBit+8] #return(substring(a, xrange(iBit,iBit + 7))) def Ln(thisString): return thisString[0:6] def Rn(thisString): return thisString[6:12] def Expander(Right): R12=Right[0:1+1] R56=Right[4:5+1] R43 = Right[3:2-1:-1] return R12+R43+R43+R56 def XOR(x,y): if(x==y): return "0" else: return "1" def XORnbits(R,key,n): buffer = "" for i in range(0,n): buffer += XOR(R[i],key[i]) return buffer #cat(seq(XOR(substring(R,i), substring(key,i)),i=1..n)): def SDESoneRound (message, key, numround): ikey = EightBitsKey(key, numround) L0 = Ln(message) R0 = Rn(message) expandedR = Expander(R0) Sboxinput = XORnbits(expandedR, ikey, 8) Sboxesoutput = S1[Sboxinput[:4]] + S2[Sboxinput[4:]] return(R0 + XORnbits(Sboxesoutput, L0, 6)) def SDES (message, key, Rounds): temp = message for i in range(1, Rounds + 1): temp = SDESoneRound(temp, key, i) cgret = temp return(cgret) def DoubleSDES (message,key1,key2): i=0 temp = message while True: for j in range(1, 4): temp = SDESoneRound(temp, key1, j) for j in range(1, 4): temp = SDESoneRound(temp, key2, j) if temp == message: print(temp) return(i) i+=1 return(temp) def SDESdecr (message, key, Rounds): L = Ln(message) R = Rn(message) temp = R + L for i in range(0, Rounds - 1 + 1): temp = SDESoneRound(temp, key, Rounds - i) L = Ln(temp) R = Rn(temp) temp = R + L return(temp)
############################################################################################ # Encryption of the plaintext 101010110101 using the key 110110010 and 4-round S-DES # ############################################################################################ key="110110010" message="101010110101" SDES(message,key,4)
'000010110111'
key="110110010" message="101010110101" L0 = Ln(message) R0 = Rn(message) ER0 = Expander(R0) k1= EightBitsKey(key,0) SboxInputs=XORnbits(ER0,k1,8) L0 R0 ER0 k1 SboxInputs
'101010' '110101' '11101001' '11011001' '00110000'
############################################################################################ # Encryption of the plaintext 101010110101 using the key 110011101 and 6-round S-DES # ############################################################################################ key="110011101" message="101010110101" Encr=SDES(message,key,6) ############################################################################################ # Decryption of the ciphertext from the previouse example Encr-000100000001 # ############################################################################################ Plain = SDESdecr(Encr,key,6) Encr ############################################################################################ # Note that the decryption recovers the plaintext i.e. Plain = message # ############################################################################################ Plain message
'000100000001' '101010110101' '101010110101'