Duplicating a javascript's RSA encryption library in C#

I'm having problems trying to basically simulate this javascript&#

Page 1 of 1

1 Replies - 4183 Views - Last Post: 27 January 2010 - 07:54 PM Rate Topic: -----

#1 Gluebag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-November 08

Duplicating a javascript's RSA encryption library in C#

Post icon  Posted 27 January 2010 - 07:46 PM

Hello, I was wondering if you guys could help me with this problem I'm having with RSA encryption. I'm fairly new to it and cannot seem to get this done. Basically what I'm trying to do is, there is a library that I use on one of my websites, and function, that when you call it, it encrypts a string with RSA and then returns the encrypted value. I then take that and decrypt it later. But what I'm trying to do here is, simply get the same output of that javascript encrypt, but in a C# program that I'm currently working on. I'm using this ChilkatSoft component in c#, only because it has gotten me closer to the goal than the built in Cryptography one in .NET, but im open for anything. Ok, time for some code...

The javascript function that im trying to simulate: (the library im using for it is from http://www-cs-studen...w/jsbn/rsa.html

var radRSAKey = {
	RSAPubKey_N: "b3f80a6b220fdaa6c08dc4dbccf1c76fdb0acfd3e5fee420797838c1c8a76849e69acdb0ebb55318f488a968335dbe0e0730f8e927ff0212c8715a5aef5bffa722b17902100e12c40b2cf65e15ea603063630887acccb9ba841fb4809b6bfbb5f61a001b7307b7fc528c51bd111e7be4082999e61fef165c4ad1503ece7d566b",
	RSAPubKey_E: "10001"
}

function radEncrypt( s ){
	if(s == undefined || !s) return null;
	var rsa = new RSAKey();
	rsa.setPublic(radRSAKey.RSAPubKey_N, radRSAKey.RSAPubKey_E);
	return rsa.encrypt(s);
}



My C# attempt at simulating that:

				 Chilkat.Rsa rsa = new Chilkat.Rsa();
				byte[] ky = Encoding.ASCII.GetBytes("b3f80a6b220fdaa6c08dc4dbccf1c76fdb0acfd3e5fee420797838c1c8a76849e69acdb0ebb55318f488a968335dbe0e0730f8e927ff0212c8715a5aef5bffa722b17902100e12c40b2cf65e15ea603063630887acccb9ba841fb4809b6bfbb5f61a001b7307b7fc528c51bd111e7be4082999e61fef165c4ad1503ece7d566b");
				byte[] ky2 = Encoding.ASCII.GetBytes("10001");
				string base64 = Convert.ToBase64String(ky);
				string base642 = Convert.ToBase64String(ky2);
				rsa.UnlockComponent("keyforthechilkatcomponent");
				rsa.ImportPublicKey("<RSAKeyValue><Modulus>" + base64 + "</Modulus><Exponent>" + base642 + "</Exponent></RSAKeyValue>");
				rsa.EncodingMode = "hex";
				string theE = rsa.EncryptStringENC("stringiwantencrypted", false);



Now the output for each one is as follows:

Javascript (What I Want): 47af76435a51a7ab3449bcafcd85c960f2b253bbebe4b90acf173d11909bb3e5fe403a476aded386372badc026c53e895c44156f54219fd7a8875a2ded658fa4eae8a986c1ff69085b04da66c2c9ee1dc7d20b4f42e6528a07c6f0709172829d84ce32b39814ed0a69a69a23781fed48c2c4620fb0320e6b3772a167ef303671

And MY C#: Code:
5ee15a7b63dcb2474a8b35fd5d0b7c1c412355a0bf5556cbe0cb33dac44e0904baaf19cf57933f61f8d3e82f847e2bfa1a04a7bd185fba85362a9a474b9c627e4745cd7d477b2e46697c7ef0b9490ba3e335d52a972819754b7abbea8183335c8dbde0ba37dc1b199ac6e28e77576f180f5c96f868836f23e4018e3d8492b3a2350e3e923b04ddbab330bab88a47110c4110336ce28d9f797e29dbee37749114d156a19c38f89f9948422c10ada2b78ee350fc2e1f056a1419796e7453d9b02de933631e3e88aaa2b8ba989c8c5a519bdf44b23c934a1a33fa2a4a48873c202de5baed32cb2a05cd1c3c5848b69a5858f6432ae124b26ead55b631c916438aea

Now what i do notice, is that for some reason, my C# encrypted is always exactly double the length of the javascript output.
Maybe you guys have a better clue than me.

Thanks ahead of time!

Is This A Good Question/Topic? 0
  • +

Replies To: Duplicating a javascript's RSA encryption library in C#

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1212
  • View blog
  • Posts: 4,130
  • Joined: 27-January 10

Re: Duplicating a javascript's RSA encryption library in C#

Posted 27 January 2010 - 07:54 PM

Take a look at this tutorial:
http://www.dreaminco...snippet1224.htm

And also for a more in depth look take a look at this one as well:
http://www.dotnetspi...cryption-C.aspx


Something to note:
The strength of the encryption is determined by the size of the key used. So, The larger the key, the stronger the encryption.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1