└── Шифрование и дешифрование текста /Шифрование и дешифрование текста: -------------------------------------------------------------------------------- 1 | from Crypto.Cipher import AES 2 | from Crypto import Random 3 | from binascii import b2a_hex 4 | import sys 5 | 6 | # get the plaintext 7 | plain_text = sys.argv[1] 8 | 9 | # The key length must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes. 10 | key = b'this is a 16 key' 11 | 12 | # Generate a non-repeatable key vector with a length 13 | # equal to the size of the AES block 14 | iv = Random.new().read(AES.block_size) 15 | 16 | # Use key and iv to initialize AES object, use MODE_CFB mode 17 | mycipher = AES.new(key, AES.MODE_CFB, iv) 18 | 19 | # Add iv (key vector) to the beginning of the encrypted ciphertext 20 | # and transmit it together 21 | ciphertext = iv + mycipher.encrypt(plain_text.encode()) 22 | 23 | 24 | # To decrypt, use key and iv to generate a new AES object 25 | mydecrypt = AES.new(key, AES.MODE_CFB, ciphertext[:16]) 26 | 27 | # Use the newly generated AES object to decrypt the encrypted ciphertext 28 | decrypttext = mydecrypt.decrypt(ciphertext[16:]) 29 | 30 | # output 31 | file_out = open("encrypted.bin", "wb") 32 | file_out.write(ciphertext[16:]) 33 | file_out.close() 34 | 35 | print("The key k is: ", key) 36 | print("iv is: ", b2a_hex(ciphertext)[:16]) 37 | print("The encrypted data is: ", b2a_hex(ciphertext)[16:]) 38 | print("The decrypted data is: ", decrypttext.decode()) 39 | 40 | 41 | #the end 42 | --------------------------------------------------------------------------------