Spaces:
Sleeping
Sleeping
| from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | |
| from cryptography.hazmat.backends import default_backend | |
| import base64 | |
| # Store a string into the variable which needs to be Encrypted | |
| simple_string = "CompanyName\n" | |
| # Display the original string | |
| print("Original String:", simple_string) | |
| # Store the cipher method | |
| ciphering = "AES-128-CTR" | |
| # Use OpenSSl Encryption method | |
| iv_length = algorithms.AES.block_size // 8 | |
| options = 0 | |
| # Non-NULL Initialization Vector for encryption | |
| encryption_iv = b'1234567891011121' | |
| # Store the encryption key | |
| encryption_key = b"QuantfromExpo" | |
| def encrypt(simple_string, key, iv): | |
| cipher = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend()) | |
| encryptor = cipher.encryptor() | |
| encrypted = encryptor.update(simple_string.encode('utf-8')) + encryptor.finalize() | |
| return base64.b64encode(encrypted).decode('utf-8') | |
| # Use encryption function to encrypt the data | |
| encryption = encrypt(simple_string, encryption_key, encryption_iv) | |
| # Display the encrypted string | |
| print("Encrypted String:", encryption) | |
| # Non-NULL Initialization Vector for decryption | |
| decryption_iv = b'1234567891011121' | |
| # Store the decryption key | |
| decryption_key = b"QuantfromExpo" | |
| def decrypt(encryption, key, iv): | |
| cipher = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend()) | |
| decryptor = cipher.decryptor() | |
| decrypted = decryptor.update(base64.b64decode(encryption)) + decryptor.finalize() | |
| return decrypted.decode('utf-8') | |
| # Use decryption function to decrypt the data | |
| decryption = decrypt(encryption, decryption_key, decryption_iv) | |
| # Display the decrypted string | |
| print("Decrypted String:", decryption) |