c - 使用 OpenSSL AES 命令行工具和 OpenSSL AES API 时出现不同的密文?

标签 c encryption openssl aes javax.crypto

为什么我使用 openssl aes 命令工具和 openssl AES api 时得到不同的密文?

我使用了三种加密方式:

  • 键入 a) openssl 命令行工具
  • 在 javax.cryto 中键入 b) 类
  • 键入 c) OpenSSL C api。

使用类型 (a) 和 (b),我得到了相同的密文。但是我在使用(c)时得到了不同的密文。

我想在使用方法c和方法a/b时得到相同的密文。 我觉得type c有问题,但是我找不到。请注意,我在上述三种方法中使用了相同的 KEY,IV 对。

输入一个:

openssl enc -aes-128-cbc -e -a -in pt.txt -out ct.txt -K 01010101010101010101010101010101 -iv 01010101010101010101010101010101 -p 

类型 b:
使用 javax.crypto 的 Java 代码。我不会粘贴代码,因为这样我得到了与类型 a 相同的密文。

类型 c: 使用 OpenSSL API 的 C 代码:

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

int main(int argc, char** argv) {

    AES_KEY aes;
    unsigned char key[AES_BLOCK_SIZE];        // AES_BLOCK_SIZE = 16
    unsigned char iv[AES_BLOCK_SIZE];        // init vector
    unsigned char* input_string;
    unsigned char* encrypt_string;
    unsigned char* decrypt_string;
    unsigned int len;        // encrypt length (in multiple of AES_BLOCK_SIZE)
    unsigned int i;

    // check usage
    if (argc != 2) {
        fprintf(stderr, "%s <plain text>\n", argv[0]);
        exit(-1);
    }

    // set the encryption length
    len = 0;
    if ( strlen(argv[1])>=AES_BLOCK_SIZE || 
         (strlen(argv[1]) + 1) % AES_BLOCK_SIZE == 0) {
        len = strlen(argv[1]) + 1;
    } else {
        len = ((strlen(argv[1]) + 1) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
    }

    // set the input string
    input_string = (unsigned char*)calloc(len, sizeof(unsigned char));
    if (input_string == NULL) {
        fprintf(stderr, "Unable to allocate memory for input_string\n");
        exit(-1);
    }
    strncpy((char*)input_string, argv[1], strlen(argv[1]));

    // Generate AES 128-bit key
    memset(key, 0x01, AES_BLOCK_SIZE);

    // Set encryption key
    memset(iv, 0x01, AES_BLOCK_SIZE);
    if (AES_set_encrypt_key(key, 128, &aes) < 0) {
        fprintf(stderr, "Unable to set encryption key in AES\n");
        exit(-1);
    }

    // alloc encrypt_string
    encrypt_string = (unsigned char*)calloc(len, sizeof(unsigned char));    
    if (encrypt_string == NULL) {
        fprintf(stderr, "Unable to allocate memory for encrypt_string\n");
        exit(-1);
    }

    // encrypt (iv will change)
    AES_cbc_encrypt(input_string, encrypt_string, len, &aes, iv, AES_ENCRYPT);

    /////////////////////////////////////

    // alloc decrypt_string
    decrypt_string = (unsigned char*)calloc(len, sizeof(unsigned char));
    if (decrypt_string == NULL) {
        fprintf(stderr, "Unable to allocate memory for decrypt_string\n");
        exit(-1);
    }

    // Set decryption key
    memset(iv, 0x01, AES_BLOCK_SIZE);
    if (AES_set_decrypt_key(key, 128, &aes) < 0) {
        fprintf(stderr, "Unable to set decryption key in AES\n");
        exit(-1);
    }

    // decrypt
    AES_cbc_encrypt(encrypt_string, decrypt_string, len, &aes, iv, 
            AES_DECRYPT);

    // print
    printf("input_string =%s\n", input_string);
    printf("encrypted string =");
    for (i=0; i<len; ++i) {
        printf("%u ", encrypt_string[i]);    
    }
    printf("\n");
    printf("decrypted string =%s\n", decrypt_string);

    return 0;
}

不同输出的原因可能是什么?

最佳答案

在您的 C 代码中,您实际上是在使用零填充:您分配一个由零填充的内存区域(通过 calloc ),然后将纯文本复制到该区域,留下末尾的零完好无损。

openssl enc 使用与您的 C 代码不同的填充。 documentation for openssl enc说(我强调):

All the block ciphers normally use PKCS#5 padding also known as standard block padding: this allows a rudimentary integrity or password check to be performed. However since the chance of random data passing the test is better than 1 in 256 it isn't a very good test.

此外,openssl enc 命令默认使用盐,它会随机化密文。盐的作用与每条消息的初始化 vector (IV) 类似。但是您使用的是显式 IV,因此盐不是随机化密文。

documentation for javax.crypto.Cipher (我想你用过)说:

A transformation is of the form:

  • "algorithm/mode/padding" or
  • "algorithm"

(in the latter case, provider-specific default values for the mode and padding scheme are used). For example, the following is a valid transformation:

Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");

因此,如果您只是使用 AESARS/CBC 而未指定填充模式,它会使用它认为合适的任何内容,在您的情况下恰好是相同的正如 OpenSSL 使用的那样(即 PKCS#5 填充)。

要改变你的 C 程序,你必须自己做同样的填充(本质上,它用字节数 x 填充 block ,所有这些都与这个数字具有相同的值,同时附加整个 block 当最后一个 block 已经满时用 16 填充) - 或者使用更高级别的 EVP 函数,它应该为您提供一种方法来指定密码的填充模式。

关于c - 使用 OpenSSL AES 命令行工具和 OpenSSL AES API 时出现不同的密文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9174250/

相关文章:

ruby - 为什么 Ruby 给出错误 "point is not on curve"?

c++ - _DEBUG 与 NDEBUG

c - 重新分配,字符**,段错误

ssl - 用于检查服务器是否提供证书的 OpenSSL 命令

.net - 保护部署在 "hostile"环境中的系统

c++ - 简化模幂 C++

ruby-on-rails - rails openssl 代码和终端加密的不同结果

c - 为什么总是显示错误:pointer being realloc'd was not allocated

c - 读取字符 0x1A 时发生文件结尾

android - 如何在c#中加密文件并在android和IOS上解密