Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to decrypt the encrypted string by private key with public key? #70

Closed
XXMY opened this issue Apr 19, 2016 · 53 comments
Closed

How to decrypt the encrypted string by private key with public key? #70

XXMY opened this issue Apr 19, 2016 · 53 comments

Comments

@XXMY
Copy link

XXMY commented Apr 19, 2016

I met a problem when I use the public key to decrypt the encrypted data by private key. However, there will be an exception when I use jsencrypt's decryption method directry, like following:
var decrypt = new JSEncrypt();
decrypt.setPublicKey(publicKey);
var decoded = decrypt.decrypt(encoded);

Furthermore, how to split the encrypted data into parts in decryption with private key while the data is too long?
Thank you for anyone can help me!

@jokerlin
Copy link

I have the same problem.

@axellettieri
Copy link

Hello,
From the official page (http://travistidwell.com/jsencrypt/):

[...] crypt.setKey(YOUR_OPENSSL_PRIVATE_OR_PUBLIC_KEY); //You can use also setPrivateKey and setPublicKey, they are both alias to setKey

//Eventhough the methods are called setPublicKey and setPrivateKey, remember
//that they are only alias to setKey, so you can pass them both a private or
//a public openssl key, just remember that setting a public key allows you to only encrypt [...]

It has sense, because decrypt with the public key would make anyone allowed to decrypt.

@jokerlin
Copy link

Yeah, but I need to decrypt the encrypted string by private key with public key to achieve digital signature.

@zsxsoft
Copy link

zsxsoft commented Aug 5, 2016

I've found the solution, but I'm not sure whether it's correct all the times, because I didn't read any document about RSA yet. It's just a trick. So I'll not PR until anyone tell me what did I do.

My changes:
zsxsoft-deprecated@6b1a3a6

By the way, I transplanted it to Classic ASP. How poor I am, holy ****! =.=....

@Chaunjie
Copy link

did you solved it?I have the same problem

@Chaunjie
Copy link

@XXMY did you solved it?

@XXMY
Copy link
Author

XXMY commented Aug 25, 2016

@xudao520 I haven't study this problem for months, and gave it up, sorry for your inconvenience.

@Chaunjie
Copy link

@XXMY I have solved this problem ,client take serve_publicKey, client_privateKey and serve take client_publicKey , serve_privateKey . so client encrypt data with serve_publicKey ,server decrypt with serve_privateKey and server encrypt data with client_publicKey ,client decrypt with client_privateKey

@XXMY
Copy link
Author

XXMY commented Aug 26, 2016

@xudao520
Two RSA key pairs, clearly you use public key to encrypt and use private key to decrypt, it not solves the problem. What I posted is how to use public to decrypt.
Sure, you enabled client to decrypt data, it is a new method, although sounds a bit complex.

@Chaunjie
Copy link

sorry, I'm not in line with your request.

@zsxsoft
Copy link

zsxsoft commented Aug 26, 2016

OK, did anyone had read my reply? My changes made a trick but solved the problem.

Get Outlook for Android

On Fri, Aug 26, 2016 at 11:12 AM +0800, "Fangwei Cai" notifications@github.com wrote:

@xudao520
Two RSA key pairs, clearly you use public key to encrypt and use private key to decrypt, it not solves the problem. The problem I posted is how to use public to decrypt.
Sure, you enable client to decrypt data, it is a new method, although sounds a bit complex.

You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#70 (comment)

@XXMY
Copy link
Author

XXMY commented Aug 26, 2016

@zsxsoft
I included your modified file, just jsencrypt.js into my script, but it works fine in public key encrypt and private key decrypt. While use private key to encrypt and public key to decrypt, the decryption result is null
my script is:
var publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALpJXQXmlFbJo78DhqUcKNGkXna5bEqBxtQ4bg4NgTTK+udPm6fEynonLY7BRucAee9cOw49vsddDeGCcvhoY8kCAwEAAQ==";

var privateKey = "MIIBOQIBAAJBALpJXQXmlFbJo78DhqUcKNGkXna5bEqBxtQ4bg4NgTTK+udPm6fEynonLY7BRucAee9cOw49vsddDeGCcvhoY8kCAwEAAQJARdVqweOGiGJeuCSiPRMpWi28G9W2+tdbqgLdfvchEKDDCuxDY5ZZ1eLTJtFx1jJNoBWarks8WCnM4tlt0rqgAQIhAOkBQFYkZd5mUC16HkDIx4oBflvdCx6lbRhQX5GkaoWBAiEAzKvMRG5Yh8AuRJX24LFk1A/YF1Yl7V9PWrfVQ7vFUkkCIAh6vC2mW3KxhH9zL0CqTeCu1ztemvX9jS/SVPgG5L2BAiB2TtxttgdQuSNFnBYv/frVpi74FmEoHE7nlpodZB4EmQIgUEnhwSLlEFHjwhjbE8S2VoIElmUKV83mak5rOrs8LBw=";

var encrypt = new JSEncrypt();
var decrypt = new JSEncrypt();

encrypt.setPublicKey(publicKey);
decrypt.setPrivateKey(privateKey);
//encrypt.setPrivateKey(publicKey);
//decrypt.setPublicKey(privateKey);

var content = "abcdefg";
console.log("Content: " + content);

var encoded = encrypt.encrypt(content);
console.log("Encoded: " + encoded);

var decoded = decrypt.decrypt(encoded);
console.log("Decoded: " + decoded);

@zsxsoft
Copy link

zsxsoft commented Aug 26, 2016

@XXMY
Are you sure your jsencrypt.js is correct? I tested in Internet Explorer 6 & Chrome 54.
image

@XXMY
Copy link
Author

XXMY commented Aug 26, 2016

@zsxsoft
I'm sure the jsencrypt.js I used is correct.
The script you tested is use public key to encrypt and use private key to decrypt, and I said it is fine.
image

You said you have solved the problem, so I try to use private key to encrypt data and use public key to decrypt, but failed.
20160826145237

@zsxsoft
Copy link

zsxsoft commented Aug 26, 2016

@XXMY
I re-test it with PHP, passed.

index.html:

<script src="jsencrypt.js"></script>
<script>
var publicKey = "\
-----BEGIN PUBLIC KEY-----\
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCn+gSMOvb+6oi2eWqmxlt/qoq4\
3S2j7yXrLhIhtS02NPE+t14ZAQxMiJd5YPKps5ZcT2JhIdTv75ZPFmnj2+E0MhL2\
XkCfkW6LEg4xVMr8TeV5cyVtRRjd8XkL+awA8niJKNYIJk8y/3112cT7TnrkB6Ct\
4LHrrn2FG2Y9nVn8hQIDAQAB\
-----END PUBLIC KEY-----\
";
var encrypt = new JSEncrypt();
var content = "abcdefg";
encrypt.setKey(publicKey);

console.log("Content: " + content);
var encoded = encrypt.encrypt(content);
console.log("Encoded: " + encoded);
fetch('http://localhost/rsa.php', {method: 'POST', body: encoded}).then(data => data.text()).then(k => {
    console.log('Decoded: ' + encrypt.decrypt(k));
});
</script>

PHP Code:

<?php
header('Access-Control-Allow-Origin: *');
define('PRIVATE_KEY', '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCn+gSMOvb+6oi2eWqmxlt/qoq43S2j7yXrLhIhtS02NPE+t14Z
AQxMiJd5YPKps5ZcT2JhIdTv75ZPFmnj2+E0MhL2XkCfkW6LEg4xVMr8TeV5cyVt
RRjd8XkL+awA8niJKNYIJk8y/3112cT7TnrkB6Ct4LHrrn2FG2Y9nVn8hQIDAQAB
AoGAXK0GYVAPAinn77UhcI4z4UX4b3IoQjApnY23lz1cinG/QDjvA6CeZoNd/yvL
9nEM3jU2NBz0XMS1C0F0frDLmJlZu3ko1EkPdN19bsDFKhNduOMugtT6s4w8WrrJ
XonnDQClddC+XNieuY2tF7d8+RK4fjvailbzrMaZZN+xKikCQQDc0PKsXCXhAwDf
1a+fzBdj+6mDBafe5p32pgXjCsBYzLk3A3UHsNPb3dQsXEmMLKQSGAe1w8W32Ugt
bst5nuGHAkEAwr3A5T+6D8aeMpnXX8CrOMlQJqxICy26p/Iyiv8nD4dCO5RAv76w
FdlN504QRf1Oj3FVuMtpifZ+7X+DhTykkwJAdz9fzjT1P86fakG71lAhUZ1Wrasg
PP/NzqVaCIKF3W6xl0QGr2CPCO2C53HvRgPVlu/jOgW/gMmWcPKkb8mdgQJBAI2N
SioY8VDISXN0eaXDMXIceqMxtUhS3At5tB1uq+DQq2cNMydtHycyhjrsdk25eyIu
+mVRjgxXXRmbB2mQk/sCQD1JWmiBihJkMB7ujbWDdrmsXe3RT6vSQCVmtVF/u5iF
t4FbIesVNmw1pOMe5v48E+utJ1ecOFGBWtcB395zmRU=
-----END RSA PRIVATE KEY-----');
$encrypted = file_get_contents('php://input');
$decrypted = '';
$privateKey = openssl_pkey_get_private(PRIVATE_KEY);
openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privateKey);
openssl_private_encrypt($decrypted, $encrypted, $privateKey);
$encrypted = base64_encode($encrypted);
echo $encrypted;

image
image

@zsxsoft
Copy link

zsxsoft commented Aug 26, 2016

Sorry, this test failed. EDITED.
Test result is my changes can't decrypt the data encrypted by jsencrypt itself.

Amazing

@shacaisheng
Copy link

@zsxsoft
can you help me?
i have the same problem when the string is too long.

@ystarlongzi
Copy link

me too!

@tansj526
Copy link

tansj526 commented Feb 8, 2017

me too! How to fix it?

@mctrivia
Copy link

mctrivia commented Feb 10, 2017

I also can't get script to work to verify signature.

Signature generated in php by using private key to encrypt hash of data and time stamp.

Public key provided to user along with encrypted value.

Decrypting this value and verifying hashs match proves I provided file at specific time.

Script only seems to work for public encrypt private decrypt. Not private encrypt public decrypt.

`
var key='-----BEGIN PUBLIC KEY-----'+ 'MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqkPnt7bw5EFYKiJNOLF7Im4T5FkWo5lw5nHoViUp6f+5/Csc6q7aGxfEIDrQ341n3qIs8RYoOsM9q7zMx4TYvlp5W0DRmhOt2/FSyA2nPkoGb4XTGLhNCl85BqSBB9UGYlT7z8iTgBhzGFfuo1vjvb64zHTmhGaPZAfCSkDFedBviXE2+bOmnxbRE4WlOpbD/+NIIPWXd6fOijOjtS8kXrE53bLQ6UszN9mn8LaRxpnEoesJ6R1+7ql+S3dCFK2/36tqa2rtnuKaqYL0GP7FIFlWz82jGT4RdB+u4kgveeA/X17uAH9skUA8wMfiIjfTZO0haj7pFrn8hV0MJJLeM/bfrnkLJvhiXTnMPMOdZrZJBaE5UM/M4OmD5TSzTq4oW4uIQj2KJQ3+7urO8RiJ4YkiAqrnVV3MQJVcMZHKSxwbCMZznMw5mXkodm8qKE6S3aQgCxHCrygA/uWK7Bh7NoLJCpy7uGz7TgLFq97okpEWaQSUTFwFBU6MQPiOIT2XdwVl696tOt06nRN1WXL44Pe5nIwOr0dRfsFIafs+OyS71cYbRr7ec+YEc7AphCFHlEYcx0c0pbrWkHo8gNWvbma1dwy0km037WupDJ1O4pgvbD7GnFvCo7QjQ5ViyRDWDlUpvJWpYfyUeyiQ59fBR6eaVQ18U26SPTLkiYPmYsUCAwEAAQ=='+
'-----END PUBLIC KEY-----';
var sig='W1ktVDN+tSDeREhtU1tu8E5QY15+EIurl4VXprPyTNC+8ryVir65qbNLOPltUF+0xWxnussndpQsQjy4azp+8IyO6owIfgXbLvHkL1wnJ6yYVWFMHQ6duvhi78qpwNu2KEmMrfISfacbXeTHz/3NRHKc/Qac8+OMa9R9NJGM4BVCP9+RjOuqwC3GV0QQSXx8+8wtmrEeAx+0JXAZC9e8wYAqtLnvoWh8aK7m84yIbBegr4XPnCjU3THoz9l02TEB3Wz6J3o/+sxmz2NgloMfMXcDRXuYy6tpgjutPIoUBtFShlGrrqOmtvqG4mmgz5kC5cnNH0Q7yAsMhYm4Mra324ZTkYJHh3GXeYmJt6u1iohV9NZrMzEXsE5le7KsUQFsIK20fwcr2AVMT+KWQ/H+md8R6OzuANhibDt+NdOXD9ujxbgFkv6IUPgCX2Gv89RD5hBcSTibJPc3NmZuDzmHpXzIqBTOQ4sE+OZtOKGQ28Q7zkyopDKcW9nfbmt3LMb0grKJN/nHFG3aU42DH146va2P6DwAeNrMQ71axPBDBVINH50yHhF6M83ULjdQT7MJU8s5749yuDiD9dKco5riLCTg0VvoShOmQPE0k2JYG3DNT2mUVk/FOXN9aJhpDD32aeT/TSihQ2e5YMu5V+X9N9FURtiZ78XMUhoB5Cw/I5s=';

var decrypt = new JSEncrypt();
decrypt.setPublicKey(key);
var uncrypted = decrypt.decrypt(sig);
console.log(uncrypted);`

@XXMY XXMY closed this as completed Feb 10, 2017
@XXMY XXMY reopened this Feb 10, 2017
@XXMY
Copy link
Author

XXMY commented Feb 11, 2017

As [axellettieri] and [mctrivia] said, jsencrypt only works with public key encryption and private key decryption.

@XXMY XXMY closed this as completed Feb 11, 2017
@XXMY
Copy link
Author

XXMY commented Feb 11, 2017

@shacaisheng
While the plain text is too long to encrypt, you can split text into several parts, the character length of each part is in limitation, finally put the encrypted parts together as a long ciphertext.

@sunnyjocker
Copy link

@XXMY Any solution to decrypt by public key? i'm stuck here for two days

@hiwanz
Copy link

hiwanz commented Nov 22, 2017

need this too.

@shiyunsheng
Copy link

shiyunsheng commented Apr 27, 2018

do you have a solution?

@shiyunsheng
Copy link

shiyunsheng commented Apr 28, 2018

@hiwanz @endlessbest @XXMY How did you solve it? please help me

@shacaisheng
Copy link

While the plain text is too long to encrypt, you can split text into several parts, the character length of each part is in limitation, finally put the encrypted parts together as a long ciphertext.
@shiyunsheng

@shiyunsheng
Copy link

@shacaisheng Thank you for your answer.It's not a question of length,My question is to decrypt it with the public key not private key。

@shacaisheng
Copy link

@shiyunsheng 你是用js 来加密数据,然后也是用js来解密这些数据么?

@shiyunsheng
Copy link

@shacaisheng 服务端用私钥加密,js端用公钥解密。。Android和ios都能实现,但js这边没查到资料。

@shacaisheng
Copy link

没错,我以前也是这样的,其他端解密是ok的,遇到长字符串的时候才会解密失败。

@shacaisheng
Copy link

@shiyunsheng
没错,我以前也是这样的,其他端解密是ok的,遇到长字符串的时候才会解密失败。js解密短的是ok的

@shiyunsheng
Copy link

把加密数据分割几个部分,然后用下面的代码可以正常使用吗?
var decrypt = new JSEncrypt();
decrypt.setPublicKey(key);
var uncrypted = decrypt.decrypt(sig);

@shacaisheng
Copy link

@shiyunsheng 你是测试一下短的字符串看能不能成功。还不行的话,就是你接入姿势有问题了

@shiyunsheng
Copy link

@shacaisheng 6位数字解密不行。能否贴一下你具体代码?

@hrp61
Copy link

hrp61 commented Apr 28, 2018 via email

@shiyunsheng
Copy link

@chinafool @shacaisheng 是服务器加密后的数据。返回给js端。客户端用的是公钥解密。。这个我查了资料jsencrypt库是不支持的。( js用公钥加密,服务器私钥解密是正常的。)

@shiyunsheng
Copy link

As [axellettieri] and [mctrivia] said, jsencrypt only works with public key encryption and private key decryption.

@mctrivia
Copy link

Most places I wanted to use it the other way around I have found are better suited for block chain tech. Check out digibyte as a way to deal with your signature needs.

@shacaisheng
Copy link

@shiyunsheng 加我qq 942152020

@shiyunsheng
Copy link

@shacaisheng 查不到942152020这个QQ

@jetBn
Copy link

jetBn commented Jun 28, 2018

jsencrypt公钥解密有解决的嘛?

@shiyunsheng
Copy link

没找到解决办法,通过接口查的

@sunnyjocker
Copy link

seems no solution to decrypt by public key on js, but java works well

@developeranirudhprabhu
Copy link

This should not be closed. This is something that is required

@netsrac
Copy link

netsrac commented Dec 31, 2018

Since it's still not possible to encrypt with the private key and decrypt with the public key using JSEncrypt, I found another library accomplishing this task: rzcoder/node-rsa

Maybe that helps somebody.

@ForteanOrg
Copy link

It is entirely feasible to have a server encrypt data with it's private key, then send that encrypted data over to the client to decrypt it with the the server's public key. As was pointed out correctly, this is not a way to improve confidentiality - as indeed everyone can fetch the public key and decode the encrypted data. But it can serve as a control to allow non-repudiation. The client should obtain the public key from the server, ensure that it indeed is the public key from the server and if a message is received from the server and can be decoded with the public key, this is in effect proof that the message indeed came from the server and not from any other source. Therefore, it is quite legit to want to have the option to be able to use the public key to decrypt a message.

@0xCCD
Copy link

0xCCD commented Feb 10, 2022

I would like to ask what the Problem is with the decrypt process with the public key when it was encrypted with the private key. Is it a time issue from the maintainer of the lib, or is it a technical issue which can not be solved with pure javascript ?

@shacaisheng
Copy link

shacaisheng commented Feb 10, 2022 via email

@travist
Copy link
Owner

travist commented Feb 10, 2022

@0xCCD it is a time issue on my part. If you need this specific use-case, then you may need to try a different library.

@0xCCD
Copy link

0xCCD commented Feb 10, 2022

thank you for letting me know, can you recommand any solution which is working within the browser

@FengShangWuQi
Copy link

why closed

@shacaisheng
Copy link

shacaisheng commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests