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
Comments
I have the same problem. |
Hello, [...] 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 It has sense, because decrypt with the public key would make anyone allowed to decrypt. |
Yeah, but I need to decrypt the encrypted string by private key with public key to achieve digital signature. |
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: By the way, I transplanted it to Classic ASP. How poor I am, holy ****! =.=.... |
did you solved it?I have the same problem |
@XXMY did you solved it? |
@xudao520 I haven't study this problem for months, and gave it up, sorry for your inconvenience. |
@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 |
@xudao520 |
sorry, I'm not in line with your request. |
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 You are receiving this because you commented. |
@zsxsoft
|
@XXMY |
@zsxsoft 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. |
@XXMY 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; |
Sorry, this test failed. EDITED. Amazing |
@zsxsoft |
me too! |
me too! How to fix it? |
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 decrypt = new JSEncrypt(); |
As [axellettieri] and [mctrivia] said, jsencrypt only works with public key encryption and private key decryption. |
@shacaisheng |
@XXMY Any solution to decrypt by public key? i'm stuck here for two days |
need this too. |
do you have a solution? |
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. |
@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。 |
@shiyunsheng 你是用js 来加密数据,然后也是用js来解密这些数据么? |
@shacaisheng 服务端用私钥加密,js端用公钥解密。。Android和ios都能实现,但js这边没查到资料。 |
没错,我以前也是这样的,其他端解密是ok的,遇到长字符串的时候才会解密失败。 |
@shiyunsheng |
把加密数据分割几个部分,然后用下面的代码可以正常使用吗? |
@shiyunsheng 你是测试一下短的字符串看能不能成功。还不行的话,就是你接入姿势有问题了 |
@shacaisheng 6位数字解密不行。能否贴一下你具体代码? |
@ALL 基本常识----------------RSA加密字符串长度是有限的,大约100来个字符。加密长字串,请用AES.
543002649@qq.com
From: loven sha
Date: 2018-04-28 12:02
To: travist/jsencrypt
CC: Subscribed
Subject: Re: [travist/jsencrypt] How to decrypt the encrypted string by private key with public key? (#70)
@shiyunsheng 你是测试一下短的字符串看能不能成功。还不行的话,就是你接入姿势有问题了
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@chinafool @shacaisheng 是服务器加密后的数据。返回给js端。客户端用的是公钥解密。。这个我查了资料jsencrypt库是不支持的。( js用公钥加密,服务器私钥解密是正常的。) |
As [axellettieri] and [mctrivia] said, jsencrypt only works with public key encryption and private key decryption. |
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. |
@shiyunsheng 加我qq 942152020 |
@shacaisheng 查不到942152020这个QQ |
jsencrypt公钥解密有解决的嘛? |
没找到解决办法,通过接口查的 |
seems no solution to decrypt by public key on js, but java works well |
This should not be closed. This is something that is required |
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. |
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. |
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 ? |
您好!您的来信已收到,谢谢!
|
@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. |
thank you for letting me know, can you recommand any solution which is working within the browser |
why closed |
您好!您的来信已收到,谢谢!
|
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!
The text was updated successfully, but these errors were encountered: