简体   繁体   English

我在 C 中的 openssl cmdline 相当于什么?

[英]What is the equivalent of my openssl cmdline in C?

I'm trying the implement the following openssl command line in C:我正在尝试在 C 中实现以下 openssl 命令行:

openssl enc -aes-256-cbc -d -in /tmp/out_enc -K \
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \
    -iv 0 -nopad -p

Output of the command line:命令行的输出:

key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
iv =00000000000000000000000000000000
<CONNECTION_REQUEST ATS_ID="2" ATP_SEQ_ID="1" REGISTRATION_ID="Y2G5R52S8PP6YX47" SERIAL_NUMBER="724574802" SPC_PRODUCT_TITLE="SPC5300" SPC_FW_VERSION="3.8.5 - R.31629" ATS_NAME="Syst&#232;me (ATS) 2" ATP1_ID="2" ATP1_UID="34" ATP1_NAME="Principal ATP 1" ATP1_COMMS_INTERFACE="1" ATP1_DEST="1, 192.168.1.62:52000" ATP1_CATEGORY="50"/>�z

This is my equivalent in C:这是我在 C 中的等价物:

long
_ast_crypt_decrypt_generic(unsigned char* ciphertext, long cipherlen, unsigned char* plaintext, const EVP_CIPHER *cipher) {
    long result = 0;

     /* A 256 bit key */
unsigned char *key = (unsigned char *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"0000000000000000";
    EVP_CIPHER_CTX *ctx = NULL;
    int len;
    long plaintext_len;

    if (cipher) {
        /* Create and initialise the context */
        if (!(ctx = EVP_CIPHER_CTX_new())) {
            g_warning("AstCrypt : EVP_CIPHER_CTX_new failed");
            goto end;
        }

        if (1 != EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv)) {
            g_warning("AstCrypt : EVP_DecryptInit_ex");
        }

        EVP_CIPHER_CTX_set_padding(ctx, 0);

        if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, cipherlen)) {
            g_warning("AstCrypt : EVP_DecryptUpdate");
            goto end;
        }
        plaintext_len = len;

        if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) {
             g_warning("AstCrypt : EVP_DecryptFinal_ex failed");
             goto end;
        }
        plaintext_len += len;

        result= plaintext_len;
    } 
    else {
        g_warning("AstCrypt : Failed to get the openssl elements.");
    }

end:
    if (ctx)
        EVP_CIPHER_CTX_free(ctx);

    return result;
}

long plainSize = _ast_crypt_decrypt_generic(headerData, headerLength, bPlain, EVP_aes_256_cbc());
 //fwrite(bPlain, plainSize, 1, stdout);

The previous code sample doesn't decrypt the data correctly (in C).前面的代码示例没有正确解密数据(在 C 中)。

Do you have any ideas what I am missing?你有什么想法我缺少什么吗?

It's probably a missing understanding of the key/iv format in the openssl lib.这可能是对 openssl 库中的 key/iv 格式缺乏理解。

The answer is that the key/iv must be represented in hex :答案是 key/iv 必须以十六进制表示:

The following works as expected :以下按预期工作:

unsigned char key[] =
        {   0xAA, 0xAA, 0xAA, 0xAA,
        0xAA, 0xAA, 0xAA, 0xAA,
        0xAA, 0xAA, 0xAA, 0xAA,
        0xAA, 0xAA, 0xAA, 0xAA,
        0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,0 };
//unsigned char key[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
    /* A 128 bit IV */
    unsigned char iv[] =
        {   0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,0 };

The problem is that you are assuming the key is direct copy of the "password".问题是您假设密钥是“密码”的直接副本。 What you are doing with that command line is specifying the key as a hex string.您在该命令行中所做的是将密钥指定为十六进制字符串。 So what you need to do is convert your "password" from a hex string into a the key buffer.因此,您需要做的是将您的“密码”从十六进制字符串转换为密钥缓冲区。 In your example you should end up with every byte set to the hex value "0xAA".在您的示例中,您应该将每个字节设置为十六进制值“0xAA”。

Because you are passing in the key directly, there is no need for the MD5 parameter (to your function or into openssl).因为您是直接传入密钥,所以不需要 MD5 参数(到您的函数或 openssl)。

If you wanted to using a password instead of a "hex string", then you need to hash it somehow into the key buffer.如果您想使用密码而不是“十六进制字符串”,那么您需要以某种方式将其散列到密钥缓冲区中。 This is where the MD5 parameter comes in. You can use something like PBKDF2 (although I would use the openssl default SHA256 and not MD5 as the hashing function).这就是 MD5 参数的用武之地。您可以使用 PBKDF2 之类的东西(尽管我会使用 openssl 默认的 SHA256 而不是 MD5 作为散列函数)。
eg openssl enc -aes-256-cbc -pbkdf2 -k password例如 openssl enc -aes-256-cbc -pbkdf2 -k 密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM