-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
create beaufort cipher #3206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
create beaufort cipher #3206
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e5519b9
create beaufort cipher
radadiyamohit81 153b45a
update the file
radadiyamohit81 53d1d2c
Update beaufort_cipher.py
radadiyamohit81 c8b66ed
Update beaufort_cipher.py
radadiyamohit81 696707b
update as per black formatter
radadiyamohit81 626fa29
Update beaufort_cipher.py
radadiyamohit81 5e9a03d
update the file
radadiyamohit81 ece0288
update file
radadiyamohit81 2fe5103
update file
radadiyamohit81 e13c006
update file
radadiyamohit81 f23e0e2
update file
radadiyamohit81 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
Author: Mohit Radadiya | ||
""" | ||
|
||
from string import ascii_uppercase | ||
|
||
dict1 = {char: i for i, char in enumerate(ascii_uppercase)} | ||
dict2 = {i: char for i, char in enumerate(ascii_uppercase)} | ||
|
||
|
||
# This function generates the key in | ||
# a cyclic manner until it's length isn't | ||
# equal to the length of original text | ||
def generate_key(message: str, key: str) -> str: | ||
""" | ||
>>> generate_key("THE GERMAN ATTACK","SECRET") | ||
'SECRETSECRETSECRE' | ||
""" | ||
x = len(message) | ||
i = 0 | ||
while True: | ||
if x == i: | ||
i = 0 | ||
if len(key) == len(message): | ||
break | ||
key += key[i] | ||
i += 1 | ||
return key | ||
|
||
|
||
# This function returns the encrypted text | ||
# generated with the help of the key | ||
def cipher_text(message: str, key_new: str) -> str: | ||
""" | ||
>>> cipher_text("THE GERMAN ATTACK","SECRETSECRETSECRE") | ||
'BDC PAYUWL JPAIYI' | ||
""" | ||
cipher_text = "" | ||
i = 0 | ||
for letter in message: | ||
if letter == " ": | ||
cipher_text += " " | ||
else: | ||
x = (dict1[letter] - dict1[key_new[i]]) % 26 | ||
i += 1 | ||
cipher_text += dict2[x] | ||
return cipher_text | ||
|
||
|
||
# This function decrypts the encrypted text | ||
# and returns the original text | ||
def original_text(cipher_text: str, key_new: str) -> str: | ||
""" | ||
>>> originalText("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") | ||
'THE GERMAN ATTACK' | ||
""" | ||
or_txt = "" | ||
i = 0 | ||
for letter in cipher_text: | ||
if letter == " ": | ||
or_txt += " " | ||
else: | ||
x = (dict1[letter] + dict1[key_new[i]] + 26) % 26 | ||
i += 1 | ||
or_txt += dict2[x] | ||
return or_txt | ||
|
||
|
||
def main(): | ||
message = "THE GERMAN ATTACK" | ||
key = "SECRET" | ||
key_new = generate_key(message, key) | ||
s = cipher_text(message, key_new) | ||
print(f"Encrypted Text = {s}") | ||
print(f"Original Text = {original_text(s, key_new)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.