Last active
December 1, 2018 14:18
-
-
Save ace0/ef8f509e57ae5963be62a5cc6236eb5b to your computer and use it in GitHub Desktop.
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
""" | |
Symmetric encryption tool | |
""" | |
from base64 import b64encode | |
import sys | |
def main(): | |
usage = "Usage: {sys.argv[0]} [test | encrypt | decrypt | crack]" | |
# If no argument, print the command usage | |
if len(sys.argv) != 2: | |
print(usage) | |
return | |
# Run the correct function based on the command | |
cmd = sys.argv[1] | |
if cmd == "test": | |
runTests() | |
elif cmd == "encrypt": | |
runEncrypt() | |
elif cmd == "decrypt": | |
runDecrypt() | |
elif cmd == "crack": | |
runCrack() | |
else: | |
# Unknown command, just print usage | |
print(usage) | |
def runTests(): | |
challenge1() | |
challenge2() | |
challenge3() | |
challenge4() | |
challenge5() | |
def runEncrypt(): | |
# TODO | |
pass | |
def runDecrypt(): | |
# TODO | |
pass | |
def runCrack(): | |
challenge8() | |
challenge9() | |
def encrypt(msg, key): | |
# TODO | |
pass | |
def decrypt(ctxt, key): | |
# TODO | |
pass | |
def expandKey(key, size): | |
# TODO | |
pass | |
def xor(bytes1, bytes2): | |
# TODO | |
pass | |
def challenge1(): | |
print("Challenge 1") | |
bytes1 = bytes.fromhex("7f3a4f1351f85b0344223e1177e14707190c0e311f4ca633f5f3e9352372") | |
bytes2 = bytes.fromhex("07e32c180dfa56496a461627542115132a4c284050495b23e2245b093159") | |
expectedResult = bytes.fromhex("78d9630b5c020d4a2e64283623c05214334026714f05fd1017d7b23c122b") | |
result = xor(bytes1,bytes2) | |
print("bytes1+bytes2=", result.hex()) | |
assert(result == expectedResult) | |
print("Passed!\n") | |
def challenge2(): | |
print("Challenge 2") | |
A = bytes.fromhex("e72d1d1f103807590f4339575e00381074485d2d580249f744052605e11d") | |
B = bytes.fromhex("07e32c180dfa56496a461627542115132a4c284050495b23e2245b093159") | |
C = xor(A,B) | |
# TODO: Write some print and/ assert statements | |
print("A+C=", None) | |
print("B =", None) | |
# assert(False) | |
print("C+B=", None) | |
print("A =", None) | |
# assert(False) | |
# print("Passed!\n") | |
def challenge3(): | |
print("Challenge 3") | |
# Here we use a encode() to turn a string (not hex string) into | |
# a sequence of bytes | |
key = "FETCH".encode() | |
longkey = expandKey(key, 18) | |
print(f"longkey={longkey}") | |
assert(longkey == "FETCHFETCHFETCHFET".encode()) | |
print("Passed!\n") | |
def challenge4(): | |
print("Challenge 4") | |
key = "GRETCHEN".encode() | |
message = "They say her hair is so big because it's full of secrets".encode() | |
ciphertext = encrypt(message, key) | |
expectedCiphertext = bytes.fromhex("133a202d633b2437673a20266320242735722c27633b2a6e253b2274212d262f322120742a3c623d673430382f682a2867212037312d313d") | |
print(f"ciphertext={ciphertext.hex()}") | |
assert(ciphertext == expectedCiphertext) | |
print("Passed!\n") | |
def challenge5(): | |
print("Challenge 5") | |
key = "GROOL".encode() | |
message = "She doesn't even go here.".encode() | |
ciphertext = encrypt(message, key) | |
print(f"ciphertext={ciphertext.hex()}") | |
recoveredMessage = decrypt(ciphertext, key) | |
print(f"recoveredMessage={recoveredMessage}") | |
assert(recoveredMessage == message) | |
print("Passed!\n") | |
def challenge8(): | |
print("Challenge 8") | |
ciphertext = bytes.fromhex("18763c713f3e25713071233436243d3023713c3e3c7d7118763c713071323e3e3d713c3e3c70") | |
# TODO | |
assert(False) | |
print("\n") | |
def challenge9(): | |
print("Challenge 9") | |
ciphertext = bytes.fromhex("022b611928212f2b3e2120376a3661392865362b2c37613e242b2a60") | |
# TODO | |
assert(False) | |
# Run! | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment