Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # playfair algorithme bu sirai for u motherfucker ^_^
- class PlayFairCipher:
- def __init__(self, key):
- self.imatrix =[
- 'A', 'B', 'C', 'D', 'E',
- 'F', 'G', 'H', 'I', 'K',
- 'L', 'M', 'N', 'O', 'P',
- 'Q', 'R', 'S', 'T', 'U',
- 'V', 'W', 'X', 'Y', 'Z'
- ]
- self.key = key
- self.matrix = []
- for e in self.key:
- if e not in self.matrix:
- self.matrix.append(e)
- for e in self.imatrix:
- if e not in self.matrix:
- self.matrix.append(e)
- def index(self, c):
- i = self.matrix.index(c)
- # i = y*5 + x donc x = i%5 et y = i/5
- return [i % 5, i / 5]
- def crypt(self, keyword):
- keys = ''
- for i in range(len(keyword)):
- if i==len(keyword)-1:
- keys+=keyword[i]
- elif keyword[i]==keyword[i+1]:
- keys+=keyword[i]
- keys+='X'
- else:
- keys+=keyword[i]
- if len(keys)%2!=0:
- keys+='X'
- i=0
- __keys=[]
- while i<len(keys):
- __keys.append(keys[i]+keys[i+1])
- i+=2
- encrypted=''
- for c in __keys:
- x1, x2 = self.index(c[0])[0], self.index(c[1])[0]
- y1, y2 = self.index(c[0])[1], self.index(c[1])[1]
- if x1 == x2:
- encrypted+=self.matrix[((y1+1)*5+x1)%25]
- encrypted+=self.matrix[((y2+1)*5+x2)%25]
- elif y1==y2:
- encrypted+=self.matrix[y1*5+(x1+1)%5]
- encrypted+=self.matrix[y2*5+(x2+1)%5]
- else:
- encrypted+=self.matrix[y1*5+x2]
- encrypted+=self.matrix[y2*5+x1]
- return encrypted
- CIPHER = PlayFairCipher('MATHEMATICS')
- print CIPHER.crypt('HELLOWORLD')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement