My AES encryption/decryption functions don't work with random ivecs

Posted by Brendan Long on Stack Overflow See other posts from Stack Overflow or by Brendan Long
Published on 2010-03-29T23:38:37Z Indexed on 2010/03/29 23:43 UTC
Read the original article Hit count: 235

Filed under:
|
|

I was bored and wrote a wrapper around openSSL to do AES encryption with less work. If I do it like this: http://pastebin.com/V1eqz4jp (ivec = 0)
Everything works fine, but the default ivec is all 0's, which has some security problems. Since I'm passing the data back as a string anyway, I figured, why not generate a random ivec and stick it to the front, the take it back off when I decrypt the string? For some reason it doesn't work though.

With random ivec: http://pastebin.com/MkDBFcn6

Well actually, it almost works. It seems to decrypt the middle of the string, but not the beginning or end:

String is: 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
Encrypting..
???l%%1u???B!
?????`pN)?????[l????{?Q???2?/?H??y"?=Z?Cu????l%%1u???B!

Decrypting..
String is: ?%???G*?5J?0??0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF!-e????V?5

I honestly have no idea what's going wrong. Maybe some stupid mistake, or maybe I'm missing something about AES?

© Stack Overflow or respective owner

Related posts about openssl

Related posts about c++