Advertisement
Korotkodul

20-21_N2

Mar 26th, 2023 (edited)
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. def dig(l):
  2.     if ord(l) >= ord('A') and ord(l) <= ord('F'):
  3.         return 10 + ord(l) - ord('A')
  4.     return ord(l) - ord('0')
  5.  
  6. def from_16(s):
  7.     ar = []
  8.     for i in s:
  9.         ar.append(dig(i))
  10.     ar = ar[::-1]
  11.     tot = 0
  12.     for i in range(len(s)):
  13.         tot += (16**i) * ar[i]
  14.     return tot
  15.  
  16.  
  17. def xor(a, b):
  18.     return a ^ b
  19.  
  20. def shift(x):
  21.     s = bin(x)
  22.     t = s[len(s) - 1] + s[:len(s) - 1]
  23.     t = t[3:]
  24.     res = int(t, 2)
  25.     return res
  26.  
  27. def K_shift(x, K):
  28.     for i in range(K):
  29.         x = shift(K)
  30.     return x
  31.  
  32. def f(a, b, K):
  33.     a = from_16(a)
  34.     b = from_16(b)
  35.     res =  xor(a,b)
  36.     res = K_shift(res, K)
  37.     return res
  38. a = '1A2B'
  39. b = 'FFFF'
  40. K = 4
  41. must = from_16('4E5D')
  42.  
  43. print(f(a,b, K), must)
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement