Spaces:
Sleeping
Sleeping
File size: 1,691 Bytes
8a87db2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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) |