最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript RSA decryption using private key - Stack Overflow

programmeradmin1浏览0评论

Hello I have this script that I set in Javascript :

<!DOCTYPE html>
    <html lang="en">
            <head>
            <script src="jquery-1.7.2.min.js" type="text/javascript"></script>
            <script language="JavaScript" type="text/javascript" src="jsbn.js"></script>
            <script language="JavaScript" type="text/javascript" src="rsa.js"></script>
            <script language="JavaScript">

                function encryptData(){
                    var pem ="-----BEGIN PUBLIC KEY-----\
                    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+tii3IwzHa4i142kAB0dRVXoXA2Q8oF48UgMA\
                    AV54+JDED5iVyJK1s7J/xGi4U3+9sRoraE7bS19Nihs5DuYa0gsbKs/5jXOtKiw94fAtMyJTcX0d\
                    SzZhJKcX9vEzI27Hdu1rNFY64Ixz3KjrG1N/pXHtwjE1Ira5XZdTezx0wwIDAQAB\
                    -----END PUBLIC KEY-----";
                    var key = RSA.getPublicKey(pem);
                    var message = "some text to RSA encrypt";
                    var encryptedMessage = RSA.encrypt(message, key);
                    $('#RSAMessageEncrypted').html(encryptedMessage);
                }
            </script>
            </head>
        <body onload=encryptData();>
        <form>
            <strong>RSA encrypted Message :</strong> <textarea rows="10" cols="100"   id="RSAMessageEncrypted"></textarea><br>
        </form> 
        </body>
    </html>

My text area is filled with my message encrypted in RSA using the public key above.

The encrypted message is :

oH1JVWIT7MOH4ObCjFlBuFcAUvooSZANMokFy5jApSi8n0ABVc9vwXP2EFl2x5+UColU6s2VAU+RsZPV4ZgZu3+GhN9pDUb1ZMVyYRECDy/7h+mE+UdDZTPwP1TAcBrspL/XM6+q6mJfUWbEKfR/2vyNUhBxpt6QOhVaI29aHfU=

My question is as following: How can I decrypt my message using the private key pair that this public key was derived from? Is there any library for decryption?

My private key is :

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC+tii3IwzHa4i142kAB0dRVXoXA2Q8oF48UgMAAV54+JDED5iVyJK1s7J/xGi4
U3+9sRoraE7bS19Nihs5DuYa0gsbKs/5jXOtKiw94fAtMyJTcX0dSzZhJKcX9vEzI27Hdu1rNFY6
4Ixz3KjrG1N/pXHtwjE1Ira5XZdTezx0wwIDAQABAoGBAJY4Gd4XZ6t1Epxi4oQ5N33jXGXgMAZp
M5FD8EUCzw1ujWlB96iT8qvZPdLSPMJkUwxyAfELGoI13n3POR46lBhRpYKkTZm1UmEBscdO2B9w
B7YUM9caJG4TgWBKxD8KLriQANnXZrv9zNGx4Cs0USeQgWuDfgkBF+PvL5AVq+jRAkEA+O0MNvBM
kGtGCLt/V34jiivGs9HtXKSJY6vI60GYZYB7D3SniYVUsMocC+O3vyqdpMiO/szfLTAWdXB2yl3Q
VwJBAMQhmQkT57rJ07SS40t1HGonryrmGF3BcHw9sVCijZF/jrbKeL5XmhnbCr9Lhy/InepcSRFI
Pqzj32w0timLi3UCQCVsliKIXCp2RlA3yDxiNXunezc7v4DnJ9S5VfqwoPXZrCa1th6B0irGKBNN
iCmQgaTtljoOmRsVGLtNTj68ff8CQGeiLR/b0bUkEeY3OzzS27nH0EoFnNhlbw9m9btauR0pXnp9
j8FbvoRs9kfQG9WG1tJQAxfLqbpnCdY+IAbhwkUCQQDOIrm78sSuVL+U6f98P2eDSzhKgnepEEVJ
u5iBs3lT5T+p+Xov6DPLd13Pm7Lyppj8S7pW/1CwkTVwW+U3O2kj
-----END RSA PRIVATE KEY-----

Hello I have this script that I set in Javascript :

