Click here to Skip to main content
13,146,402 members (49,009 online)
Click here to Skip to main content
Add your own
alternative version


330 bookmarked
Posted 12 Dec 2007

Object Oriented JavaScript Class Library in C#/.NET Style

, 9 Sep 2017
Rate this:
Please Sign up or sign in to vote.
JavaScript class library which partly replicates Microsoft .NET Framework structure and functionality.


Welcome to Object Oriented JavaScript class library in C#/.NET style.


I like coding with JavaScript in object oriented style. But one day, I decided to bring my code into another level and make my JavaScript code to look like C# as much as possible. So I did the following:

  1. Started to use .NET coding standards on my JavaScripts. You can find them on MSDN - .NET Framework: Guidelines for Names.
  2. Ported some useful classes and methods from .NET to JavaScript with the same class and property names. Some code parts were written from scratch, some parts were borrowed from the Internet and some parts were ported from C# directly. This JavaScript library also contains AES and RSA algorithm classes compatible with Microsoft .NET Framework "System.Security.Cryptography" classes.
  3. Started to use XML Comments inside JavaScript. They are not supported very well by Visual Studio 2008 but I hope support will be better in the future.

Screenshot - JocysComClassBrowser.jpg


Coding with JavaScript in C# .NET style provides these benefits:

  1. Any C# developer instantly understands the purpose of JavaScript code.
  2. You don't need to write help for your new JavaScript classes because Microsoft did it already.
  3. When porting a new class from C# to JavaScript, you don't need to think about how to name it or where to put it. All you need is to look for the same class in C# and use the same naming.
  4. More JavaScript classes you have, the easier it will be to port new ones.
  5. By using C# classes as primary reference, it will be much easier for different developers to write and integrate JavaScript classes into one big file library. It is because by looking at some C# class, the developer knows what type of input and output function must support/produce and he doesn't need to coordinate this with other developers.
  6. And many more...

Example: Hash Algorithm - HMAC-MD5 Checksum

C# (3.0) code to create HMAC-MD5 checksum:

// Create HMAC-MD5 Algorithm.
var hmac = new System.Security.Cryptography.HMACMD5();  
// Convert string to array of bytes.
var key = System.Text.Encoding.UTF8.GetBytes("test key");  
var data = System.Text.Encoding.UTF8.GetBytes("test data");  
// Compute hash.  
var hashBytes = hmac.ComputeHash(key, data);
// Convert to HEX string.
var hex = System.BitConverter.ToString(hashBytes);
// Convert to GUID so you can store it inside database.
var guid = new System.Guid(hashBytes);

HMAC-MD5 checksum code written with this JavaScript library:

Include JavaScripts:

  • System.js
  • System.BitConverter.js
  • System.Text.js
  • System.Security.Cryptography.MD5.js
  • System.Security.Cryptography.HMACMD5.js
// Create HMAC-MD5 Algorithm.
var hmac = new System.Security.Cryptography.HMACMD5();  
// Convert string to array of bytes.
var key = System.Text.Encoding.UTF8.GetBytes("test key");  
var data = System.Text.Encoding.UTF8.GetBytes("test data");  
// Compute hash.  
var hashBytes = hmac.ComputeHash(key, data);
// Convert to HEX string.
var hex = System.BitConverter.ToString(hashBytes);
// Convert to GUID so you can store it inside database.
var guid = new System.Guid(hashBytes);

As you can see, the code is 100% identical. SHA-1 and SHA-256 hash algorithm class is also available:

Example: Symmetric algorithm - AES-256 Encryption

Online examples:

JavaScript code for AES-256 encryption is identical to C# code except for only one minor difference. In JavaScript, I need to use "new System.Byte(length)" (line 20) instead of simple "new byte[length]". Of course, I can create a class alias by doing "byte = System.Byte" inside JavaScript and make that difference smaller. You can use "System.Security.Cryptography.AesCryptoServiceProvider" .NET class, instead of "RijndaelManaged" class, to decrypt/encrypt data on computers where Federal Information Processing Standard (FIPS) is enforced.

