Blowfish加密在加密和解密期间混淆了前8个字节 [英] Blowfish crypto messes up first 8 bytes during encryption and decryption
本文介绍了Blowfish加密在加密和解密期间混淆了前8个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚尝试了一些en /解密与openssl c库blowfish算法,并遇到一个奇怪的错误:解密的消息的前64位被以某种方式搞乱。
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.
我做错了什么?
以下是代码:
#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;
}
输出为:
.,�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'
!
推荐答案
在BF_cbc_encrypt之前尝试memset(ivec,0,8)。
BF_cbc_encrypt可能会改变它。
Try memset(ivec, 0, 8) before BF_cbc_encrypt. BF_cbc_encrypt maybe change it.
这篇关于Blowfish加密在加密和解密期间混淆了前8个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文