【问题标题】:decrypting files using openssl使用 openssl 解密文件
【发布时间】:2015-10-06 05:10:05
【问题描述】:

我正在尝试解密我最初使用 openssl 加密的文件:

#include <stdio.h>
#include <openssl/aes.h>
#include <string.h>
#include <stdlib.h>

const static unsigned char aes_key[]={"passwordpasswor"}; //15 characters + \0

int main(int argc, char *argv[]) {
if (argc>1) {
  if (strcmp("encrypt",argv[1])==0) {
    FILE *file;
    file=fopen("file.txt","w+b");
    unsigned char aes_input[]="#!/bin/bash\necho hello world\0";
    unsigned char iv[AES_BLOCK_SIZE];
    memset (iv,0x00,AES_BLOCK_SIZE);
    unsigned char enc_out[sizeof(aes_input)];
    unsigned char dec_out[sizeof(aes_input)];
    AES_KEY enc_key,dec_key;
    AES_set_encrypt_key(aes_key,sizeof(aes_key)*8,&enc_key);
    AES_cbc_encrypt(aes_input,enc_out,sizeof(aes_input),&enc_key,iv,AES_ENCRYPT);
    //decryption
    memset(iv,0x00,AES_BLOCK_SIZE);
    AES_set_decrypt_key(aes_key,sizeof(aes_key)*8,&dec_key);
    AES_cbc_encrypt(enc_out,dec_out,sizeof(aes_input),&dec_key,iv,AES_DECRYPT);
    //verify
    printf("original %s\n",aes_input);
    hex_print(enc_out, sizeof enc_out);
    printf("sizeof enc_out is %i\n",sizeof(enc_out));
    fwrite(enc_out,1,sizeof(enc_out),file);
    printf("decrypted %s\n",dec_out);
    }

  if (strcmp("decrypt",argv[1])==0) {
    printf("decrypt\n");
    FILE *file;
    char * ciphertext=0;
    int file_length;
    file=fopen("file.txt","r+b");
    if (file) {
      fseek (file,0,SEEK_END);
      file_length= ftell(file);
      fseek(file,0,SEEK_SET);
      ciphertext=malloc(file_length);
      if (ciphertext)
        fread(ciphertext,1,file_length,file);
      fclose(file);
      }
    //printf("ciphertext is %s\n",ciphertext);

    //begin decryption
    AES_KEY dec_key;
    unsigned char enc_out[sizeof(ciphertext)];
    unsigned char iv[AES_BLOCK_SIZE];
    memset (iv,0x00,AES_BLOCK_SIZE);
    AES_set_decrypt_key(aes_key,sizeof(aes_key)*8,&dec_key);
    AES_cbc_encrypt(ciphertext,enc_out,sizeof(ciphertext),&dec_key,iv,AES_DECRYPT);
    printf("original string is %s\n",enc_out);
    }

  }
    return 0;
}

我也在加密功能中测试解密(在写入或读取文件之前直接解密),它工作正常 然而,独立的解密功能没有正确解密:

decrypt
original string is #!/bin/b�:�j�

由于解密工作正常,我假设它正在写入文件或从文件读取到字符串,从而导致这种乱码输出。

我做错了什么?

谢谢。

【问题讨论】:

    标签: c linux openssl cryptography aes


    【解决方案1】:

    我有一些建议可能会有所帮助。最重要的是缓冲区管理。

    1. 在第一部分关闭您的加密文件。此外,您不需要 null 终止在引号中声明的字符串。例如,"hello world" 不是 "hello world\0" 这将等同于您的 cstring 中的 "hello world\0\0"(将被视为 "hello world\0")。这更像是一种约定俗成的事情。

    2. 使条件句 ifelse if 否则您总是执行这两项检查。

    3. 我的理解是 openssl 的加密/解密函数处理缓冲区,使用 sizeof(buffer) 是不可靠的。我相信这些函数会返回输出的长度,因此您应该将其保存在变量中并使用它。

    键带走,使用具有正确缓冲区大小的fwrite 函数,而不使用sizeof。由于加密会给你伪随机字符,因此你的缓冲区中可能有空终止符。

    这里是使用 AES256 CBC 的 openssl 手册的链接。我最近才用它来加密和解密文件,它对我有用。我会提供一个代码 sn-p 但它有点长。不幸的是,它使用的功能与您当前使用的功能不同。 https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption

    【讨论】:

    • 我很欣赏这 3 点,它们非常有用:) 你有什么建议来解决这个问题?应该修改读取或写入文件功能吗?我应该用十六进制写并在阅读后转换吗?
    • 检查这些函数的返回值,其他openssl加密和解密函数返回返回的密文/明文的长度。我会尝试找到我使用的函数的链接。
    • 如果您能够使用其他功能,请查看此 stackoverflow 问题。有一些示例和其他示例的链接。 stackoverflow.com/questions/9889492/…
    • 我仍然无法加密和解密文件:(
    • 你检查是否有aes加密和aes解密的返回值?
    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    相关资源
    最近更新 更多