17

I've seen this question asked before, though in these cases the poster wanted to encrypt something (usually a url) on a public facing website, and the responses were mostly "don't!". In my case, however, the JavaScript will be stored within a non-public internal system, so I think I have more leeway. One example of a similar question is: How to encrypt url in javascript and decrypt in c# - and the answers don't actually answer the question.

My 'JavaScript' is actually 'SuiteScript', which is defined as "SuiteScript is a JavaScript-based API that gives developers the ability to extend NetSuite", where NetSuite is a hosted CRM package, so any coding used to encrypt my string would be hidden to everyone, except for employees of my company (so considered safely hidden).

What I want to do is:

  1. generate a querystring (e.g. userid=guidValue&firstName=stringValue&company=stringValue&...),
  2. encrypt that using a secure method (such as AES256, RSA, whatever someone can suggest that's secure),
  3. call a webpage on my C# based website passing this string in the URL (e.g. mysite.com/mypage.aspx?encStr=encryptedString)
  4. have that C# page decrypt it, separate the name/value pairs and process them.

I've googled around and search stackoverflow, but not found any articles or answers that provide clear instructions of an encryption method that can be used by both technologies. Does anyone have such instructions?

5
  • 2
    Who are you trying to protect the information from? If it's an internal system, shouldn't everybody be implicitly trusted? If not, then the fact that it's non-public makes absolutely no difference. You can't encrypt at the web browser client without sending it all the code needed to figure out how to spoof the security.
    – Pointy
    Jul 26, 2011 at 13:28
  • 2
    If you do a form post over ssl then you shouldn't need to encrypt the message. Doing explicit encryption for that would be over kill.
    – Phil
    Jul 26, 2011 at 13:30
  • HTTPS isn't an option, as I don't have control over the hosted NetSuite server. I'm protecting the information from prying eyes when the string is sent to my C# based website, which is not internal. I don't need to send the code needed to figure out security spoofing. I thought that if a 'key' was used in the JavaScript and that same 'key' was available to the C# (without being sent) then decryption could be done
    – QMKevin
    Jul 26, 2011 at 13:34
  • Just to be clear, is your recieving website hosted on the NetSuite server?
    – Phil
    Jul 26, 2011 at 13:40
  • No, the receiving website is hosted elsewhere. I get your point, but HTTPS is still not an option in this case.
    – QMKevin
    Jul 26, 2011 at 14:10

1 Answer 1

19

Symmetrical

The simplest way is to use a library as the Stanford Javascript Crypto Library that implement a standard (AES in this case) and to use the same cipher in C# (via AesManaged or AesCryptoServiceProvider).

You'll get a symmetrical cipher but there nearly no more than one parameter (the key) to set for both libs to work.

Asymmetrical

You could also use an asymmetrical (Public-Key) cipher instead. An attacker getting it's hand on the javascript code would be able to send crafted requests to your server but won't be able to intercept other requests.

There is an implementation of RSA in javascript and the RSACryptoServiceProvider class provide the support in .Net

A full example is available in Code project including a path to the RSA in javascript lib to support padding (mandatory when using the .Net implementation)

Note

Both of theses solutions by themselves are vulnerable to replay (an attacker intercepting a string and sending it back later -- potentially multiple times -- to the server)

6
  • I'll look at this, but there's more investigation here than I was looking for.
    – QMKevin
    Jul 26, 2011 at 13:37
  • I'd definitely use an asymmetric cipher. Using a symmetrical one requires you to send the key to the client, which means the "prying eyes" could intercept that key and easily decrypt all traffic back and forth (defeating the purpose).
    – The Moof
    Jul 26, 2011 at 14:02
  • That makes sense, and sounds like something I'd like to do.. but how? I'm really looking for some instructions here, rather than avenues for further investigation. Not because I'm lazy, but because I'm finding this very trick to understand, and I learn so much better by doing rather than reading (i.e. I'm more of a 'give me a fish' person than a 'teach me to fish' - especially when time constraints are in effect, which they are)
    – QMKevin
    Jul 26, 2011 at 14:09
  • I added a link to a code project with sample code to do this (It's example is a little more complex but uses the RSA cipher between C# and javascript) Jul 26, 2011 at 14:21
  • Wonderful, Thank you! I'll review the code project and get back to you.
    – QMKevin
    Jul 26, 2011 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.