简体   繁体   English

Javascript到PHP-使用模数和指数进行RSA加密

[英]Javascript to PHP - RSA encrypt with modulus and exponent

I have code in javascript https://notepad.pw/codeherejs 我在javascript https://notepad.pw/codeherejs中有代码

var c = 'd3bcef1f00424f3261c89323fa8cdfa12bbac400d9fe8bb627e8d27a44bd5d59dce559135d678a8143beb5b8d7056c4e1f89c4e1f152470625b7b41944a97f02da6f605a49a93ec6eb9cbaf2e7ac2b26a354ce69eb265953d2c29e395d6d8c1cdb688978551aa0f7521f290035fad381178da0bea8f9e6adce39020f513133fb';
var f = '10001';
var g = new RSAKey;
    g.setPublic(c, f);
    var result = g.encrypt(password)

It working fine. 工作正常。 And give me result is result = 1e2738d762382f82f8412b87e9dd9a18aaa52ea28f6b204bb0f1e0f710a973aaa417e533d39127fd1d8959c553ae53dd83738e4eb6544cb77fd08438afd33594c42ff7e5186bd23908b642188b82b7552140af82f7bd5d768770cd9305573640739be4a70bbc2bd190c5a192685ab88c5a612680005eff2f37944c8e24803ea2 并给我结果是result = 1e2738d762382f82f8412b87e9dd9a18aaa52ea28f6b204bb0f1e0f710a973aaa417e533d39127fd1d8959c553ae53dd83738e4eb6544cb77fd08438afd33594c42ff7e5186bd23908b642188b82b7552140af82f7bd5d768770cd9305573640739be4a70bbc2bd190c5a192685ab88c5a612680005eff2f37944c8e24803ea2

I conver the code to PHP, i use phpseclib 我将代码转换为PHP,我使用phpseclib

$fm_modulus = 'd3bcef1f00424f3261c89323fa8cdfa12bbac400d9fe8bb627e8d27a44bd5d59dce559135d678a8143beb5b8d7056c4e1f89c4e1f152470625b7b41944a97f02da6f605a49a93ec6eb9cbaf2e7ac2b26a354ce69eb265953d2c29e395d6d8c1cdb688978551aa0f7521f290035fad381178da0bea8f9e6adce39020f513133fb';

$fm_exponent = '10001';

$rsa = new Crypt_RSA();
$modulus = new Math_BigInteger(base64_decode($fm_modulus), 256);
$exponent = new Math_BigInteger(base64_decode($fm_exponent), 256);

$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
$pass_ok = $rsa->encrypt($pass);

PHP return for me the symbol, i don't know how to convert it to plain text (same result in javascript). PHP为我返回了符号,我不知道如何将其转换为纯文本(JavaScript中的结果相同)。 I try base64, hash sha256 sha512. 我尝试base64,哈希sha256 sha512。 All false, please help me 都是假的,请帮帮我

$fm_modulus and $fm_exponent aren't base64-encoded nor are they in base-256. $fm_modulus$fm_exponent不是base64编码的,也不是在base-256中。 They're hex encoded and that's it. 它们是十六进制编码的,仅此而已。 So try this: 所以试试这个:

$rsa = new Crypt_RSA();
$modulus = new Math_BigInteger($fm_modulus, 16);
$exponent = new Math_BigInteger($fm_exponent, 16);

I was able to recognize this by the fact that $fm_exponent is 10001. 10001 is the hex encoding of 65537 , which is a pretty common RSA exponent. 我能够通过$fm_exponent为10001的事实认识到这一点$fm_exponent65537的十六进制编码,这是一个非常常见的RSA指数。

I also suspect the padding mode phpseclib is using may not be correct. 我也怀疑phpseclib使用的填充模式可能不正确。 You might need to do $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_NONE); 您可能需要执行$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_NONE); or $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); .

It actually is somewhat hard to know for sure since you didn't post the private key so the result can't be decrypted. 实际上,由于您没有发布私钥,因此很难确定确切的信息,因此结果无法解密。 You also didn't post the plaintext of what you're trying to encrypt. 您也没有发布要加密的明文。 As Maarten Bodewes said, with good padding modes, the ciphertext will be different every time you try to encrypt the same plaintext but it's possible your JS library isn't using a good padding mode. 正如Maarten Bodewes所说,使用良好的填充模式,每次尝试加密相同的纯文本时,密文都会有所不同,但是JS库很可能没有使用良好的填充模式。 phpseclib, by default, uses OAEP, which does have randomized output, but "textbook RSA" doesn't use randomized padding (or any padding, really). 默认情况下,phpseclib使用OAEP,它确实具有随机输出,但是“教科书RSA”不使用随机填充(或任何填充)。

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

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