简体   繁体   English

通过密码和 openssl_decrypt 解密加密的私钥

[英]decrypt encrypted private key by password and openssl_decrypt

I have a private key encrypted script with a password and here is its output:我有一个带有密码的私钥加密脚本,这是它的输出:

{
    "iv":"Ra6kDXvh2DBiZ0r37pNuzg==",
    "v":1,
    "iter":10000,
    "ks":256,
    "ts":64,
    "mode":"ccm",
    "adata":"",
    "cipher":"aes",
    "salt":"pNN1xP7SZks=",
    "ct":"Sd8p3C3vPuW+LD
    nO9GwltDnqGOHg7+qguaEjQxzidEh5RNDh7bodJfmzmoB4DjFYQ4Qi8ferWoVV6bwJ2Q9/BnqI+
    X4A1MQY/HgVbtc9AnXj1EczsKxsUxG/ET7W+OBGQGLddzKVC38ACRg9q0NjOieOH0yTx64="
}

I dont know exactly whats name of this type of encryption and I want to know how to decrypt it using a password and PHP.我不知道这种加密的确切名称是什么,我想知道如何使用密码和 PHP 对其进行解密。

According to my research, it can be decrypted by the openssl_decrypt function.根据我的研究,它可以通过openssl_decrypt函数解密。 But I couldn't find how to use my parameters in this function.但是我找不到如何在这个函数中使用我的参数。

For example, I have a key called salt in the json that I have and I don't know what to do with it.例如,我在 json 中有一个名为salt的密钥,但我不知道如何处理它。

Also, in the openssl_decrypt function, there is an input argument called tag .此外,在openssl_decrypt函数中,有一个名为tag的输入参数。 I don't know the json key that it belongs to.我不知道它所属的 json 密钥。

This is a sample of the code I'm using:这是我正在使用的代码示例:

$ct = 'Sd8p3C3vPuW+LD
nO9GwltDnqGOHg7+qguaEjQxzidEh5RNDh7bodJfmzmoB4DjFYQ4Qi8ferWoVV6bwJ2Q9/BnqI+
X4A1MQY/HgVbtc9AnXj1EczsKxsUxG/ET7W+OBGQGLddzKVC38ACRg9q0NjOieOH0yTx64=';
$method = 'aes-256-ccm';
$password = 'Qw370207610';
$options = 0;
$iv = base64_decode('Ra6kDXvh2DBiZ0r37pNuzg==');

$output = openssl_decrypt($ct, $method, $password, $options, $iv);

And I received this error:我收到了这个错误:

openssl_decrypt(): Setting of IV length for AEAD mode failed openssl_decrypt(): AEAD 模式的 IV 长度设置失败

UPDATE:更新:

So I have gethered that for producing the third parameter (key) that is used in openssl_decrypt, I should act like this:所以我总结了为了生成在 openssl_decrypt 中使用的第三个参数(密钥),我应该这样做:

$ks = 256;
$key_length = $ks/8;
$password = 'Qw370207610';
$salt_base64 = 'pNN1xP7SZks=';
$salt = base64_decode($salt_base64);
$iterations = 10000;
$digest_algorithm = 'sha256';

$key = openssl_pbkdf2 ( $password , $salt , $key_length , $iterations , $digest_algorithm );

And then it can be decrypted in this way:然后就可以这样解密了:

$ct_base64 = 'Sd8p3C3vPuW+LD
nO9GwltDnqGOHg7+qguaEjQxzidEh5RNDh7bodJfmzmoB4DjFYQ4Qi8ferWoVV6bwJ2Q9/BnqI+
X4A1MQY/HgVbtc9AnXj1EczsKxsUxG/ET7W+OBGQGLddzKVC38ACRg9q0NjOieOH0yTx64=';
$ct = base64_decode($ct_base64);

$ts = 64;
$tag_length = $ts/8;
$tag = substr($ct,-$tag_length);
$ccm = substr($ct,0,-$tag_length);

$method = 'aes-256-ccm';

$options = OPENSSL_RAW_DATA;
$iv_base64 = 'Ra6kDXvh2DBiZ0r37pNuzg==';
$iv = base64_decode($iv_base64); // 16 bytes length

$output = openssl_decrypt($ccm, $method, $key, $options, $iv, $tag);

However in PHP, for decrypting aes-ccm, there is just openssl, and they haven't offered another library.但是在PHP中,为了解密aes-ccm,只有openssl,他们还没有提供另一个库。

On the other hand, these functions don't accept an IV (initialization vector) larger than 12 bytes.另一方面,这些函数不接受大于 12 字节的 IV(初始化向量)。 Because IV of my encrypted message is 16 bytes and it can not be decrypted in PHP at all !!因为我的加密消息的 IV 是 16 个字节,根本无法在 PHP 中解密!!

Have PHP developers not thought about this? PHP 开发人员没有考虑过这个问题吗?

I have never had such problems in nodejs, but I always face some kind of restriction in PHP.我在 nodejs 中从来没有遇到过这样的问题,但是我在 PHP 中总是面临某种限制。

No, you have a password encrypted script, where a secret key is generated from that password.不,你有一个密码加密脚本,其中密钥是从密码生成的。 And it was generated using SJCL or a compatible library, demonstration here .它是使用 SJCL 或兼容库生成的,演示在这里

The salt and iteration count iter are input to the PBKDF2 function, which generates the AES key. salt 和迭代计数iter被输入到 PBKDF2 函数,该函数生成 AES 密钥。

I hope you can progress using this information, because SO is not a code delivery service.我希望您可以使用此信息取得进展,因为 SO 不是代码交付服务。 Fortunately I know that OpenSSL contains PBKDF2, but I'm not sure if it is exposed to you.幸运的是,我知道 OpenSSL 包含 PBKDF2,但我不确定它是否向您公开。

please try this : From mcrypt_decrypt to openssl_decrypt请试试这个: 从 mcrypt_decrypt 到 openssl_decrypt

and for this you don't need the value of $iv if you give your data raw-value it will convert into base64 and other operation will be done by your method and algorithm.为此,如果您提供数据原始值,则不需要 $iv 的值,它将转换为 base64,其他操作将由您的方法和算法完成。 I think this might help you.我想这可能对你有帮助。

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

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