How to encrypt and decrypt the data through this AES 256 algo in this (0xe4, 0x56, 0x45) form.. it is made from unicode\utf-8 by replacing \x with 0x

Viewed 150

this is AAES256 algo how to change its code to print the encryptoin in > {0xe8, 0x45} form I want result in this {0x38, 0x67} form


BLOCK_SIZE = 16
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
 
password = input("hihellolistenbbcnews")
 
 
def get_private_key(password):
    salt = b"this is a salt"
    kdf = PBKDF2(password, salt, 64, 1000)
    key = kdf[:32]
    return key
 
 
def encrypt(raw, password):
    private_key = get_private_key(password)
    raw = pad(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw))
 
 
def decrypt(enc, password):
    private_key = get_private_key(password)
    enc = base64.b64decode(enc)
    iv = enc[:16]
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return unpad(cipher.decrypt(enc[16:]))
 
encrypted = encrypt("This is a secret message", password)
open("favicon.ico", "wb").write(encrypted)
print(encrypted)
1 Answers

At the moment you are encoding your (binary) output from encrypt function to Base64 encoding:

return base64.b64encode(iv + cipher.encrypt(raw))

and vice versa for decryption.

If you need a hex encoding you can use the following two en-/decoding functions:

def bytesToHex(input):
  return input.hex()

def hexStringToByteArray(input):
  return bytes.fromhex(input)
Related