MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / decrypt

Function decrypt

ciphers/caesar_cipher.py:91–160  ·  view source on GitHub ↗

decrypt ======= Decodes a given string of cipher-text and returns the decoded plain-text Parameters: ----------- * `input_string`: the cipher-text that needs to be decoded * `key`: the number of letters to shift the message backwards by to decode Optional:

(input_string: str, key: int, alphabet: str | None = None)

Source from the content-addressed store, hash-verified

89
90
91def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
92 """
93 decrypt
94 =======
95
96 Decodes a given string of cipher-text and returns the decoded plain-text
97
98 Parameters:
99 -----------
100
101 * `input_string`: the cipher-text that needs to be decoded
102 * `key`: the number of letters to shift the message backwards by to decode
103
104 Optional:
105
106 * `alphabet` (``None``): the alphabet used to decode the cipher, if not
107 specified, the standard english alphabet with upper and lowercase
108 letters is used
109
110 Returns:
111
112 * A string containing the decoded plain-text
113
114 More on the caesar cipher
115 =========================
116
117 The caesar cipher is named after Julius Caesar who used it when sending
118 secret military messages to his troops. This is a simple substitution cipher
119 where very character in the plain-text is shifted by a certain number known
120 as the "key" or "shift". Please keep in mind, here we will be focused on
121 decryption.
122
123 Example:
124 Say we have the following cipher-text:
125 ``Jgnnq, ecrvckp``
126
127 And our alphabet is made up of lower and uppercase letters:
128 ``abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ``
129
130 And our shift is ``2``
131
132 To decode the message, we would do the same thing as encoding, but in
133 reverse. The first letter, ``J`` would become ``H`` (remember: we are decoding)
134 because ``H`` is two letters in reverse (to the left) of ``J``. We would
135 continue doing this. A letter like ``a`` would shift back to the end of
136 the alphabet, and would become ``Z`` or ``Y`` and so on.
137
138 Our final message would be ``Hello, captain``
139
140 Further reading
141 ===============
142
143 * https://en.m.wikipedia.org/wiki/Caesar_cipher
144
145 Doctests
146 ========
147
148 >>> decrypt('bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo', 8)

Callers 2

brute_forceFunction · 0.70
caesar_cipher.pyFile · 0.70

Calls 1

encryptFunction · 0.70

Tested by

no test coverage detected