Last active
August 6, 2021 20:38
-
-
Save gombossb/f9c0a8963a28acf191e822b5ddb01656 to your computer and use it in GitHub Desktop.
BME VIK GT Gettysburg
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
mode = 'decode' | |
cipher = 'LYXCMLICLRFWJHZKEVVCDRXHU' | |
plaintext = 'HELLOWORLD' | |
key = 'JULYTHREE' | |
#key = 'AUGUSTFIVE' | |
# az A-Z karakterkódokat 0-25 között tároljuk, -65 offsettel az ASCII-tól | |
# az abc túlcsordítását modulo 26-al oldjuk meg | |
# karakter számmá alakítása | |
def char2num(char): | |
char = char.upper() | |
return ord(char) - 65 | |
# szám karakterré alakítása | |
def num2char(num): | |
return chr(num + 65) | |
# számok összeadása | |
def addNums(num1, num2): | |
return (num1 + num2) % 26 | |
# számok kivonása | |
def subNums(num1, num2): | |
out = num1 - num2 | |
# underflow kezelés | |
if out < 0: | |
out = 26 - abs(out) | |
return out | |
# dekódolás | |
def decode(cipher, key): | |
decoded = '' | |
for x in range(0, len(cipher)): | |
decoded += num2char(subNums(char2num(cipher[x]), char2num(key[x]))) | |
return decoded | |
# kódolás | |
def encode(plaintext, key): | |
encoded = '' | |
for x in range(0, len(plaintext)): | |
encoded += num2char(addNums(char2num(plaintext[x]), char2num(key[x]))) | |
return encoded | |
def keyFix(key, textlen): | |
# kódszó többszörözése ha nem elég hosszú | |
if len(key) < textlen: | |
key = key*(textlen//len(key)+1) | |
# felesleg levágása a kódszóról | |
key = key[0:textlen] | |
return key | |
# mód választása | |
if mode == 'decode': | |
print(f'cipher: {cipher}') | |
print(f'key: {key}') | |
print(f'output: {decode(cipher, keyFix(key, len(cipher)))}') | |
elif mode == 'encode': | |
print(f'plain: {plaintext}') | |
print(f'key: {key}') | |
print(f'output: {encode(plaintext, keyFix(key, len(plaintext)))}') | |
else: | |
print('kecske') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment