我有一段使用Blowfish(openssl/blowfish.h)加密的測試代碼,然後解密一個字符串。但是當它再次出現時,它還沒有被正確解密。誰能告訴我爲什麼請?Blowfish C++沒有正確加密/解密..爲什麼..?
(在http://pastebin.com/AaWSF5pX從OP的原始拷貝)
#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」
請包括問題中的代碼,而不是把它放在外部網站 – Erik 2011-03-07 22:31:08