2

所以这是我的C程序,我不知道为什么我不能解密加密的字符串。我想编写一个小 C 程序,它接受一个字符串,加密,然后接受加密的字符串,并使用提供的密钥解密。两个问题:A)我在这里做错了什么?B)密钥是如何存储的,也就是说,如果我现在正在进行新的加密并且用户想要解密以前的文本,当他提供密码时,河豚会知道如何解密吗?

这是河豚的文档: http ://www.openssl.org/docs/crypto/blowfish.html 现在是我的程序:

#include <string.h>
#include "blowfish.h"
#include "bf_pi.h"
#include "bf_locl.h"
#include <stdio.h>
#include <stdlib.h>

int main()
 {
    char from[128], to[128];
    int len = 128;
    BF_KEY key;
    char temp_buf[16];
    int n = 0;          /* internal blowfish variables */
    unsigned char iv[8];        /* Initialization Vector */
    /* fill the IV with zeros (or any other fixed data) */
    memset(iv, 0, 8);


    printf("input password: ");
    scanf("%s", &temp_buf);

    strcpy(from, "ABCDEFGHTHISISTHEDATA"); //ENCRYPT THIS

    BF_set_key(&key, 16, temp_buf);

    BF_cfb64_encrypt(from, to, len, &key, iv, &n, BF_ENCRYPT);
    printf("encrypted to -->  %s\n", to); //SUCCESSFULY ENCRYPTED


    BF_cfb64_encrypt(from, to, len, &key, iv, &n, BF_DECRYPT);
    printf("File %s has been decrypted to --> %s \n",from,  to); //FAILS DOES NOT DECRYPT
}
4

1 回答 1

1

我认为你应该切换fromto变量:

//as WhozCraig mentioned, fill in n and iv again before decryption
n = 0;          /* internal blowfish variables */
/* fill the IV with zeros (or any other fixed data) */
memset(iv, 0, 8);

BF_cfb64_encrypt(to, from, len, &key, iv, &n, BF_DECRYPT);
printf("File %s has been decrypted to --> %s \n",to,  from); //FAILS DOES NOT DECRYPT

保留原始 IV(这并不难;全为零)并在解密之前将 n 重置为零将得到 OP 想要的 - WhozCraig

于 2013-09-02T15:57:36.770 回答