【问题标题】:php implementation of openssl encopenssl enc的php实现
【发布时间】:2020-12-01 17:58:03
【问题描述】:

使用 guacamole-auth-json,编码 php 客户端实现。

我需要能够使用 aes-128-cbc 密码加密 hmac sha256 散列 json。我有相同的哈希输入数据,但未能实现加密。

bash 中的工作 guacamole-auth-json 示例

#!/bin/bash -e

##
## Encryption/signing key.
##
SECRET_KEY="$1"

##
## The filename of the JSON data being signed and encrypted.
##
JSON_FILENAME="$2"
##

## A null (all zeroes) IV.
##
NULL_IV="00000000000000000000000000000000"

##
## Signs the contents of the given file using the given key. The signature is
## created using HMAC/SHA-256, and is output in binary form to STDOUT, followed
## by the raw contents of the file.
##
## @param KEY
##     The key to use to sign the contents of the given file with HMAC/SHA-256.
##
## @param FILENAME
##     The filename of the file to sign.
##
sign() {

    KEY="$1"
    FILENAME="$2"

    #
    # Write out signature
    #

    openssl dgst                                \
        -sha256 -mac HMAC -macopt hexkey:"$KEY" \
        -binary "$FILENAME"

    #
    # Write out file contents
    #

    cat "$FILENAME"

}

##
## Encrypts all data received through STDIN using the provided key. Data is
## encrypted using 128-bit AES in CBC mode (with a null IV). The encrypted
## result is printed to STDOUT encoded with base64.
##
## @param KEY
##     The key to encrypt STDIN with, as a 16-byte (32-digit) hexadecimal
##     value.
##
encrypt() {

    KEY="$1"

    #
    # Encrypt STDIN
    #

    openssl enc -aes-128-cbc -K "$KEY" -iv "$NULL_IV" -nosalt -a

}

#
# Sign and encrypt file using secret key
#
sign "$SECRET_KEY" "$JSON_FILENAME" | encrypt "$SECRET_KEY"

我的其他数据生成 php 实现

$auth_json = "{\n";
$auth_json = $auth_json . "    \"username\" : \"\",\n";
$auth_json = $auth_json . "    \"expires\" : \"1607500000000\",\n";
$auth_json = $auth_json . "    \"connections\" : {\n";
$auth_json = $auth_json . "        \"Trading server 001\" : {\n";
$auth_json = $auth_json . "            \"protocol\" : \"" . get_post_meta( $post->ID,'trading_server_protocol', true ) . "\",\n";
$auth_json = $auth_json . "            \"parameters\" : {\n";
$auth_json = $auth_json . "                \"hostname\" : \"" . get_post_meta( $post->ID,'trading_server_hostname', true ) . "\",\n";
$auth_json = $auth_json . "                \"port\" : \"" . get_post_meta( $post->ID,'trading_server_port', true ) . "\",\n";
$auth_json = $auth_json . "                \"password\" : \"" . get_post_meta( $post->ID,'trading_server_password', true ) . "\",\n";
$auth_json = $auth_json . "                \"read-only\" : \"" . get_post_meta( $post->ID,'trading_server_readonly', true ) . "\"\n";
$auth_json = $auth_json . "            }\n";
$auth_json = $auth_json . "        }\n";
$auth_json = $auth_json . "    }\n";
$auth_json = $auth_json . "}";
            
$encrypt_key = "12345678901234567890123456789012";
$NULL_IV="00000000000000000000000000000000";
// HMAC Hex to byte
$secret     = hex2bin($encrypt_key);
$auth_json_hash = hash_hmac("sha256", $auth_json, $secret);
$auth_json_hash_bin = hash_hmac("sha256", $auth_json, $secret, true);
            
$cipher = "AES-128-CBC";
$data = $auth_json_hash_bin . $auth_json;
$auth_json_encrypted_raw = openssl_encrypt( $data, $cipher, $encrypt_key, $options = OPENSSL_RAW_DATA, $NULL_IV );
$auth_json_encrypted_base64 = base64_encode( $auth_json_encrypted_raw );

我有相同的哈希值,但无法调用 openssl_encrypt 函数,结果与使用 bash 示例产生的结果相同。

两者之间可能存在一些问题

openssl enc -aes-128-cbc -K "$KEY" -iv "$NULL_IV" -nosalt -a

openssl_encrypt($data, $cipher, $encrypt_key, $options = OPENSSL_RAW_DATA, $NULL_IV)

我明白,我需要在 php 中对结果进行 base64 编码(-a 开关),并且需要一些解决方案(-nosalt 开关)。

【问题讨论】:

  • 提示:不要手动构建 JSON。创建一个对象/数组或两者的混合,然后使用JSON_ENCODE() 来制作 JSON 字符串
  • 密钥和 IV 编码可能不同(bash 中的十六进制和 php 中的原始字节)
  • 谢谢,那是正确的,我自己也在同一时间找到了它。 :-)

标签: php encryption openssl guacamole


【解决方案1】:

问题在于 php 中的十六进制参数(encrypt_key,NULL_IV)。

正确的代码应该是

$encrypt_key = "12345678901234567890123456789012";
$NULL_IV="00000000000000000000000000000000";
..
$auth_json_encrypted_raw = openssl_encrypt( $data, $cipher, hex2bin($encrypt_key), $options = OPENSSL_RAW_DATA, hex2bin($NULL_IV) );

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2016-05-24
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多