简体   繁体   English

我们可以使用 angular 中的私钥加密数据并使用 RSA 加密使用 PHP 中的公钥解密数据吗?

[英]Can we Encrypt data with private key in angular and Decrypt data with public key in PHP using RSA Encryption?

i'm trying to Encrypt data in using RSA Public Key in angular and Decrypt data with public key In php.我正在尝试使用 angular 中的 RSA 公钥加密数据,并使用 php 中的公钥解密数据。 i'm trying to Encrypt with JsEncrypt library in angular and Decrypt in Php Using我正在尝试使用 angular 中的 JsEncrypt 库加密并使用 Php 中的解密

:openssl_public_decrypt($signature, $decrypted, $publicKey, OPENSSL_PKCS1_PADDING);

I need to resolve encryption and Decryption between Angular and Php.我需要解决 Angular 和 Php 之间的加密和解密。

Please see:请参见:

ANGULAR CODE ANGULAR 代码

enc(data: any){  this.$encrypt.setPrivateKey(`dummyprivakey`);
        let ndata = this.$encrypt.encrypt(data);
    console.log("encrypt data ", ndata)
       return ndata
}

PHP CODE: PHP 代码:

    openssl_public_decrypt ($encrypted, $decrypted , $publickey); 
       var_dump($decrypted);

I'm unable decode the data.我无法解码数据。

Actually this is possible - of course -.实际上这是可能的——当然——。 The question is: Why would you?问题是:你为什么要这样做?

You - and you should always - send your request via HTTPS which means it's secured and encrypted.您 - 而且您应该始终 - 通过 HTTPS 发送您的请求,这意味着它是安全和加密的。

So it's unnecessary work for the browser and the PHP code.所以对于浏览器和 PHP 代码来说是不必要的工作。

Look into https://github.com/phpseclib/phpseclib for PHP.查看https://github.com/phpseclib/phpseclib以获取 PHP。

And https://www.npmjs.com/package/encrypt-rsa for angular. https://www.npmjs.com/package/encrypt-rsa为 angular。

SSL and SSL pinning is great, but there's always a workaround using MITM attacks, and root certificates to bypass SSL pinning. SSL 和 SSL 固定非常好,但总有一种解决方法,使用 MITM 攻击和根证书绕过 SSL 固定。

When encrypting , you use a public key , and when decrypting you use a private key , if your front-end does not need to decrypt then make sure to not include the private key in your app as there's always ways to inspect variables in runtime and that would invalidate your entire implementation.加密时使用公钥解密时使用私钥,如果前端不需要解密,请确保不要在应用程序中包含私钥,因为总有办法在运行时检查变量和这将使您的整个实施无效。

You can always generate more public keys using the private key, so the other way will also invalidate your entire implementation.您始终可以使用私钥生成更多公钥,因此另一种方式也会使您的整个实现无效。

If you are looking to transmit data securely, I recommend looking at solutions such as JWE+JWS.如果您希望安全地传输数据,我建议您查看 JWE+JWS 等解决方案。

Below are examples using phpseclib for php and encrypt-rsa for angular下面是对 php 使用 phpseclib 和对 angular 使用 encrypt-rsa 的示例

For Encrypting in Angular:对于 Angular 中的加密:

const encryptedText = encryptRsa.encryptStringWithRsaPublicKey({ 
  text: 'hello world',   
  publicKey,
});
console.log(encryptedText);

For decrypting in PHP:在 PHP 中解密:

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('private.key')); // private key

echo $rsa->decrypt($encryptedText);

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

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