Include JavaScripts:

  • System.js
  • System.IO.js
  • System.Text.js
  • System.Convert.js
  • System.BitConverter.js
  • System.Security.Cryptography.js
  • System.Security.Cryptography.SHA1.js
  • System.Security.Cryptography.HMACSHA1.js
  • System.Security.Cryptography.RijndaelManaged.js
// Turn input string into a byte array.
var input = System.Text.Encoding.UTF8.GetBytes("Plain Text");
// Create an instance of the Rijndael class.
var cipher = new System.Security.Cryptography.RijndaelManaged();
// Calculate salt to make it harder to guess key by using a dictionary attack.
var passwordBytes = System.Text.Encoding.UTF8.GetBytes("password");
var hmac = new System.Security.Cryptography.HMACSHA1(passwordBytes);
var salt = hmac.ComputeHash(passwordBytes);
// Generate Secret Key from the password and salt.
// Note: Set number of iterations to 10 in order for JavaScript example to work faster.
var secretKey = 
	new System.Security.Cryptography.Rfc2898DeriveBytes(passwordBytes, salt, 10);
// Create a encryptor from the existing SecretKey bytes by using
// 32 bytes (256 bits) for the secret key and
// 16 bytes (128 bits) for the initialization vector (IV).
var key = secretKey.GetBytes(32);
var iv = secretKey.GetBytes(16);
// Get cryptor as System.Security.Cryptography.ICryptoTransform class.
var cryptor = cipher.CreateEncryptor(key, iv);
// Create new Input.
var inputBuffer = new System.Byte(input.length);
// Copy data bytes to input buffer.
System.Buffer.BlockCopy(input, 0, inputBuffer, 0, inputBuffer.length);
// Create a MemoryStream to hold the output bytes.
var stream = new System.IO.MemoryStream();
// Create a CryptoStream through which we are going to be processing our data.
var mode = System.Security.Cryptography.CryptoStreamMode.Write;
var cryptoStream = new System.Security.Cryptography.CryptoStream(stream, cryptor, mode);
// Start the crypting process.
cryptoStream.Write(inputBuffer, 0, inputBuffer.length);
// Finish crypting.
// Convert data from a memoryStream into a byte array.
var outputBuffer = stream.ToArray();
// Close both streams.
// Convert encrypted data into a base64-encoded string.
var base64String = System.Convert.ToBase64String(outputBuffer);
// base64String = laFf3eKu9tzB2XksJjd8EVM3PA9O30wz0Y+X3nyelW4=

Example: Asymmetric algorithm - RSA Encryption

RSA JavaScript classes are compatible with Microsoft .NET Framework. It means that you can encrypt/decrypt data with JavaScript and encrypt/decrypt it with "System.Security.Cryptography.RSACryptoServiceProvider" Microsoft .NET Framework class. You can export, import, generate RSA Keys and use "Direct Encryption (PKCS#1 v1.5)" and "OAEP padding (PKCS#1 v2)" padding.

RSA allows you to encrypt and submit data securely without Secure Sockets Layer (SSL). You can check System.Security.Cryptography.RSA.aspx example. This is done in 3 simple steps:

  • Step 1: Server generates RSA key and shows only public key to the user on the web page.
    Note: Private RSA key is stored on server side and only server can decrypt submitted data.
  • Step 2: User enters password or other sensitive data into textbox.
  • Step 3: User hits [Submit] button. JavaScript will encrypt password with RSA public key, replace plain text with encrypted Base64 code and submit web form to the server.
    Note: Then you can use strong password and AES symmetric encryption to submit extra data. In this way, you will protect sensitive data with military grade encryption.

Online examples:

Include JavaScripts:

  • System.js
  • System.IO.js
  • System.Text.js
  • System.Convert.js
  • System.BitConverter.js
  • System.Security.Cryptography.js
  • System.Security.Cryptography.SHA1.js
  • System.Security.Cryptography.HMACSHA1.js
  • System.Security.Cryptography.RSA.js

C# and JavaScript code for RSA encryption/decryption is 100% identical:

