Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Math 314
Views: 1617
attach("saes.py") saes = SAES() def xor(A,B): return bin(''.join('0' if i == j else '1' for i, j in zip(A,B))) def L8(A): return A[0:8] def R8(A): return A[8:16]
bin = BinaryStrings() key = bin("1110111011110000"); key P = bin("0000000000000001"); P
1110111011110000 0000000000000001
C=saes(P,key,algorithm="encrypt"); C
0101001000010100
C2=saes(C,key,algorithm="encrypt"); C2
0101110011101001
saes(C,key,algorithm="decrypt")
1000011100111011
#Determine Roundkeys MS = MatrixSpace(FiniteField(16, "x"), 2, 2) matK = MS(saes.binary_to_GF(key)).transpose() #Convert Key to Matrix rkey0=saes.round_key(matK,0) rkey1=saes.round_key(matK,1) rkey2=saes.round_key(matK,2) print "K0",saes.GF_to_binary(rkey0) print "K1",saes.GF_to_binary(rkey1) print "K2",saes.GF_to_binary(rkey2)
K0 0100101011110101 K1 1101110100101000 K2 1000011110101111
saes.binary_to_GF(P)
[x^3, x^2 + x + 1, x + 1, x^3 + x + 1]
#round 0 matP = MS(saes.binary_to_GF(P)).transpose() C0=saes.add_key(matP, rkey0) print "Add round key 0:",saes.GF_to_binary(C0)
Add round key 0: 1100110111001110
#round 1 S1=saes.nibble_sub(C0, algorithm="encrypt") print "Substitution",saes.GF_to_binary(S1) SR1=saes.shift_row(S1) print "Switch Rows",saes.GF_to_binary(SR1) MC1=saes.mix_column(SR1) print "MixColumns",saes.GF_to_binary(MC1) C1 = saes.add_key(MC1, rkey1) print "Add round key 1:",saes.GF_to_binary(C1)
Substitution 1100111011001111 Switch Rows 1100111111001110 MixColumns 0101101000011011 Add round key 1: 1000011100110011
#round 2 S2=saes.nibble_sub(C1, algorithm="encrypt") print "Substitution",saes.GF_to_binary(S2) SR2=saes.shift_row(S2) print "Switch Rows",saes.GF_to_binary(SR2) C2 = saes.add_key(SR2, rkey2) print "Add round key 2:",saes.GF_to_binary(C2)
Substitution 0110010110111011 Switch Rows 0110101110110101 Add round key 2: 1110110000011010
0010010001110000
0010010001110000
1101011100101000
P
1101011100101000
P1 = bin("1101011100101000"); P1 P2 = bin("1110001011000111"); P2 C0 = bin("0000000000000000"); C0 X1 = bin("0101000010001010"); X1
#ECB: C1=saes(P1,key,algorithm="encrypt");C1 C2=saes(P2,key,algorithm="encrypt");C2
0010010011101100 0011101000001010
#CBC: C1=saes(xor(P1,C0),key,algorithm="encrypt");C1 C2=saes(xor(P2,C1),key,algorithm="encrypt");C2
0010010011101100 0010010110001101
saes(C1,key,algorithm="decrypt")
1101011100101000
xor(saes(C2,key,algorithm="decrypt"),C1)
1110001011000111
P2
1110001011000111
#CBC C1=saes(xor(P,Z),key,algorithm="encrypt");C1 C2=saes(xor(Q,C1),key,algorithm="encrypt");C2
0010010001110000 1000011101110101
#Decrypting CBC saes(C1,key,algorithm="decrypt")
1101011100101000
xor(saes(C2,key,algorithm="decrypt"),C1)
1110001011000111
Q
1110001011000111
Z = bin("0000000000000000"); Z P1 = bin("00110011") P2 = bin("00110000") P3= bin("10101101") P4 = bin("11000000")
0000000000000000
#CFB O1=L8(saes(X1,key,algorithm="encrypt"));O1 C1=xor(O1,P1);C1 X2=R8(X1)*C1;X2
00000000 00110011 1000101000110011
O2=L8(saes(X2,key,algorithm="encrypt"));O2
11011010
C2=xor(O2,P2);C2
11101010
X3=R8(X2)*C2;X3
0011001111101010