【发布时间】:2011-07-10 16:56:27
【问题描述】:
我有这段测试代码,它使用 Blowfish (openssl/blowfish.h) 来加密,然后解密一个字符串。但是当它再次出现时,它并没有被正确解密。谁能告诉我为什么?
(复制自 OP 在http://pastebin.com/AaWSF5pX 的原件)
#include <stdlib.h>
#include <cstdio>
#include <string.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
// blowfish key
const char *key = "h&6^5fVghasV_Fte";
BF_KEY bfKey;
BF_set_key(&bfKey, strlen(key), (const unsigned char*)key);
// encrypt
const unsigned char *inStr = (const unsigned char *)"hello world\0";
unsigned char *outStr = (unsigned char *)malloc(sizeof(unsigned char) * 100);
BF_ecb_encrypt(inStr, outStr, &bfKey, BF_ENCRYPT);
// decrypt
unsigned char buf[100];
BF_ecb_encrypt((const unsigned char*)outStr, buf, &bfKey, BF_DECRYPT);
std::cout << "decrypted: " << buf << "\n";
free(outStr);
return 0;
}
输入:“Hello World”
输出:“你好 wo4�\Z��”
【问题讨论】:
-
请在问题中包含代码,而不是将其放在外部网站上
标签: c++ encryption blowfish