lab_encryption/ccipher.py

64 lines
1.5 KiB
Python

test_str = "abc"
bytes_lst = []
for item in test_str:
bytes_lst.append(bytes(item,'utf-8'))
print("As bytes, the string is:")
print(bytes_lst)
encoded_lst = []
for item in test_str:
encoded_lst.append(item.encode())
print("Encoding the characters in the string also gives:")
print(encoded_lst)
hex_lst=[]
for item in encoded_lst:
hex_lst.append(list(item)[0])
print("The hex values associated with those bytes is:")
print(hex_lst)
# Checkpoint 1
def numerify(message):
hex_values=[]
for item in message:
hex_values.append(item.encode()[0])
return hex_values
print(numerify('abc'))
# Checkpoint 2
secret_number = 1
def encrypt(numeric_message):
to_encrypt = []
for item in numeric_message:
to_encrypt.append((item + secret_number) % 256)
return to_encrypt
print(encrypt(numerify('abc')))
# Checkpoint 3
def decrypt(numeric_message):
to_decrypt = []
for item in numeric_message:
to_decrypt.append((item + (256 - secret_number)) % 256)
return to_decrypt
print(decrypt(encrypt(numerify('abc'))))
# Checkpoint 4
def wordify(decrypted_message):
word = []
for items in decrypted_message:
word.append(chr(items))
return ''.join(word)
print(wordify(decrypt(encrypt(numerify('abc')))))
secret_number = 78
message = open('xfile.txt','r').read().replace('[','').replace(']','').split(',')
# Is this how I was supposed to get the list from the file?
message_copy = []
for item in message:
message_copy.append(int(item))
message_copy=wordify(decrypt(message_copy))
print(message_copy)