php - 适用于 PHP 的 AWS 开发工具包 - 解密密码

标签 php encryption amazon-web-services amazon-ec2 aws-sdk

对于我正在处理的项目,我使用适用于 PHP 的 Amazon AWS SDK,并且需要以纯文本格式检索服务器环境的密码。然而,documentation ec2 方法证实了我们的发现:该方法只会返回加密的字符串。从表面上看,这很好,因为适用于 PHP 的 AWS 开发工具包使用未加密的 HTTP POST 请求通过 cURL 发送和接收数据,这对于用户来说是不可见的。因此,我们的密码数据不会在网络上到处传播。

问题在于没有任何内容解释如何解密该字符串。我的私钥是 PEM 文件,但没有方法或文档说明如何处理该字符串以使其可用。几次尝试都没有取得任何成果,我开始认为我需要重新考虑我正在进行的项目的策略,但后来我找到了最新版本的适用于 PHP 的 AWS 开发工具包中的代码,它揭示了如何进行解密字符串以生成纯文本形式的密码。

最佳答案

我发现的答案是 getPasswordData 方法返回一个经过 Base64 编码和加密的字符串。您需要先使用 base64_decode() 对其进行解码,然后才能使用 PHP 的 OpenSSL 库成功解密。以下函数负责这两件事:

/**
 * @param obj $ec2_client The EC2 PHP client, from the AWS SDK for PHP
 * @param string $client_id The ID of the client whose password we're trying to get.
 * @return mixed The unencrypted password for the client, or false on failure.
 */
function aws_get_ec2_password($ec2_client, $client_id){
    //  First, run getPasswordData to get the Password Data Object.
    $pw_obj = $ec2_client->getPasswordData($client_id);

    //  Next, use the local get() method to isolate the password
    $pw_b64 = $pw_obj->get("PasswordData");

    //  Decode the password string.
    $pw_encrypted = base64_decode($pw_b64);

    //  Now, get your PEM key.
    //
    //  You can also use a raw string of the PEM key instead of get_file_contents(),
    //  or adjust the function so that you can pass it as an argument.
    //
    //  Technically, this step might not be necessary, as the documentation for
    //  openssl_private_decrypt() suggests that $key can just be the path, and it will
    //  create the key object internally.
    $key = openssl_get_privatekey(file_get_contents("path/to/key.pem"));

    //  Create an empty string to hold the password.
    $pw = "";

    //  Finally, decrypt the string and return (will return false if decryption fails).
    if(openssl_private_decrypt($pw_encrypted, $pw, $key)){
        return $pw;
    }else{
        return false;
    }
}

我希望这可以帮助其他人避免给我带来的头痛!

关于php - 适用于 PHP 的 AWS 开发工具包 - 解密密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261784/

相关文章:

Php Cart 没有转移到 Paypal

php - 与货币和国家相关的产品的数据库结构

php - 在包含单引号和双引号的字符串中转义单引号

php - 将 crypt() salt 存储在数据库中以进行密码比较是否安全

c# - Adyen,无法获得正确的 SHA-256 加密

Python导入boto3错误: cannot import name ClientError

php - 如何从mysql中的两个表中选择两列

hadoop - HDFS 加密 |远程异常

database - 审计数据的云存储有哪些好的选择? (连续写,很少查询)

amazon-web-services - 新手安装问题: Oracle JDK7 on AWS/Tomcat