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