0
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes
import hashlib
import base64

def decrypt(enc, key_hash):                # To decrypt data
    print("\nIn Decryption method\n")
    unpad = lambda s: s[:-ord(s[-1:])]
    enc = base64.b64decode(enc)

    iv = enc[:AES.block_size]
    cipher = AES.new(key_hash, AES.MODE_CFB, iv)
    ciper_text =  cipher.decrypt(enc[AES.block_size:])
    ciper_text = ciper_text.decode('utf-16')
    ciper_text = unpad(ciper_text)
    return ciper_text

def encrypt(ID, temperature, key_hash):     # To encrypt data
    print("\nIn Encryption method\n")
    BS = AES.block_size
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)

    ID = pad(ID)
    ID = ID.encode('utf-16') 

    temperature = pad(temperature)
    temperature = temperature.encode('utf-16')
    iv = get_random_bytes(AES.block_size)
    cipher = AES.new(key= key_hash, mode= AES.MODE_CFB, iv= iv)

    ID_cipher = base64.b64encode(iv + cipher.encrypt(ID))
    temperature_cipher = base64.b64encode(iv + cipher.encrypt(temperature))
    print("Id cipher is '{0}'".format(ID_cipher))
    print("temp cipher is '{0}'".format(temperature_cipher))
    return (ID_cipher, temperature_cipher)

no = int(input("enter no of records"))
key_hash = hashlib.sha256(b"charaka").digest()       # Creating key for cipher


for i in range(no):                                    
   ID = input("enter ID\n")
   temperature = input("enter temperature\n")       
   (ID_cipher, temperature_cipher) = encrypt(ID, temperature, key_hash)
   print("Decyrpted ID is '{0}'".format((decrypt(ID_cipher, key_hash))))
   print("Decyrpted temp is '{0}'".format((decrypt(temperature_cipher, key_hash))))

When I want to enter a record i.e "ID, temperature" and trying to decrypt both ID is decrypting fine but temperature is not decrypting. Sometimes it produces utf-16 error i.e

ciper_text = ciper_text.decode('utf-16') UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 4-5: illegal UTF-16 surrogate

sometimes, output is not displayed properly

In Decryption method Decyrpted temp is '勼⼋'

My doubt is when ID is decrypting well why am I getting problem in decrypting temperature value. I have explored other tutorials about encoding techniques but the problem is same.

I used pycryptodome library to encrypt strings using AES. Thank you

mani varma
  • 31
  • 1
  • 4
  • A key should not be hashed. CFB doesn't require padding. Encrypting twice with the same IV is dangerous. Your encryption and decryption methods are not balanced (one does more than the other). Please fix those issues before continuing to debug. Oh, and you talk about both UTF-16 (code) and UTF-8 (title); I'd just stick with UTF-8. – Maarten Bodewes May 30 '20 at 10:03
  • Sir, key should be hashed. I am not encrypting same thing twice. If Encryption and Decryption are not balanced, how do you think ID value is decrypting successfully. Both utf-8 and utf-16 issue errors while decrypting temperature . Please run the code once . Thank you, Sir. – mani varma May 30 '20 at 10:57
  • The cipher object is _stateful_. In `encrypt` the _same_ `cipher` object is applied for the encryption of `ID` and `temperature`, in `decrypt` _different_ `cipher` objects (since only one ciphertext is decrypted per call). This has to be made consistent, e.g. by using a _new_ `cipher` object concerning `temperature` in `encrypt`. – Topaco May 30 '20 at 15:23
  • Thank you So much @Topaco . It's working – mani varma May 31 '20 at 13:36

0 Answers0