Last active
November 16, 2023 17:12
-
-
Save ameyanekar/c9c7b44a9c77238b06c8ea875517debf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import sys | |
from Crypto.Cipher import AES | |
from Crypto.Util.Padding import pad | |
import base64 | |
if not len(sys.argv) > 1: | |
print("Pass a string to encrypt on the command line:") | |
print("Usage: python vfs-encrypt.py IdToEncrypt") | |
sys.exit() | |
data = sys.argv[1] | |
#Add the key and IV from the report here | |
def aes_encrypt(hex_key, hex_iv, data): | |
# Convert hex key and IV to bytes | |
key = bytes.fromhex(hex_key) | |
iv = bytes.fromhex(hex_iv) | |
# Ensure the key and IV are 16 bytes for AES-128 | |
if len(key) != 16 or len(iv) != 16: | |
raise ValueError("Key and IV must be 16 bytes (32 hex characters) long.") | |
# Padding the data | |
data = pad(data.encode(), AES.block_size) | |
# Initializing the cipher with the given IV | |
cipher = AES.new(key, AES.MODE_CBC, iv) | |
# Encrypting the data | |
ct_bytes = cipher.encrypt(data) | |
# Encoding the result to base64 for easy handling | |
return base64.b64encode(ct_bytes).decode('utf-8') | |
encrypted_data = aes_encrypt(hex_key, hex_iv, data) | |
print("Encrypted:", encrypted_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment