Author Topic: RSA Async encryption in nodejs decrypt in C#  (Read 3572 times)


  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
RSA Async encryption in nodejs decrypt in C#
« on: December 18, 2017, 04:49:53 AM »

Im trying to encrypt a string in nodejs and decrypt it in C#.
Of course using Chilkat libs on both sides.

here is my JS code:

Code: [Select]

  var rsa = new chilkat.Rsa();

  var success = rsa.UnlockComponent("Anything for 30-day trial");
  if (success !== true) {

  //  Generate a 1024-bit key.  Chilkat RSA supports
  //  key sizes ranging from 512 bits to 4096 bits.
  //  Note: Starting in Chilkat v9.5.0.49, RSA key sizes can be up to 8192 bits.
  //  It takes a considerable amount of time and processing power to generate
  //  an 8192-bit key.
  success = rsa.GenerateKey(1024);
  if (success !== true) {

  //  Keys are exported in XML format:
  var publicKey = rsa.ExportPublicKey();

  var privateKey = rsa.ExportPrivateKey();

  rsa.EncodingMode = "base64";

  var encryptedString = rsa.EncryptStringENC("Hello Hello", false);

  fs.writeFileSync('c:\\temp\\private.pem', privateKey)
  fs.writeFileSync('c:\\temp\\', publicKey)
  fs.writeFileSync('c:\\temp\\encrypted.txt', encryptedString);

And here is my C# code
Code: [Select]

            Chilkat.Rsa rsa3 = new Chilkat.Rsa();
            Chilkat.PrivateKey privKey2 = new Chilkat.PrivateKey();

            var success = privKey2.LoadPemFile(@"C:\temp\private.pem");
            var privKeyXml = privKey2.GetXml();
            success = rsa3.ImportPrivateKey(privKeyXml);

            //  The encrypted AES key is encoded using base64, so set
            //  our EncodingMode to "base64".
            rsa3.EncodingMode = "base64";

            string decryptedAesKey = rsa3.DecryptStringENC("wo2ACBNVtz9NdrJfa0BVqInf0qE/UmYFmFkJ4VsId6Wk/JocQyhF2eCuRE3thO6kDO4WghAZ2/DsCUoFZzbQ+XZZdA9i3fp0hs6qFcLKtKjjbrA5ysFhh4G5AtJcvP8g27BJHvP+cnP3ugJssuWlDbR86X8jz5oJg/IAfmipg3c=", true);
            Console.WriteLine("decryptedAesKey = " + decryptedAesKey);

Ill be happy to have your support on this issue.