Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6005-CEM-Codebase/ChatClient/buggs/aesDemo.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54 lines (32 sloc)
1.05 KB
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
from Crypto.Cipher import AES | |
#Our Static Key | |
KEY = b"ThisIsAStaticKey" | |
IV = b"ThisIsAnStaticIV" | |
def encryptMessage(message, IV=None): | |
"""Encrypt a message using a static key""" | |
#Create a AES object We use CFB as we dont need to Pad the object input | |
cipher = AES.new(KEY, AES.MODE_CFB, iv=IV) | |
#Encode the Data | |
enciphered = cipher.encrypt(message) | |
#The IV is automagically Generated for us | |
IV = cipher.iv | |
#REturn them | |
return enciphered, IV | |
def decryptMessage(message, IV): | |
""" | |
Given a message and a IV | |
Decrypt the data using a static key | |
""" | |
cipher = AES.new(KEY, AES.MODE_CFB, iv=iv) | |
#Deode the Data | |
deciphered = cipher.decrypt(message) | |
return deciphered | |
if __name__ == "__main__": | |
message = b"Hello World" | |
#Use a Static IV | |
code, iv = encryptMessage(message, IV) | |
print("Cipher Text is {0}".format(code)) | |
print("IV is {0}".format(iv)) | |
#Then Decipher | |
decode = decryptMessage(code, iv) | |
print("Decoded Message {0}".format(decode)) | |