daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Div = (1<<8) + (1<<4) + (1<<3) + 2 + 1
  2. global sbox
  3. sbox = [[0]*16 for x in range(16)]
  4. def add(a,b):
  5.     return a^b
  6.  
  7. def mult(a,b):
  8.     out = 0
  9.     while(b!=0):
  10.         if((b&1)!=0):
  11.             out=add(out,a)
  12.         b=b>>1
  13.         a=a<<1
  14.     return out
  15.  
  16.  
  17. def mod(a,b):
  18.     abits = 0
  19.     bbits = 0
  20.     tmp = a
  21.     while(tmp!=0):
  22.         abits+=1
  23.         tmp = tmp>>1
  24.     tmp = b
  25.     while(tmp!=0):
  26.         bbits+=1
  27.         tmp = tmp>>1
  28.     div = b
  29.     while(bbits<abits):
  30.         bbits+=1
  31.         div = div<<1
  32.        
  33.    
  34.     while(div>=b):
  35.         if(a & (1<<(bbits-1))):
  36.             a = a^div
  37.         div=div>>1
  38.         bbits-=1
  39.     return a
  40.    
  41. def inverseMod(a):
  42.     for i in range(256):
  43.         if(mod(mult(a,i),Div) == 1):
  44.             return i
  45.    
  46.     return 0
  47.  
  48. def sboxByte(a):
  49.     invA = inverseMod(a)
  50.     b = []
  51.     bp = []
  52.     x = [1,1,0,0,0,1,1,0]
  53.     for i in range(8):
  54.         if(invA & (1<<i)):
  55.             b.append(1)
  56.         else:
  57.             b.append(0)
  58.     for i in range(8):
  59.         bp.append(b[i] ^ b[(i+4)%8] ^ b[(i+5)%8] ^ b[(i+6)%8] ^ b[(i+7)%8] ^ x[i])
  60.     sboxVal = 0
  61.     for i in range(8):
  62.         sboxVal = sboxVal<<1
  63.         sboxVal += bp[7-i]
  64.     sbox[a>>4][a%(1<<4)] = sboxVal
  65.    
  66. def fillSBox():
  67.     for i in range(256):
  68.         sboxByte(i)
  69.  
  70.        
  71. fillSBox()
  72. # print(sbox)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top