// Text to encrypt and decrypt.
var text = "plain text";
// Use OAEP padding (PKCS#1 v2).
var doOaepPadding = true;
// RSA 512-bit key: Public (Modulus), Private (D) and CRT (P, Q, DP, DQ, InverseQ).
var xmlParams =
	"<RSAKeyValue>" +
			LLBU/2D5mO5/Wu5U/Rya1E55aYFZeaZMNqAw==</Modulus>" +
		"<Exponent>AQAB</Exponent>" +
		"<P>2TsVXWPEvDIJv/gd2rX9k0UOyXuaYgoAchIH6vUicis=</P>" +
		"<Q>xO4+OYREQfqYRQK7y73+RaUG0IxobT0OQ0c+Ok2hc4k=</Q>" +
		"<DP>K7/xgpiIU9rECeyfnp/OjS14V+3T3vDivBaTj6eFI3c=</DP>" +
		"<DQ>K4N9ClZ4gp+tn6oP9t//XEIvtEsiE+kmyqTmUhmvMAk=</DQ>" +
		"<InverseQ>p7o4BOlKZQZ693R1ViZ66y5gTjUkNNTd2za7/1YGBCs=</InverseQ>" +
			I8vgJSklTso7jTlpMtf2oe7UZ0WRWEtgPS3tZn4Q==</D>" +
// ------------------------------------------------
// RSA Keys
// ------------------------------------------------
var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
// Import parameters from XML string.
// Export RSA key to RSAParameters and include:
//    false - Only public key required for encryption.
//    true  - Private key required for decryption.
// Export parameters and include only Public Key (Modulus + Exponent) 
// required for encryption.
var rsaParamsPublic = rsa.ExportParameters(false);
// Export Public Key (Modulus + Exponent) and include Private Key (D) 
// required for decryption.
var rsaParamsPrivate = rsa.ExportParameters(true);
// ------------------------------------------------
// Encrypt
// ------------------------------------------------
var decryptedBytes = System.Text.Encoding.UTF8.GetBytes(text);
// Create a new instance of RSACryptoServiceProvider.
rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
// Import the RSA Key information.
// Encrypt byte array.
var encryptedBytes = rsa.Encrypt(decryptedBytes, doOaepPadding);
// Convert bytes to base64 string.
var encryptedString = System.Convert.ToBase64String(encryptedBytes);
// ------------------------------------------------
// Decrypt
// ------------------------------------------------
// Convert base64 string back to bytes.
encryptedBytes = System.Convert.FromBase64String(encryptedString);
// Create a new instance of RSACryptoServiceProvider.
rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
// Import the RSA Key information.
// Decrypt byte array.
decryptedBytes = rsa.Decrypt(encryptedBytes, doOaepPadding);
// Get decrypted data.
text = System.Text.Encoding.UTF8.GetString(decryptedBytes);
// ------------------------------------------------
// Generate new RSA Key pair
// ------------------------------------------------
// Specify RSA key size.
var keySize = 512;
// Create a new instance of RSACryptoServiceProvider.
rsa = new System.Security.Cryptography.RSACryptoServiceProvider(keySize);
// Export the RSA Key information as XML string.
//    false - Only public key required for encryption.
//    true  - Private key required for decryption.
xmlParams = rsa.ToXmlString(true);

Example: Mobile Apps, Encryption and Compression

RSA encryption is very useful for mobile devices (jQuery Mobile). You can store sensitive information which belongs to the client (like credit cards, addresses, passwords, etc.) in encrypted state on device itself. Data can be decrypted only by submitting it to your server where RSA private decryption key is stored. Data is safe even if client device or server disk is lost. Client can't decrypt the data and no sensitive information is stored on the server. Compression can be used to reduce client data bill.

Online examples:

Example: User Interface

Library contains some user interface classes.

