login about faq

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

General Concept:

  • Server Side - Creates the RSA Public / Private Keys
  • Webmethod Gives the Client Side Java the Public Key.
  • JavaScript uses the Public Key to encrypt the Data and submits to the Server
  • Server Decrypts the Data.

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.

asked Jan 07 '15 at 09:56

ColdFireDragon's gravatar image

ColdFireDragon
162

edited Jan 07 '15 at 10:25


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.

link

answered Jan 07 '15 at 10:31

ColdFireDragon's gravatar image

ColdFireDragon
162

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×47

Asked: Jan 07 '15 at 09:56

Seen: 1,554 times

Last updated: Jan 07 '15 at 10:31

powered by OSQA