Advertisement
Guest User

Eugene Tan

a guest
Sep 7th, 2009
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. s = [16, 42, 28, 3, 26, 0, 31, 46, 27, 14, 49, 62, 37, 56, 23, 6, 40, 48, 53, 8, 20, 25, 3, 1, 2, 63, 15, 34, 55, 21, 39, 57, 54, 45, 47, 13, 7, 44, 61, 9, 60, 32, 22, 29, 52, 19, 12, 50, 5, 51, 11, 18, 59, 41, 36, 30, 17, 38, 10, 4, 58, 43, 35, 24]
  2.  
  3. p = [24, 5, 15, 23, 14, 32, 19, 18, 26, 17, 6, 12, 34, 9, 8, 20, 28, 0, 2, 21, 29, 11, 33, 22, 30, 31, 1, 25, 3, 35, 16, 13, 27, 7, 10, 4]
  4.  
  5. key = 0
  6.  
  7. def sbox(x):
  8.     '''S-box function'''
  9.     return s[x]
  10.  
  11. def pbox(x):
  12.     '''P-box function'''
  13.     # if the texts are more than 32 bits,
  14.     # then we have to use longs
  15.     y = 0l
  16.  
  17.     # for each bit to be shuffled
  18.     for i in range(len(p)):
  19.  
  20.         # if the original bit position
  21.         # is a 1, then make the result
  22.         # bit position have a 1
  23.         if (x & (1l << i)) != 0:
  24.             y = y ^ (1l << p[i])
  25.    
  26.     return y
  27.  
  28. def demux(x):
  29.     '''Demultiplex, takes in 36-bit to six 6-bit values'''
  30.     y = []
  31.     for i in range(0, 6):
  32.         y.append((x >> (i * 6)) & 0x3f)
  33.  
  34.     return y
  35.  
  36. def mux(x):
  37.     '''Multiplex, takes in six 6-bit to 36-bit values'''
  38.     y = 0l
  39.     for i in range(0, 6):
  40.         y = y ^ (x[i] << (i * 6))
  41.  
  42.     return y
  43.  
  44. def mix(p, k):
  45.     '''Key mixing'''
  46.     v = []
  47.     key = demux(k)
  48.     for i in range(0, 6):
  49.         v.append(p[i] ^ key[i])
  50.  
  51.     return v
  52.  
  53. def round(p, k):
  54.     '''Round function'''
  55.     u = []
  56.  
  57.     # Calculate the S-boxes
  58.     for x in demux(p):
  59.         u.append(sbox(x))
  60.  
  61.     # Run through the P-box
  62.     v = demux(pbox(mux(u)))
  63.  
  64.     # XOR in the key
  65.     w = mix(v, k)
  66.  
  67.     # Glue back together, return
  68.     return mux(w)
  69.  
  70. def encrypt(p, rounds):
  71.     '''Encryption'''
  72.     key = 0b111100001111000011111100001111000011
  73.     x = p
  74.     for i in range(rounds):
  75.         x = round(x, key)
  76.  
  77.     return x
  78.  
  79. def apbox(x):
  80.     y = 0l
  81.     for i in range(len(p)):
  82.         if (x & (1l << i)) != 0:
  83.             pval = p.index(i)
  84.             y = y ^ (1l << pval)
  85.     return y
  86.  
  87. def asbox(x):
  88.     return s.index(x)
  89.  
  90. def unround(c, k):
  91.     '''Opposite of the round function'''
  92.     x = demux(c)
  93.     u = mix(x, k)
  94.     v = demux(apbox(mux(u)))
  95.     w = []
  96.     for s in v:
  97.         w.append(asbox(s))
  98.  
  99.     return mux(w)
  100.  
  101. def decrypt(c, rounds):
  102.     '''Decryption function'''
  103.     key = 0b111100001111000011111100001111000011
  104.     x = c
  105.     for i in range(rounds):
  106.         x = unround(x, key)
  107.  
  108.     return x
  109.  
  110. if __name__ == '__main__':
  111.     #import pdb
  112.     #pdb.set_trace()
  113.     plaintext = 0b111100001111000011110000111100001111
  114.     ciphertext = encrypt(plaintext, 1)
  115.     print 'plaintextA: ', plaintext
  116.     print 'ciphertext: ', ciphertext
  117.     print 'plaintextB: ', decrypt(ciphertext, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement