from PIL import Image from numpy import * from BitVector import BitVector as bv im = Image.open('FullMapEE.bmp') L = list(im.getdata()) for l, v in enumerate(L): if L[l] == 255: L[l] = 0 else: L[l] = 1 #add 5 more rows for i in range(850): L.append(0) for K in range(16): for i in range(170): # read entire columns buf = bv(size=8) for j in range(8): # read entire block buf[7 - j] = int(L[i + 170 * j + 170 * 8 * K]) print "0x" + buf.get_hex_string_from_bitvector(), ",", print
#! /usr/bin/env python from BitVector import BitVector as bv from numpy import poly1d as P n = 4 m = bv(bitlist=[1, 0, 0, 1, 1]) print "computing GF(2^n)" print "n = {0}".format(n) print "modulus m = " print P(list(m)) for i in range(0, 2 ** n): a1 = bv(bitlist=[0, 0, 1, 0]) a = a1 if i == 0: # 0 case. a = bv(bitlist=[0, 0, 0, 0]) else: for j in range(1, i): a = a.gf_multiply_modular(a1, m, n) print "============================================" print "g^{0}".format(i) print "Binary: {0}".format(a) print "Hex: {0}".format(a.getHexStringFromBitVector()) print "Polynomial:" print P(list(a))
#! /usr/bin/env python #perform the MixCols step in the AES cipher. from BitVector import BitVector as bv n = 8 #for GF(2^8) #modulus for AES #x^8 + x^4 + x^3 + x + 1 m = bv(bitlist = [1,0,0,0,1,1,0,1,1]) col00 = bv(intVal=0x7C,size = 8) col01 = bv(intVal=0xF2,size = 8) col02 = bv(intVal=0x2B,size = 8) col03 = bv(intVal=0xAB,size = 8) bv2 = bv(intVal=2, size = 8) bv3 = bv(intVal=3, size = 8) newcol00 = col00.gf_multiply_modular(bv2,m,n) ^ col01.gf_multiply_modular(bv3,m,n) ^ col02 ^ col03 newcol01 = col01.gf_multiply_modular(bv2,m,n) ^ col02.gf_multiply_modular(bv3,m,n) ^ col00 ^ col03 newcol02 = col02.gf_multiply_modular(bv2,m,n) ^ col03.gf_multiply_modular(bv3,m,n) ^ col00 ^ col01 newcol03 = col03.gf_multiply_modular(bv2,m,n) ^ col00.gf_multiply_modular(bv3,m,n) ^ col02 ^ col01 print newcol00.getHexStringFromBitVector() print newcol01.getHexStringFromBitVector() print newcol02.getHexStringFromBitVector() print newcol03.getHexStringFromBitVector()
#! /usr/bin/env python #effecient modular exponentiation algorithm #from page 283, Stallings crypto book 5th ed. #computes a^b mod n # inputs: a, b, n from BitVector import BitVector as bv k = 10 a = 5 bval = 596 b = bv(intVal=bval, size=k) n = 1234 print "performing the naive calculation:" print "result:{0}".format(a**bval % n) print "Performing fast modular exponentiation:" print "a:{0}, b:{1}, n:{2}".format(a,bval,n) c = 0 f = 1 for i in reversed(xrange(k)): j = k- 1 - i #to fix the wierd indexing in BitVector print "i:{3}, f:{0}, c:{1}, bi:{2}".format(f,c,b[j],i) c = 2 * c f = (f * f) % n if (b[j] == 1): c = c + 1