涂鸦门锁 - AES-128 ECB PKCS7Padding - PHP

问题描述 投票:0回答:1

我试图使用tuya开发者将临时钥匙推入智能门,但我坚持使用PHP进行的加密。 涂鸦问这个:

Wi-Fi锁的原始密码长度为七位数字 Zigbee 锁和蓝牙锁的六位数字。密码是 使用 ECB 模式的 AES-128 算法进行加密, PKCS7 填充。要获得原始密钥,请解密临时密钥 Ticket_key 与 AES 使用由颁发的 accessKey 平台。输出格式为十六进制。

我正在尝试使用一些库,但他们要求 IV,我正在阅读一些内容,但无法弄清楚它是否必须是随机的,或者如果必须提供,我使用什么随机 IV 是否重要或者我必须使用服务器端可以理解的格式??

谢谢,我是密码学新手

php encryption tuya
1个回答
0
投票

尝试使用此代码进行加密/解密:

// AES encryption functions
function encrypt_password($plaintext, $key) {
    $cipher = "aes-128-ecb"; // AES with ECB mode
    $block_size = 16; // AES block size in bytes

    // Pad the plaintext to match the AES block size
    $padding = $block_size - (strlen($plaintext) % $block_size);
    $plaintext .= str_repeat(chr($padding), $padding);

    // Encrypt the padded plaintext
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA);

    // Convert ciphertext to hexadecimal and take only the first 16 bytes (32 characters)
    return substr(bin2hex($ciphertext), 0, 32);
}



function decrypt_password($ciphertext, $key) {
    $cipher = "aes-256-ecb"; // AES with ECB mode
    $padded_plaintext = openssl_decrypt(hex2bin($ciphertext), $cipher, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
    return rtrim($padded_plaintext, "\0");
}

function get_original_key($ticket_key, $access_secret) {
    $original_key = decrypt_password($ticket_key, $access_secret);
    return $original_key;
}

// Main function
function prepare_tuya_password($plaintext, $ticket_key, $access_secret) {
    
    // Get the original_key
    $original_key = get_original_key($ticket_key, $access_secret);
   // echo "original_key: " . $original_key . "\n";

    // Encrypted_password
    $encrypted_password = encrypt_password($plaintext, $original_key);
    //echo "encrypted_password: " . $encrypted_password . "\n";

    // End
    //echo "Have a nice day\n";
    return  $encrypted_password;
}
 
© www.soinside.com 2019 - 2024. All rights reserved.