<!DOCTYPE html>
    <html lang="en">
            <head>
            <script src="jquery-1.7.2.min.js" type="text/javascript"></script>
            <script language="JavaScript" type="text/javascript" src="jsbn.js"></script>
            <script language="JavaScript" type="text/javascript" src="rsa.js"></script>
            <script language="JavaScript">

                function encryptData(){
                    var pem ="-----BEGIN PUBLIC KEY-----\
                    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+tii3IwzHa4i142kAB0dRVXoXA2Q8oF48UgMA\
                    AV54+JDED5iVyJK1s7J/xGi4U3+9sRoraE7bS19Nihs5DuYa0gsbKs/5jXOtKiw94fAtMyJTcX0d\
                    SzZhJKcX9vEzI27Hdu1rNFY64Ixz3KjrG1N/pXHtwjE1Ira5XZdTezx0wwIDAQAB\
                    -----END PUBLIC KEY-----";
                    var key = RSA.getPublicKey(pem);
                    var message = "some text to RSA encrypt";
                    var encryptedMessage = RSA.encrypt(message, key);
                    $('#RSAMessageEncrypted').html(encryptedMessage);
                }
            </script>
            </head>
        <body onload=encryptData();>
        <form>
            <strong>RSA encrypted Message :</strong> <textarea rows="10" cols="100"   id="RSAMessageEncrypted"></textarea><br>
        </form> 
        </body>
    </html>

My text area is filled with my message encrypted in RSA using the public key above.

The encrypted message is :

oH1JVWIT7MOH4ObCjFlBuFcAUvooSZANMokFy5jApSi8n0ABVc9vwXP2EFl2x5+UColU6s2VAU+RsZPV4ZgZu3+GhN9pDUb1ZMVyYRECDy/7h+mE+UdDZTPwP1TAcBrspL/XM6+q6mJfUWbEKfR/2vyNUhBxpt6QOhVaI29aHfU=

My question is as following: How can I decrypt my message using the private key pair that this public key was derived from? Is there any library for decryption?

My private key is :

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC+tii3IwzHa4i142kAB0dRVXoXA2Q8oF48UgMAAV54+JDED5iVyJK1s7J/xGi4
U3+9sRoraE7bS19Nihs5DuYa0gsbKs/5jXOtKiw94fAtMyJTcX0dSzZhJKcX9vEzI27Hdu1rNFY6
4Ixz3KjrG1N/pXHtwjE1Ira5XZdTezx0wwIDAQABAoGBAJY4Gd4XZ6t1Epxi4oQ5N33jXGXgMAZp
M5FD8EUCzw1ujWlB96iT8qvZPdLSPMJkUwxyAfELGoI13n3POR46lBhRpYKkTZm1UmEBscdO2B9w
B7YUM9caJG4TgWBKxD8KLriQANnXZrv9zNGx4Cs0USeQgWuDfgkBF+PvL5AVq+jRAkEA+O0MNvBM
kGtGCLt/V34jiivGs9HtXKSJY6vI60GYZYB7D3SniYVUsMocC+O3vyqdpMiO/szfLTAWdXB2yl3Q
VwJBAMQhmQkT57rJ07SS40t1HGonryrmGF3BcHw9sVCijZF/jrbKeL5XmhnbCr9Lhy/InepcSRFI
Pqzj32w0timLi3UCQCVsliKIXCp2RlA3yDxiNXunezc7v4DnJ9S5VfqwoPXZrCa1th6B0irGKBNN
iCmQgaTtljoOmRsVGLtNTj68ff8CQGeiLR/b0bUkEeY3OzzS27nH0EoFnNhlbw9m9btauR0pXnp9
j8FbvoRs9kfQG9WG1tJQAxfLqbpnCdY+IAbhwkUCQQDOIrm78sSuVL+U6f98P2eDSzhKgnepEEVJ
u5iBs3lT5T+p+Xov6DPLd13Pm7Lyppj8S7pW/1CwkTVwW+U3O2kj
-----END RSA PRIVATE KEY-----
Share Improve this question asked Jan 14, 2013 at 0:27 user1818439user1818439 331 gold badge1 silver badge6 bronze badges 3
  • The rsa module that you're using is only for encryption; to also perform decryption you need rsa2.js and jsbn2.js – Ja͢ck Commented Jan 14, 2013 at 1:03
  • Hello Thanks for the answer can you show me how using jsbn2.js and rsa2.js please I tried but there is no RSA.getPrivateKey(pem) for reading the key or RSA.decrypt(). thanks. – user1818439 Commented Jan 14, 2013 at 2:06
  • I think I have the same problem: we are supposed to read the private key with function "RSASetPrivate(N,E,D)", and then decode the whole with function "RSADecrypt(ctext)". But there does not seem to be a way to simply read the private key, as we would do with openssl directly, with something like "openssl rsautl -decrypt -inkey private.key". – plang Commented Nov 27, 2013 at 14:51
Add a ment  | 

1 Answer 1

Reset to default 8

First of all, I hope that's not the RSA private key that you are going to use in production.

As regards to doing the encryption/decryption on JavaScript side, there are various libraries such as Standford's JSBN RSA Encryption library and ohdave.'s RSA in JavaScript utility for doing simple encryption/decryption, but your main concern with these tools and even your architecture is performance. I would not do RSA encryption with JavaScript unless it was absolutely necessary and there were no other workarounds.

发布评论

评论列表(0)

  1. 暂无评论