Skip to content

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

Closed
@XXMY

Description

@XXMY

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!

Activity

jokerlin

jokerlin commented on Apr 25, 2016

@jokerlin

I have the same problem.

axellettieri

axellettieri commented on Apr 29, 2016

@axellettieri

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

jokerlin commented on Apr 29, 2016

@jokerlin

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

zsxsoft

zsxsoft commented on Aug 5, 2016

@zsxsoft

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

Chaunjie commented on Aug 25, 2016

@Chaunjie

did you solved it?I have the same problem

Chaunjie

Chaunjie commented on Aug 25, 2016

@Chaunjie

@XXMY did you solved it?

XXMY

XXMY commented on Aug 25, 2016

@XXMY
Author

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

Chaunjie

Chaunjie commented on Aug 26, 2016

@Chaunjie

@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

XXMY commented on Aug 26, 2016

@XXMY
Author

@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

Chaunjie commented on Aug 26, 2016

@Chaunjie

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

zsxsoft

zsxsoft commented on Aug 26, 2016

@zsxsoft

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

XXMY commented on Aug 26, 2016

@XXMY
Author

@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

zsxsoft commented on Aug 26, 2016

@zsxsoft

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

XXMY

XXMY commented on Aug 26, 2016

@XXMY
Author

@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

zsxsoft commented on Aug 26, 2016

@zsxsoft

@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

41 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @travist@hiwanz@0xCCD@zsxsoft@ystarlongzi

        Issue actions

          How to decrypt the encrypted string by private key with public key? · Issue #70 · travist/jsencrypt