function firstButton_Click(){
    Trace.Write("First Button Click");

function secondButton_Click(){
    Trace.Write("Second Button Click");

function Window_Load(){
    Trace.IsEnabled = true;
    Trace.Write("Start Demo");
    // Create toolbar.
    var toolBar = new System.Web.UI.Interface.ToolBar("MyToolBar");
    // Add toolbar to document.
    // Create Bar.
    var bar = new System.Web.UI.Interface.Bar("MainBar", document, "Bar Title");
    // Create first button.
    var firstButton = new System.Web.UI.Interface.Button("FirstButton", document);
    firstButton.SetTitle("First Button");
    firstButton.customAction = firstButton_Click;
    // Create second button.
    var secondButton = new System.Web.UI.Interface.Button("SecondButton", document);
    secondButton.SetTitle("Second Button");
    secondButton.customAction = secondButton_Click;

window.onload = Window_Load;

It will produce this interface on the web page:



JavaScript has a very limited number of types:

JavaScript Objecttypeof(Object)
Function 'function'
String 'string'
Number 'number'
Boolean 'boolean'
null 'object'
undefined 'undefined'

But by combining the existing types, we can create JavaScript objects similar to C#. For example:

C# TypeJavaScript Type
public property declared with "this." prefix: this.Name = new String;
private property declared with "var" prefix: var name = new String;
class this.[ClassName] = function(){... without "return value;" at the end
void function which has no "return value;" at the end
short/Int16 whole Number from [-2^15, 2^15-1] range
int/Int32 whole Number from [-2^31, 2^31-1] range
long/Int64 whole Number from [-2^63, 2^63-1] range (Requires BigInteger class)
byte whole Number from [0, 255] range: var b = 14;
sbyte whole Number from [-128, 127] range: var sb = -14;
bytes[]Array() filled with integers from [0-255] range.
bit Number: 0 or 1
bit[]Array() filled with integers from [0-1] range.
char String which contains a single character. Declared with single quotes: var c = 's'
char[] Array() filled single characters:
var chars = new Array(1); chars[0] = 's';
object parameter which was declared with "{ }": var o = {};
enum Object with "Enum" suffix and comma separated values: this.TriStateEnum = { Unknown: -2, False: 0, True: 1 }
EventHandler function with parameters "sender" and "e": function(sender, e) or this.Click(sender, e)

NUMBERS: All numbers in JavaScript are 64-bit (8 bytes) floating point numbers (double: 1-bit sign, 11-bits exponent, 52-bits mantissa). There is no Double, Single/Float, Boolean, Int16, UInt16, Int32 or UInt32. But you can use public static methods of System.BitConverter JavaScript class in order to treat the same JavaScript number as a different type:

// Convert number to [0x00, 0x00, 0xCC, 0xCC] array.
var bytes = System.BitConverter.GetBytes(-859045888, System.TypeCode.Int32);
// Convert bytes back to -859045888.
var n = System.BitConverter.ToInt32(bytes, 0);  

System.BitConverter JavaScript class supports little-endian (default), big-endian byte orders and numeric arrays. System.BitConverter class is very useful in encoding/decoding/encryption/decryption classes. Please note that you need to specify number type when using GetBytes(value, typeCode) method by using System.TypeCode enumeration values (this enumeration is located inside System.js file).

I've added System.BigInt class (same as .NET internal System.Security.Cryptography.BigInt class). It represents an arbitrarily large signed integer whose value in theory has no upper or lower bounds. It means you can add, subtract, multiply, divide numbers of Godzilla proportions in JavaScript which can be useful with client side encryption:

// Increase global System.BigInt size to 512 bytes.
// BigInt will act like System.Int4096 (default is System.Int1024).
System.BigInt.MaxBytes = 512;
// Create big integer from hexadecimal number.
var n1 = new System.BigInt("0x010203040506");
// Create big integer from decimal number.
var n2 = new System.BigInt("-280422911905295");
// Multiply them.
var n3 = System.BigInt.Multiply(n2, n1);
// Store result in various forms.
var h = n3.ToHex()     // -0x01010C234779B3FAEED09F5A
var d = n3.ToDecimal() // -310751254825142252681076570
var bytes = n3.Elements // A6602F11054C86B8DCF3FEFEFFFFFF...   

NOTE: You can use <param type="byte[]" name="data">...</param> inside JavaScript XML Comments in order to specify type of input data and <returns type="double">...</returns> - for output.

JavaScript IntelliSense

Visual Studio 2010 has built-in support for JavaScript IntelliSense. This means that if you open file, place cursor at the end of file and type "System." then straight after the dot, Visual Studio will bring up a menu containing all available properties of System namespace:

Screenshot - JocysComJavaScriptIntelliSense.png






System.Type.Inherits method allows to use IntelliSense from inside of inherited class:

Screenshot - JocysComJavaScriptInherits.png






The good news here is that Microsoft is moving in the right direction. The bad news is that JavaScript IntelliSense works only with specific JavaScript coding style and sometimes needs workarounds. In other words, it works in mysterious ways or doesn't work at all :). Some upgrades are needed on my code too.




Extract source archive into webroot (/) folder of your website.

Example: System.Security.Password

Inside the source code, you can find examples (Examples/) including password generator example. You can run it:

Screenshot - SystemSecurityPasswordGeneratorGui.gif

To make a suggestion or report bugs, please write to


  • 2017-09-08 - Fixed bug inside System.Web.debug.js class, updated security and examples.
    • Updated examples to be FIPS-compliant:
      • System.Security.Cryptography.AES.aspx
      • System.Security.Cryptography.AES.aspx.cs
      • System.Security.Cryptography.aspx
      • System.Security.Cryptography.aspx.cs
      • System.Web.Mobile.htm
  • 2017-05-25 - Refactored some code. Fixed issue with TraceLog time format.
    • New Classes:
      • System.Security.Cryptography.SHA256
      • System.Security.Cryptography.HMACSHA256
    • New Examples:
      • System.Security.Cryptography.SHA256.htm
  • 2017-05-21 - Fixed compatibility issues with ASP.NET AJAX Control Toolkit. Resolved hundreds of errors and warnings, reported by Visual Studio ESLint. TraceLog is using DIV instead of IFRAME now.
  • 2011-08-29 - GZip compression classes and encryption example with web service for mobile application was added:
    • New Classes:
      • System.IO.Compression.DeflateInput
      • System.IO.Compression.HuffmanTree
      • System.IO.Compression.FastEncoderWindow
      • System.IO.Compression.FastEncoder
      • System.IO.Compression.Deflater
      • System.IO.Compression.Inflater
      • Etc...
    • New Examples:
      • System.Web.Mobile.htm
      • System.IO.Compression.htm
      • System.IO.Compression.aspx
  • 2011-07-27 - Intellise RSA encryption bug fix. JavaScript IntelliSense "type icon" support added. Other various changes. Project file updated to Visual Studio 2010. Fixed code to work on Google Chrome. You must start Chrome with --allow-file-access-from-files option in order for demo to run locally (with the file:/// in the URL).
    • New Classes:
      • System.IO.Stream
      • System.Diagnostics.TraceListener
  • 2010-02-24 - RSA encryption and Unit Testing classes were added:
    • New Classes:
      • System.Security.Cryptography.RSAParameters
      • System.Security.Cryptography.RSACryptoServiceProvider
      • System.Security.Cryptography.PKCS1Padding
      • System.Security.Cryptography.PKCS1MaskGenerationMethod
      • System.Security.Cryptography.HashAlgorithm
      • System.TestTools.UnitTesting.Assert
    • New Examples:
      • System.Security.Cryptography.RSA.aspx
      • System.Security.Cryptography.RSA.htm
      • System.TestTools.UnitTesting.htm
  • 2009-06-21 - New System.BigInt(.js file) class with examples:
    • Examples/System.BigInt.htm
    • Examples/System.BigInt.aspx
  • 2009-03-14 - PassGen now works in Internet Explorer again. Some cross-domain security issues with FX fixed. Adjusted to work better with "Microsoft AJAX Framework" scripts (Resource of System.Web.Extensions.dll/MicrosoftAjax.debug.js). Microsoft has some cool JavaScript classes there.
  • 2009-02-04 - New fixes and new bugs. Moved everything to /Common/JsClasses folder. Latest source available from SVN server (see above).
  • 2008-03-14 - Some bug fixes:
    • New Classes:
      • System.Web.UI.HtmlControls.TextBox.CommandLine - Converts input to command line. Will be used later to create plain JavaScript console and in chat applications.
    • Examples Updated (Examples/ folder):
      • System.Web.UI.HtmlControls.TextBox.CommandLine.htm
      • System.Web.UI.ShortKeys/System.Web.UI.ShortKeys.htm - Shows how to override and use custom windows keyboard layout on the web page. Can also capture keys and perform non-default custom actions.
  • 2008-01-27
    • New enumerations:
      • System.TypeCode
    • New classes:
      • System.Collections.BitArray
      • System.BitConverter (System.BitConverter.js) class was updated with methods:
      • System.BitConverter.GetByte(value, typeCode) - get bytes from Double, Single, Boolean, Int16, UInt16, Int32, UInt32. You need to use values of System.TypeCode to specify how JavaScript must treat a number.
        You also can convert byte[] back to numbers by using methods
        • System.BitConverter.ToDouble(bytes, startIndex)
        • System.BitConverter.ToSingle(bytes, startIndex)
        • System.BitConverter.ToBoolean(bytes, startIndex)
        • System.BitConverter.ToInt16(bytes, startIndex)
        • System.BitConverter.ToUInt16(bytes, startIndex)
        • System.BitConverter.ToInt32(bytes, startIndex)
        • System.BitConverter.ToUInt32(bytes, startIndex)
      • Int64 and UInt64 types are not available yet because JavaScript 64-bit (8 bytes) float point numbers have only 52-bit mantissa which means that it is not possible to work with 64-bit whole numbers properly (possible by reusing BigInt class).
    • Updated Examples:
      • System.BitConverter.htm
      • System.BitConverter.aspx
  • 2008-01-13 - Some bugs were fixed and new JavaScript classes were added:
    • System.Security.Cryptography.js file:
      • System.Security.Cryptography.Rfc2898DeriveBytes
      • System.Security.Cryptography.ICryptoTransform
      • System.Security.Cryptography.RNGCryptoServiceProvider
      • System.Security.Cryptography.CryptoStrea
    • System.Security.Cryptography.RijndaelManaged.js file (AES encryption):
      • System.Security.Cryptography.RijndaelManaged
    • System.js file (new methods):
      • System.Buffer.BlockCopy
      • System.Array.Reverse
      • System.Array.Clear
      • System.Array.GetMultiDimensional
      • System.Array.FillMultiDimensiona
    • Examples Created (Examples/ folder):
      • System.Security.Cryptography.AES.htm
      • System.Security.Cryptography.AES.aspx
      • System.Security.Cryptography.htm
      • System.Security.Cryptography.aspx
    • Some interface parts/classes/examples were updated to Office 2007 style:
      • System.Web.UI.Interface.ToolBar.htm
      • System.Web.UI.Interface.TabStrip.htm
    • Methods System.BitConverter static class was updated.
  • 2007-12-15 - New 'System.IO.MemoryStream' class. I will use it later with symmetric encryption algorithms so encryption can be done between two web browser clients without any ActiveX.
    Now you can get byte[] arrays filled with 0 numbers in JavaScript easier:
    var bytes = new System.Byte(10);
    It's the same as 'byte[] bytes = new System.Byte[10];' or 'byte[] bytes = new byte[10];' in C#. You can define multi-dimensional arrays filled with zeroes too:
    var bytes = new System.Byte(16,16); bytes[4][5] = 10;
  • 2007-12-12 - Created. Some links inside examples were fixed. New source code was uploaded. Alex's Code Syntax Highlighter was updated to 1.5.1 version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionExcellent Pin
itiwant26-Jul-11 16:58
memberitiwant26-Jul-11 16:58 
AnswerRe: Excellent [modified] Pin
EJocys26-Jul-11 21:19
memberEJocys26-Jul-11 21:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 9 Sep 2017
Article Copyright 2007 by EJocys
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid