I am trying to attempt the following:
Encrypt a string using the AES algorithm with a 128 bit secret key. The cipher mode I've been told to use is CBC with PKCS5 padding. The initialization vector is 16 bytes of 00.
Here are the instructions I was given:
Using the secret key I need to generate a 128-bit secret key using the first 16 bytes.
Read the bytes of the string to be encoded in "UTF-8".
Encrypt the string using the AES algorithm and your 128 bit key. Be sure to use CBC mode and PKCS5 padding.
Convert the encrypted bytes to hex string.
Here is what I have so far:
message = "string to be encoded"
secret = "mysecretkey".encode('UTF-8')
key = secret[0..15]
iv = "/0" * 16
cipher = OpenSSL::Cipher.new('aes-128-cbc')
cipher.encrypt
cipher.key = aes_key = key
cipher.iv = aes_iv = iv
encrypted = cipher.update(message)
encrypted << cipher.final()
My output is gibberish. I'm assuming it's because I'm not properly converting to hex? Also how do I set PKCS5 padding? And am I doing the iv correctly? Or am I completely off track on this altogether?
Any help would be greatly appreciated!