Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def otp(a, b, mode):
- """one-time pad
- mode=0: encrypt; mode=1: decrypt"""
- a, b = otpformat(a), otpformat(b)
- output = ''
- i = 0
- if mode == 0: sign = 1
- elif mode == 1: sign = -1
- for letter in a:
- output += chr((((ord(letter)-65)
- + (ord(b[i%len(b)])-65)*sign)%26) + 65)
- i += 1
- return output
- def otpformat(a):
- a = a.replace(' ','')
- a = a.upper()
- return a
- # m='polloesloco'; c='hvcpsjwcgjf'; k = 'IHJEEFEJIHJ'
- m='abc'; k='azy'; c = otp(m,k,0)
- def makenum(a):
- output = []
- a = otpformat(a)
- for letter in a:
- output.append(ord(letter) - 65)
- print letter,ord(letter) - 65
- return output
- def subber(c,m):
- output = []
- for i in range(len(c)):
- j = c[i]-m[i]
- if j < 0:
- j = j+26
- output.append(j)
- return output
- m = makenum('polloesloco')
- c = makenum('hvcpsjwcgjf')
- k = subber(c,m)
- print m
- print c
- print k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement