Archived Forum Post

Index of archived forum posts


Chilkat RSA C# w/ JSEncrypt JavaScript

Jan 07 '15 at 10:31

I am running into an issue with getting these 2 API's to like each other ;)

General Concept:

This is the error I am getting back (I verified that I do get an encrypted string from the JavaScript)

    DllDate: Aug 15 2013
    UnlockPrefix: XXXXXXXX
    Username: JOSHDEVVM:.NET v4.5
    Architecture: Little Endian; 32-bit
    Language: .NET 4.0
    VerboseLogging: 0
    usePrivateKey: 1
      KeyType: Private
      InputSize: 86
      Padding: OAEP
      HashAlg: SHA-1
      ParamLen: 0
      ModulusBitLen: 1024
      Input size must be a multiple of modulus length
      ModulusByteLen: 128
    decrypt: Elapsed time: 0 millisec

Here is the code:

(Creating the RSA Key Pairs)

private void CreateRSAKeys()
            // If we already have the Private key we
            // don't want to make a new one
            if (!String.IsNullOrEmpty(PrivateKey))

            // Create the RSA Object
            Chilkat.Rsa rsa = new Chilkat.Rsa();

            // Unlock the RSA Component

            // Set Options
            rsa.EncodingMode = "base64";
            rsa.OaepPadding = true;
            rsa.LittleEndian = false;

            // Generate the Key

            // Add Keys to Session
            Session.Add("PublicKey", rsa.ExportPublicKey());
            Session.Add("PrivateKey", rsa.ExportPrivateKey());


(Webmethod to give the public key)

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public static String GetRSAPublicKey()
            // Load the PublicKey form Session
            String publicKeyXml = HttpContext.Current.Session["PublicKey"] as String;

            // In case it is not there.
            if (String.IsNullOrEmpty(publicKeyXml))
                return String.Empty;

            // Create the PubicKey Object
            Chilkat.PublicKey publicKey = new Chilkat.PublicKey();

            // Load the Data

            // Return the PEM Format
            return publicKey.GetOpenSslPem();            

(Decyption Method)
private String Decrypt(String EncData)
            // Create the RSA Object
            Chilkat.Rsa rsaDecryptor = new Chilkat.Rsa();

            // Unlock the RSA Component

            // Set Options
            rsaDecryptor.EncodingMode = "base64";            
            rsaDecryptor.OaepPadding = true;
            rsaDecryptor.LittleEndian = false;

            // Load the Private Key

            // Decrypt
            String clearText = rsaDecryptor.DecryptStringENC(EncData, true);

            return clearText;

(JavaScript) (Get the Key via Akax)

function GetRSAKey() {

            var RSAPublicKey;

                type: 'GET',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                async: false,
                url: 'Default.aspx/GetRSAPublicKey',
                success: function (result) {
                    RSAPublicKey = result.d;

            return RSAPublicKey;

(Encrypt on Click)

function ClientClick() {            
            var encrypt = new JSEncrypt();

            var encrypted = encrypt.encrypt($("#UserData").val());

Any help would be greatly appreciated.


After Posting this Figured out the issue :)

Even though in the code for the JSEncrypt it says it is using the PKCS1 v2.0 padding it is not is is using the PKCS1 v1.5 changed that and it works.