简体   繁体   English

JS中的AES加密,PHP中的解密?

[英]AES encryption in JS, decrypt in PHP?

When my form gets submitted, it will first make a request to this controller action to get the server's public key: 提交表单后,它将首先向此控制器操作发出请求以获取服务器的公钥:

public function preprocessPayment(Request $request) {
    // Get public key
    $publicKey = $this->EncryptionService->getPublicKey();

    // Generate iv
    $method = 'aes-256-cbc';
    $ivlen = openssl_cipher_iv_length($method);
    $iv = openssl_random_pseudo_bytes($ivlen);

    return response()->json([
        'success' => true, 
        'data' => [
            'public_key' => $publicKey,
            'iv' => $iv
        ]
    ]);
}

After that, in my client, I'm going to generate a secret key using AES via CryptoJS, that will later be encrypted with the public_key . 之后,在我的客户端中,我将通过CryptoJS使用AES生成一个秘密密钥,稍后将使用public_key进行加密。

Then, the form data will be encrypted in AES using the AES secret key, and then the following payload will be submitted to the server: 然后,将使用AES密钥在AES中对表单数据进行加密,然后将以下有效负载提交给服务器:

{
    secret_key: xxx,
    iv: xxx,
    form_data: {...}
}

The AES encrypted data will be processed here: AES加密的数据将在此处处理:

public function storePayment(Request $request) {
    // Decrypt AES secret key (that was encrypted with the RSA public key),
        // using RSA private key
    // Decrypt AES client data with secret key
    // Store data in database
}

My question is, how will I do the AES secret key generation and encryption on the client side using CryptoJS? 我的问题是,如何使用CryptoJS在客户端进行AES密钥生成和加密? Could not seem to find any good documentation about it. 似乎找不到关于它的任何好的文档。 How should I format the data so it will be accepted by the server for decryption? 我应该如何格式化数据,以便服务器将其接受以进行解密?

And I'm stuck with decrypting AES in PHP, because it requires a $tag and I don't know where to get that when everything is coming from the client. 而且我一直坚持用PHP解密AES,因为它需要一个$tag ,而且当一切都来自客户端时,我也不知道该从哪里获得。

$originalData = openssl_decrypt($data, 'aes-128-gcm', $secretKey, $options=0, $iv, $tag);

I found this link: http://cryptojs.altervista.org/js-php/ , but I'm not sure how to make it work because I'm not sure where to locate the needed scripts. 我找到了此链接: http : //cryptojs.altervista.org/js-php/ ,但是我不确定如何使它正常工作,因为我不确定在哪里可以找到所需的脚本。

Edit: 编辑:

I made a mistake, for decrypting on the server, I was using aes-128-gcm instead of aes-256-cbc . 我在服务器上解密时犯了一个错误,我使用的是aes-128-gcm而不是aes-256-cbc When I corrected it, I was able to decrypt without the $tag . 更正后,无需使用$tag即可解密。

An AES-256 key is nothing more than 32 random bytes. AES-256密钥不过是32个随机字节。 So you create the key by using a cryptographically secure random number generator. 因此,您可以使用加密安全的随机数生成器来创建密钥。

However, both RSA PKCS#1 v1.5 and AES-CBC are vulnerable to padding oracle attacks. 但是,RSA PKCS#1 v1.5和AES-CBC都容易受到填充Oracle攻击。 So not only can an adversary change the message, the message is also not kept confidential. 因此,不仅对手可以更改消息,而且消息也不会保密。 In other words, you can use 256 bit keys as much as you want, but you should not create your own transport protocol, because the perceived security just isn't there. 换句话说,您可以根据需要使用256位密钥,但是您不应创建自己的传输协议,因为感知的安全性还不存在。

You could sign the ciphertext, but that has problems as well - generally we sign then encrypt. 您可以对密文进行签名,但这也有问题-通常我们先签名然后进行加密。

Use TLS. 使用TLS。

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

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