可以';t get PHP';s openssl_encrypt以匹配C中aes_encrypt的输出


Can't get PHP's openssl_encrypt to match output from aes_encrypt in C

我不是程序员,所以请耐心等待…

不幸的是,我使用的一个应用程序将密码以明文形式存储在MySQL中,这是我不想要的。由于该程序使用了OpenSSL库,我可以访问aes函数。

下面我拼凑了一个演示代码,它使用这些函数来加密测试字符串,并使用MD5对其进行散列(因为加密的文本是二进制的(:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/md5.h>
char *str2md5(const char *str, int length) {
    int n;
    MD5_CTX c;
    unsigned char digest[16];
    char *out = (char*)malloc(33);
    MD5_Init(&c);
    while (length > 0) {
        if (length > 512) {
            MD5_Update(&c, str, 512);
        } else {
            MD5_Update(&c, str, length);
        }
        length -= 512;
        str += 512;
    }
    MD5_Final(digest, &c);
    for (n = 0; n < 16; ++n) {
        snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
    }
    return out;
}
int main(int argc, char* argv[]) {
        AES_KEY aesKey_;
        unsigned char userKey_[16];
        unsigned char in_[16];
        unsigned char out_[16];
        strcpy(userKey_,"1234567890abcdef");
        strcpy(in_,"texttoencrypt");
        fprintf(stdout,"Original message: %s'n", in_);
        AES_set_encrypt_key(userKey_, 128, &aesKey_);
        AES_encrypt(in_, out_, &aesKey_);
        char *output = str2md5(out_, strlen(out_));
        fprintf(stdout,"MD5 of Encrypted message: %s'n", output);
        AES_set_decrypt_key(userKey_, 128, &aesKey_);
        AES_decrypt(out_, in_,&aesKey_);
        fprintf(stdout,"Recovered Original message: %s'n", in_);
        return 0;
}

该输出:

Original message: texttoencrypt
MD5 of Encrypted message: 3675b450ae0415e5a8521b9bb7ee01ba
Recovered Original message: texttoencrypt

现在在PHP中,我使用此代码生成各种AES-128加密字符串,类似地,MD5处理结果:

<?php
$methods = openssl_get_cipher_methods();
$plain = "texttoencrypt";
$password = "1234567890abcdef";
foreach ($methods as $method) {
        if (preg_match('/AES-128/', $method)) {
                $encrypted = openssl_encrypt($plain, $method, $password);
                $decrypted = openssl_decrypt($encrypted, $method, $password);
                echo $method . ' : ' . md5($encrypted) . ' ; ' . $decrypted . "'r'n";
        }
}
?>

输出:

AES-128-CBC : 08d6f8e2ae21a7a506fabf91adcc3b63 ; texttoencrypt
AES-128-CFB : ce10ea28d7607bd6514e478e025e47c6 ; texttoencrypt
AES-128-CFB1 : 6adde484b8bee26f9b1ca7856634586d ; texttoencrypt
AES-128-CFB8 : aea100f1473c0a3d6380dd0f28585e19 ; texttoencrypt
AES-128-ECB : 08d6f8e2ae21a7a506fabf91adcc3b63 ; texttoencrypt
AES-128-OFB : ce10ea28d7607bd6514e478e025e47c6 ; texttoencrypt

不幸的是,我没有得到与C代码生成的3675b450ae0415a8521b9bb7ee01ba相匹配的结果。我已经尝试了在PHP手册页和SE上看到的几乎每一条评论,但都无法匹配。

我不能修改C代码,只能修改PHP。。。因此,任何关于如何让PHP与C输出相匹配的指针都是值得赞赏的!

 AES_encrypt(in_, out_, &aesKey_);
 char *output = str2md5(out_, strlen(out_));

谁负责空终止out,使strlen按预期工作?当然不是AES_encrypt

此外,在strcpy(userKey_,"1234567890abcdef");中,您正在将17字节的数据(您必须计算空终止符(复制到char的数组16中。