'How to sign data with RSA private Key on Javascript/jquery

I got this C# code but I need it to be done in Javascript. The idea is that I need to execute this bit of code when there is no internet in the browser, basically, I have to save some data in the local storage and then use them to execute this method to generate a private key.

private static string SignData(string ppath,string ppass)
{
    X509Certificate2 keyStore = new X509Certificate2(ppath, ppass);
    RSA privateKey = keyStore.GetRSAPrivateKey();
    byte[] iicSignature = privateKey.SignData(Encoding.ASCII.GetBytes(pConcat), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

    byte[] iic = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(iicSignature);
    return BitConverter.ToString(iic).Replace("-", string.Empty);
}

P.S: I managed to extract the private key and save it to local storage.Now I need to sign some data with this key,SHA256 algorithm and pkcs1 padding.



Solution 1:[1]

First of use Bouncy Castle to extract your private key from the certificate and convert it to pkcs8.

Then you will need to find a javascript library that does sign data with a private key. I suggest you use the window.crypto library that is supported by most browsers. It has a method called window.crypto.subtle.sign() which signs data using a private key you have to import with window.crypto.subtle.importKey() you can read more here about this library.

And finally you will need a md5 library to hash the signed data. You can find that here.

It's mostly what you've talked about in this chat.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1