-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Porta cipher #1550
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
Porta cipher #1550
Changes from 3 commits
b55cb5f
5de2ec0
fec2940
e5e5fba
9ff2f60
691b7e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# The objective of this GitHub Action is to add a new DIRECTORY.md file to a pull request if needed. | ||
name: directory_writer | ||
on: [pull_request] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
max-parallel: 1 | ||
matrix: | ||
python-version: [3.7] | ||
steps: | ||
|
||
- uses: actions/checkout@v1 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Update DIRECTORY.md | ||
run: | | ||
scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md | ||
git config --global user.name 'directory_writer' | ||
git config --global user.email '[email protected]' | ||
git remote set-url origin https://x-access-token:${{ secrets.gh_token }}@github.com/$GITHUB_REPOSITORY | ||
git checkout $GITHUB_HEAD_REF | ||
if git diff-files --quiet; then echo 0; else git commit -am "fixup: DIRECTORY.md" && git push; fi |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
alphabet = { | ||
mrvnmchm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"A": ("ABCDEFGHIJKLM", "NOPQRSTUVWXYZ"), | ||
"B": ("ABCDEFGHIJKLM", "NOPQRSTUVWXYZ"), | ||
"C": ("ABCDEFGHIJKLM", "ZNOPQRSTUVWXY"), | ||
"D": ("ABCDEFGHIJKLM", "ZNOPQRSTUVWXY"), | ||
"E": ("ABCDEFGHIJKLM", "YZNOPQRSTUVWX"), | ||
"F": ("ABCDEFGHIJKLM", "YZNOPQRSTUVWX"), | ||
"G": ("ABCDEFGHIJKLM", "XYZNOPQRSTUVW"), | ||
"H": ("ABCDEFGHIJKLM", "XYZNOPQRSTUVW"), | ||
"I": ("ABCDEFGHIJKLM", "WXYZNOPQRSTUV"), | ||
"J": ("ABCDEFGHIJKLM", "WXYZNOPQRSTUV"), | ||
"K": ("ABCDEFGHIJKLM", "VWXYZNOPQRSTU"), | ||
"L": ("ABCDEFGHIJKLM", "VWXYZNOPQRSTU"), | ||
"M": ("ABCDEFGHIJKLM", "UVWXYZNOPQRST"), | ||
"N": ("ABCDEFGHIJKLM", "UVWXYZNOPQRST"), | ||
"O": ("ABCDEFGHIJKLM", "TUVWXYZNOPQRS"), | ||
"P": ("ABCDEFGHIJKLM", "TUVWXYZNOPQRS"), | ||
"Q": ("ABCDEFGHIJKLM", "STUVWXYZNOPQR"), | ||
"R": ("ABCDEFGHIJKLM", "STUVWXYZNOPQR"), | ||
"S": ("ABCDEFGHIJKLM", "RSTUVWXYZNOPQ"), | ||
"T": ("ABCDEFGHIJKLM", "RSTUVWXYZNOPQ"), | ||
"U": ("ABCDEFGHIJKLM", "QRSTUVWXYZNOP"), | ||
"V": ("ABCDEFGHIJKLM", "QRSTUVWXYZNOP"), | ||
"W": ("ABCDEFGHIJKLM", "PQRSTUVWXYZNO"), | ||
"X": ("ABCDEFGHIJKLM", "PQRSTUVWXYZNO"), | ||
"Y": ("ABCDEFGHIJKLM", "OPQRSTUVWXYZN"), | ||
"Z": ("ABCDEFGHIJKLM", "OPQRSTUVWXYZN"), | ||
} | ||
|
||
|
||
def generate_table(key): | ||
""" | ||
>>> generate_table('marvin') | ||
[('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'), ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'), ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These long lines need to be wrapped. |
||
""" | ||
return [alphabet[char] for char in key.upper()] | ||
|
||
|
||
def encrypt(key, words): | ||
""" | ||
>>> encrypt('marvin', 'jessica') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This indenting is not needed or desirable. |
||
'QRACRWU' | ||
""" | ||
cipher = "" | ||
count = 0 | ||
table = generate_table(key) | ||
for char in words.upper(): | ||
cipher += get_opponent(table[count], char) | ||
count = (count + 1) % len(table) | ||
return cipher | ||
|
||
|
||
def decrypt(key, words): | ||
""" | ||
>>> decrypt('marvin', 'QRACRWU') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
'JESSICA' | ||
""" | ||
return encrypt(key, words) | ||
|
||
|
||
def get_position(table, char): | ||
""" | ||
>>> table = [('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'), ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'), ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
>>> get_position(table, 'A') | ||
(None, None) | ||
""" | ||
row = -1 | ||
|
||
if char in table[0]: | ||
row = 0 | ||
elif char in table[1]: | ||
row = 1 | ||
|
||
return (None, None) if row == -1 else (row, table[row].index(char)) | ||
|
||
|
||
def get_opponent(table, char): | ||
""" | ||
>>> table = [('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'), ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'), ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
>>> get_opponent(table, 'A') | ||
'A' | ||
""" | ||
row, col = get_position(table, char.upper()) | ||
|
||
if row == 1: | ||
return table[0][col] | ||
else: | ||
return table[1][col] if row == 0 else char | ||
|
||
|
||
if __name__ == "__main__": | ||
""" | ||
ENTER KEY: marvin | ||
ENTER TEXT TO ENCRYPT: jessica | ||
ENCRYPTED: QRACRWU | ||
DECRYPTED WITH KEY: JESSICA | ||
""" | ||
key = input("ENTER KEY: ") | ||
text = input("ENTER TEXT TO ENCRYPT: ") | ||
cipher_text = encrypt(key, text) | ||
|
||
print("ENCRYPTED: " + cipher_text) | ||
print("DECRYPTED WITH KEY: " + decrypt(key, cipher_text)) | ||
|
||
import doctest | ||
|
||
doctest.testmod() |
Uh oh!
There was an error while loading. Please reload this page.