mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Update caesar_cipher.py
This commit is contained in:
parent
ed19b1cf0c
commit
7015729091
@ -5,25 +5,25 @@ from string import ascii_letters
|
||||
|
||||
def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
"""
|
||||
encrypt
|
||||
encrypt:
|
||||
=======
|
||||
Encodes a given string with the caesar cipher and returns the encoded
|
||||
message
|
||||
message.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
* input_string: the plain-text that needs to be encoded
|
||||
* key: the number of letters to shift the message by
|
||||
* input_string: the plain-text that needs to be encoded.
|
||||
* key: the number of letters to shift the message by.
|
||||
|
||||
Optional:
|
||||
* alphabet (None): the alphabet used to encode the cipher, if not
|
||||
specified, the standard english alphabet with upper and lowercase
|
||||
letters is used
|
||||
letters is used.
|
||||
|
||||
Returns:
|
||||
* A string containing the encoded cipher-text
|
||||
* A string containing the encoded cipher-text.
|
||||
|
||||
More on the caesar cipher
|
||||
More on the caesar cipher:
|
||||
=========================
|
||||
The caesar cipher is named after Julius Caesar who used it when sending
|
||||
secret military messages to his troops. This is a simple substitution cipher
|
||||
@ -37,7 +37,7 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
And our alphabet is made up of lower and uppercase letters:
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
And our shift is "2"
|
||||
And our shift is "2".
|
||||
|
||||
We can then encode the message, one letter at a time. "H" would become "J",
|
||||
since "J" is two letters away, and so on. If the shift is ever two large, or
|
||||
@ -46,11 +46,11 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
|
||||
Our final message would be "Jgnnq, ecrvckp"
|
||||
|
||||
Further reading
|
||||
Further reading:
|
||||
===============
|
||||
* https://en.m.wikipedia.org/wiki/Caesar_cipher
|
||||
|
||||
Doctests
|
||||
Doctests:
|
||||
========
|
||||
>>> encrypt('The quick brown fox jumps over the lazy dog', 8)
|
||||
'bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo'
|
||||
@ -83,24 +83,24 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
|
||||
def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
"""
|
||||
decrypt
|
||||
decrypt:
|
||||
=======
|
||||
Decodes a given string of cipher-text and returns the decoded plain-text
|
||||
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
|
||||
* input_string: the cipher-text that needs to be decoded.
|
||||
* key: the number of letters to shift the message backwards by to decode.
|
||||
|
||||
Optional:
|
||||
* alphabet (None): the alphabet used to decode the cipher, if not
|
||||
specified, the standard english alphabet with upper and lowercase
|
||||
letters is used
|
||||
letters is used.
|
||||
|
||||
Returns:
|
||||
* A string containing the decoded plain-text
|
||||
* A string containing the decoded plain-text.
|
||||
|
||||
More on the caesar cipher
|
||||
More on the caesar cipher:
|
||||
=========================
|
||||
The caesar cipher is named after Julius Caesar who used it when sending
|
||||
secret military messages to his troops. This is a simple substitution cipher
|
||||
@ -115,7 +115,7 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
And our alphabet is made up of lower and uppercase letters:
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
And our shift is "2"
|
||||
And our shift is "2".
|
||||
|
||||
To decode the message, we would do the same thing as encoding, but in
|
||||
reverse. The first letter, "J" would become "H" (remember: we are decoding)
|
||||
@ -125,11 +125,11 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
|
||||
Our final message would be "Hello, captain"
|
||||
|
||||
Further reading
|
||||
Further reading:
|
||||
===============
|
||||
* https://en.m.wikipedia.org/wiki/Caesar_cipher
|
||||
|
||||
Doctests
|
||||
Doctests:
|
||||
========
|
||||
>>> decrypt('bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo', 8)
|
||||
'The quick brown fox jumps over the lazy dog'
|
||||
@ -148,26 +148,26 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
|
||||
def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str]:
|
||||
"""
|
||||
brute_force
|
||||
brute_force:
|
||||
===========
|
||||
Returns all the possible combinations of keys and the decoded strings in the
|
||||
form of a dictionary
|
||||
form of a dictionary.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
* input_string: the cipher-text that needs to be used during brute-force
|
||||
* input_string: the cipher-text that needs to be used during brute-force.
|
||||
|
||||
Optional:
|
||||
* alphabet: (None): the alphabet used to decode the cipher, if not
|
||||
specified, the standard english alphabet with upper and lowercase
|
||||
letters is used
|
||||
letters is used.
|
||||
|
||||
More about brute force
|
||||
More about brute force:
|
||||
======================
|
||||
Brute force is when a person intercepts a message or password, not knowing
|
||||
the key and tries every single combination. This is easy with the caesar
|
||||
cipher since there are only all the letters in the alphabet. The more
|
||||
complex the cipher, the larger amount of time it will take to do brute force
|
||||
complex the cipher, the larger amount of time it will take to do brute force.
|
||||
|
||||
Ex:
|
||||
Say we have a 5 letter alphabet (abcde), for simplicity and we intercepted the
|
||||
@ -179,11 +179,11 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
|
||||
ecd... and so on, until we reach a combination that makes sense:
|
||||
"cab"
|
||||
|
||||
Further reading
|
||||
Further reading:
|
||||
===============
|
||||
* https://en.wikipedia.org/wiki/Brute_force
|
||||
|
||||
Doctests
|
||||
Doctests:
|
||||
========
|
||||
>>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20]
|
||||
"Please don't brute force me!"
|
||||
|
Loading…
Reference in New Issue
Block a user