Advertisement
Guest User

playfair

a guest
Mar 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. # playfair algorithme bu sirai for u motherfucker ^_^
  2.  
  3. class PlayFairCipher:
  4.     def __init__(self, key):
  5.         self.imatrix =[
  6.                     'A', 'B', 'C', 'D', 'E',
  7.                     'F', 'G', 'H', 'I', 'K',
  8.                     'L', 'M', 'N', 'O', 'P',
  9.                     'Q', 'R', 'S', 'T', 'U',
  10.                     'V', 'W', 'X', 'Y', 'Z'
  11.                     ]
  12.         self.key = key
  13.         self.matrix = []
  14.         for e in self.key:
  15.             if e not in self.matrix:
  16.                 self.matrix.append(e)
  17.         for e in self.imatrix:
  18.             if e not in self.matrix:
  19.                 self.matrix.append(e)
  20.     def index(self, c):
  21.         i = self.matrix.index(c)
  22.         # i = y*5 + x donc x = i%5 et y = i/5
  23.         return [i % 5, i / 5]
  24.     def crypt(self, keyword):
  25.         keys = ''
  26.         for i in range(len(keyword)):
  27.             if i==len(keyword)-1:
  28.                 keys+=keyword[i]
  29.             elif keyword[i]==keyword[i+1]:
  30.                 keys+=keyword[i]
  31.                 keys+='X'
  32.             else:
  33.                 keys+=keyword[i]
  34.         if len(keys)%2!=0:
  35.             keys+='X'
  36.         i=0
  37.         __keys=[]
  38.         while i<len(keys):
  39.             __keys.append(keys[i]+keys[i+1])
  40.             i+=2
  41.         encrypted=''
  42.         for c in __keys:
  43.             x1, x2 = self.index(c[0])[0], self.index(c[1])[0]
  44.             y1, y2 = self.index(c[0])[1], self.index(c[1])[1]
  45.             if x1 == x2:
  46.                 encrypted+=self.matrix[((y1+1)*5+x1)%25]
  47.                 encrypted+=self.matrix[((y2+1)*5+x2)%25]
  48.             elif y1==y2:
  49.                 encrypted+=self.matrix[y1*5+(x1+1)%5]
  50.                 encrypted+=self.matrix[y2*5+(x2+1)%5]
  51.             else:
  52.                 encrypted+=self.matrix[y1*5+x2]
  53.                 encrypted+=self.matrix[y2*5+x1]
  54.         return encrypted
  55. CIPHER = PlayFairCipher('MATHEMATICS')
  56. print CIPHER.crypt('HELLOWORLD')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement