mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
Update playfire.ipynb
This commit is contained in:
parent
7becd05ff9
commit
609a711bf3
|
@ -33,31 +33,42 @@
|
|||
"source": [
|
||||
"class PlayFire:\n",
|
||||
" \"\"\"\n",
|
||||
" PlayFire class implements the Playfair cipher for encryption and decryption of messages.\n",
|
||||
" PlayFire class implements the Playfair \n",
|
||||
" cipher for encryption and decryption of messages.\n",
|
||||
"\n",
|
||||
" The Playfair cipher is a digraph substitution cipher that encrypts pairs of letters. It requires a key, which\n",
|
||||
" is used to create a 6x6 matrix of letters and digits, and processes the message in pairs.\n",
|
||||
" The Playfair cipher is a digraph substitution \n",
|
||||
" cipher that encrypts pairs of letters. It requires a key, which\n",
|
||||
" is used to create a 6x6 matrix of letters and \n",
|
||||
" digits, and processes the message in pairs.\n",
|
||||
"\n",
|
||||
" Attributes:\n",
|
||||
" key (str): The key used to generate the matrix.\n",
|
||||
" key_matrix (list): The 6x6 matrix used for encryption and decryption.\n",
|
||||
" extra (str): The extra character used to pad the message if the length is odd (default is 'x').\n",
|
||||
" key_matrix (list): The 6x6 matrix used for \n",
|
||||
" encryption and decryption.extra (str): The extra character \n",
|
||||
" used to pad the message if the length is odd (default is 'x').\n",
|
||||
"\n",
|
||||
" Methods:\n",
|
||||
" __verify_key(key): Verifies that the key is valid (contains unique characters).\n",
|
||||
" __make_matrix(): Creates a 6x6 matrix using the key and the remaining letters/digits.\n",
|
||||
" find_idx(pair): Finds the positions (row and column indices) of the pair of characters in the matrix.\n",
|
||||
" encrypt(msg): Encrypts the given message using the Playfair cipher.\n",
|
||||
" decrypt(msg): Decrypts the given encrypted message using the Playfair cipher.\n",
|
||||
" __verify_key(key): Verifies that the key is \n",
|
||||
" valid (contains unique characters).\n",
|
||||
" __make_matrix(): Creates a 6x6 matrix using \n",
|
||||
" the key and the remaining letters/digits.\n",
|
||||
" find_idx(pair): Finds the positions (row and column indices) \n",
|
||||
" of the pair of characters in the matrix.\n",
|
||||
" encrypt(msg): Encrypts the given message using \n",
|
||||
" the Playfair cipher.\n",
|
||||
" decrypt(msg): Decrypts the given encrypted \n",
|
||||
" message using the Playfair cipher.\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" def __init__(self, key, extra=\"x\"):\n",
|
||||
" \"\"\"\n",
|
||||
" Initializes the PlayFire cipher with a key and an optional extra character for padding.\n",
|
||||
" Initializes the PlayFire cipher with a key and \n",
|
||||
" an optional extra character for padding.\n",
|
||||
"\n",
|
||||
" Parameters:\n",
|
||||
" key (str): The key to generate the cipher matrix.\n",
|
||||
" extra (str, optional): The character used for padding the message if its length is odd. Defaults to 'x'.\n",
|
||||
" extra (str, optional): The character used for \n",
|
||||
" padding the message if its length is odd. Defaults to 'x'.\n",
|
||||
" \"\"\"\n",
|
||||
" self.key = self.__verify_key(key)\n",
|
||||
" self.key_matrix = self.__make_matrix()\n",
|
||||
|
@ -71,7 +82,8 @@
|
|||
" key (str): The key to verify.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" str: The valid key if it contains only unique characters, else prints an error.\n",
|
||||
" str: The valid key if it contains only unique \n",
|
||||
" characters, else prints an error.\n",
|
||||
" \"\"\"\n",
|
||||
" keyy = []\n",
|
||||
" for i in key:\n",
|
||||
|
@ -84,7 +96,8 @@
|
|||
"\n",
|
||||
" def __make_matrix(self):\n",
|
||||
" \"\"\"\n",
|
||||
" Creates a 6x6 matrix from the key by filling in remaining characters of the alphabet and digits.\n",
|
||||
" Creates a 6x6 matrix from the key by filling \n",
|
||||
" in remaining characters of the alphabet and digits.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" list: A 6x6 matrix for encryption and decryption.\n",
|
||||
|
@ -102,13 +115,16 @@
|
|||
"\n",
|
||||
" def find_idx(self, pair):\n",
|
||||
" \"\"\"\n",
|
||||
" Finds the row and column indices of the characters in the matrix.\n",
|
||||
" Finds the row and column indices of the \n",
|
||||
" characters in the matrix.\n",
|
||||
"\n",
|
||||
" Parameters:\n",
|
||||
" pair (list): A pair of characters whose positions are to be found in the matrix.\n",
|
||||
" pair (list): A pair of characters whose \n",
|
||||
" positions are to be found in the matrix.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" list: A list containing the row and column indices of both characters in the matrix.\n",
|
||||
" list: A list containing the row and column \n",
|
||||
" indices of both characters in the matrix.\n",
|
||||
" \"\"\"\n",
|
||||
" idxs = [6, 6]\n",
|
||||
" for i in range(6):\n",
|
||||
|
|
Loading…
Reference in New Issue
Block a user