Blowfish crypto messes up first 8 bytes during encryption and decryption

心不动则不痛 提交于 2019-12-24 05:06:26

问题


I just tried out some en-/decryption with the openssl c librarys blowfish algorithm and ran into an odd error: the first 64 bits of the decrypted message are somehow messed up.

What am I doing wrong?

Here is the code:

#include <openssl/blowfish.h>
#include <cstring>
#include <cstdio>

int main()
{
    unsigned char rawKey[] = "password";

    BF_KEY key;
    BF_set_key(&key, strlen((char*) rawKey), rawKey);

    unsigned char msg[] = "Lorem ipsum dolor sit amet";

    unsigned char enc[64];
    memset(enc, 0, 64);
    unsigned char ivec[8];
    memset(ivec, 0, 8);

    BF_cbc_encrypt(msg, enc, strlen((char*) msg) + 1, &key, ivec,
        BF_ENCRYPT);

    unsigned char dec[64];

    BF_cbc_encrypt(enc, dec, strlen((char*) msg) + 1, &key, ivec,
        BF_DECRYPT);

    printf("%s\n", dec);

    return 0;
}

The output is:

.,�s�Ksum dolor sit amet

0x7fffffffde20: 46 '.'  3 '\003'    23 '\027'   44 ','  -102 '\232' 115 's' -2 '\376'   75 'K'
0x7fffffffde28: 115 's' 117 'u' 109 'm' 32 ' '  100 'd' 111 'o' 108 'l' 111 'o'
0x7fffffffde30: 114 'r' 32 ' '  115 's' 105 'i' 116 't' 32 ' '  97 'a'  109 'm'
0x7fffffffde38: 101 'e' 116 't' 0 '\000'

Thanks in advance :D !


回答1:


Try memset(ivec, 0, 8) before BF_cbc_encrypt. BF_cbc_encrypt maybe change it.



来源:https://stackoverflow.com/questions/21189487/blowfish-crypto-messes-up-first-8-bytes-during-encryption-and-decryption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!