Archived Forum Post

Index of archived forum posts

Question:

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)

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

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))
                return;

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

            // Unlock the RSA Component
            rsa.UnlockComponent("XXXXXXXX_XXXXXXXXXXXX");

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

            // Generate the Key
            rsa.GenerateKey(1024);

            // 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
            publicKey.LoadXml(publicKeyXml);

            // 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
            rsaDecryptor.UnlockComponent("XXXXXXX_XXXXXXXXXX");

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

            // Load the Private Key
            rsaDecryptor.ImportPrivateKey(PrivateKey);

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

            return clearText;
}

(JavaScript) (Get the Key via Akax)

function GetRSAKey() {

            var RSAPublicKey;

            $.ajax({
                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();

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

Any help would be greatly appreciated.


Answer

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.