Giter Site home page Giter Site logo

jocyscom / jsclasses Goto Github PK

View Code? Open in Web Editor NEW
56.0 15.0 21.0 9.66 MB

.NET converted into Object Oriented JavaScript Class Library

C# 2.91% Batchfile 0.31% HTML 8.50% CSS 9.52% Ruby 0.04% JavaScript 76.59% Shell 0.01% XSLT 0.55% ASP.NET 0.54% SCSS 1.04%

jsclasses's Introduction

Introduction

Welcome to Object Oriented JavaScript class library in C#/.NET style. This JavaScript library contains hashing (MD5, HMACMD5, SHA1, HMACSHA256, SHA256), encryption (AES, RSA) and some other JavaScript classes compatible with Microsoft .NET Framework. Examples for server are (Federal Information Processing Standard) FIPS-compliant. You can use these classes for end-to-end encryption between web clients.

It allows you to write some JavaScript code 100% identical to C#. For example, this is JavaScript code which converts unicode text to bytes and then creates Base64 string from it:

var text = "おはようございます – Good morning!";
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
var base64 = System.Convert.ToBase64String(bytes);
// base64: 44GK44Gv44KI44GG44GU44GW44GE44G+44GZIOKAkyBHb29kIG1vcm5pbmch

Download

https://github.com/JocysCom/JsClasses/archive/master.zip - 3.2 MB

Background

I like coding with JavaScript in object-oriented style. One day, I decided to 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.
  3. Started to use XML Comments inside JavaScript. They are not supported very well by Visual Studio as C# but I hope that support will be better in the future.

Documents

https://www.jocys.com/Common/JsClasses/Documents:

Screenshot

Benefits

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.:

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.

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.AES.js
// Turn input string into a byte array.
var input = System.Text.Encoding.UTF8.GetBytes("Plain Text");
// Create an instance of the AES class.
var cipher = new System.Security.Cryptography.AesCryptoServiceProvider();
// 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.
cryptoStream.FlushFinalBlock();
// Convert data from a memoryStream into a byte array.
var outputBuffer = stream.ToArray();
// Close both streams.
stream.Close();
cryptoStream.Close();
// 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>" +
        "<Modulus>pxtmFnrGI6Sb8ziyY+NRUDuQ4b/ETw5WabQ4daFQqzsCEr/6J/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>" +
            "<D>XZqFVrYy4qhECruJgVZFp/GVuD5Y0gev88nVjl5r911QT+I8vgJSklTso7jTlpMtf2oe7UZ0WRWEtgPS3tZn4Q==</D>" +
        "</RSAKeyValue>";
// ------------------------------------------------
// RSA Keys
// ------------------------------------------------
var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
// Import parameters from XML string.
rsa.FromXmlString(xmlParams);
// 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.
rsa.ImportParameters(rsaParamsPublic);
// 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.
rsa.ImportParameters(rsaParamsPrivate);
// 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.
    document.body.appendChild(toolBar.Node);
    // Create Bar.
    var bar = new System.Web.UI.Interface.Bar("MainBar", document, "Bar Title");
    toolBar.Add(bar);
    // Create first button.
    var firstButton = new System.Web.UI.Interface.Button("FirstButton", document);
    firstButton.SetText("First");
    firstButton.SetImage("Images/Icons/Options-16x16.png");
    firstButton.SetTitle("First Button");
    firstButton.customAction = firstButton_Click;
    bar.Add(firstButton);    
    // Create second button.
    var secondButton = new System.Web.UI.Interface.Button("SecondButton", document);
    secondButton.SetText("Second");
    secondButton.SetImage("Images/Icons/Trace.16x16.png");
    secondButton.SetTitle("Second Button");
    secondButton.customAction = secondButton_Click;
    bar.Add(secondButton);    
}

window.onload = Window_Load;

It will produce this interface on the web page:

Screenshot

JavaScript Types

JavaScript has a very limited number of types:

JavaScript Object typeof(Object)
Object 'object'
Array '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# Type JavaScript 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 References

In JavaScript there is no ref or out keyword, which indicates a value that is passed by reference. You have to use JavaScript Object type to replicate reference functionality. For example:

C# reference parameter example:

// Function with reference parameter.
void ChangeValue(ref int param1)
{
    param1 = 2;
}

// Set default value to 0.
var p = 0;
// Funcion will set 'p' to 2.
void ChangeValue(p);

JavaScript reference parameter example:

// Function with reference parameter.
function ChangeValue(param1)
{
    param1.Value = 2;
}

// Set default value to 0.
var p = { Value: 0 };
// Funcion will set 'p.Value' to 2.
void ChangeValue(p);

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

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

Screenshot

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.

Installation

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

jsclasses's People

Contributors

ejocys avatar yqrashawn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsclasses's Issues

JS files load order

As I can see, loading JS file with incorrect order will get crash by a big dinosaur.
For example, system.debug.js shall load first.
I have review all your codes and documentation, and your deleted file JocysComJavaScriptClasses.zip(I dont know why you deleted, you may update the link on documentation website)
Still not found some clue.

Using JsClasses with node.js

Today JsClasses are heavily coupled for browser use when referencing the window object in the basic System namespace.

How would you suggest to de-couple the classes from the browser?

I would love to contribute and wrap the classes in a modern JS standard.

The README suggestion for making more attractive

First, Your library is EPIC.

I think this library is worth more attention and more contributors.

May you add in README:

  • More attractive title and description. for example ".NET converted into JS", "Increasing inter-operability performances"
  • Add simplified code comparing C# and JS, then emphasize the both return is identical. Dont make it more than 3lines, or it might get ignored.
  • Make it a little bit over-hyped. (Dont look at me, Google, Microsoft, IBM all do this)

You can put Jocys.com bigger at the top, and middle size of documentation.

The use of eval statement in Javascript

This is a Phenomenal library, after running through static code analysis in sonarqube I'm seeing under the vulnerability tab some issues related to the use of eval statements which fall under owasp-a7 category. Could it be updated?

RSA Decryption infrequently returns empty array when C# would not

I am encountering an issue that is infrequently showing up when decrypting some bytes using the RSACryptoServiceProvider. In the vast majority of cases, the output is correct for other input data.

Example for an encrypted byte array that will cause the decryption to succeed in C# and to fail in JavaScript (C# code followed by JS code):

using (var rsa = new RSACryptoServiceProvider(1024))
{
    var rsaParameters = new RSAParameters
    {
        D = new byte[] {83, 2, 72, 41, 251, 127, 74, 7, 216, 204, 6, 47, 215, 147, 107, 94, 120, 146, 170, 101, 124, 157, 78, 54, 129, 219, 93, 31, 169, 110, 177, 206, 114, 43, 109, 64, 124, 190, 42, 23, 77, 36, 220, 207, 107, 232, 217, 111, 2, 46, 93, 60, 51, 100, 219, 55, 235, 160, 102, 11, 240, 115, 110, 12, 209, 32, 199, 123, 59, 223, 117, 80, 108, 187, 249, 222, 104, 96, 25, 196, 33, 46, 7, 2, 68, 193, 125, 167, 43, 200, 253, 150, 20, 151, 153, 224, 95, 54, 96, 222, 62, 154, 161, 205, 4, 35, 50, 106, 168, 104, 130, 136, 196, 206, 241, 213, 116, 197, 120, 61, 156, 166, 253, 253, 211, 32, 192, 37 },
        DP = new byte[] {140, 55, 185, 180, 167, 87, 181, 75, 29, 239, 103, 114, 171, 239, 159, 109, 97, 149, 194, 92, 155, 239, 145, 59, 17, 21, 125, 203, 18, 46, 30, 243, 203, 254, 132, 113, 241, 239, 69, 138, 234, 183, 223, 68, 103, 224, 132, 26, 195, 38, 10, 0, 254, 17, 26, 168, 134, 130, 126, 186, 233, 161, 75, 13},
        DQ = new byte[] {192, 93, 194, 73, 3, 66, 188, 80, 200, 178, 116, 150, 211, 157, 108, 91, 31, 35, 192, 20, 101, 144, 205, 222, 32, 58, 144, 121, 192, 107, 94, 52, 44, 156, 155, 12, 253, 85, 118, 111, 50, 221, 197, 131, 245, 173, 123, 119, 85, 133, 151, 182, 119, 91, 165, 234, 244, 146, 70, 160, 211, 134, 150, 183},
        Exponent = new byte[] {1, 0, 1},
        InverseQ = new byte[] {68, 124, 205, 56, 249, 77, 65, 126, 45, 165, 101, 65, 253, 121, 110, 174, 50, 108, 34, 215, 181, 146, 107, 192, 233, 189, 251, 120, 45, 112, 231, 182, 182, 135, 56, 112, 154, 83, 136, 31, 211, 63, 86, 254, 110, 120, 94, 174, 233, 157, 217, 202, 191, 16, 231, 255, 48, 195, 3, 161, 1, 64, 97, 69},
        P = new byte[] {208, 234, 226, 108, 191, 99, 90, 7, 113, 245, 148, 27, 208, 240, 224, 55, 129, 119, 171, 213, 51, 130, 33, 143, 223, 129, 17, 246, 193, 244, 40, 49, 83, 16, 186, 181, 153, 81, 131, 41, 79, 230, 234, 98, 209, 182, 123, 49, 187, 209, 48, 187, 56, 33, 3, 244, 153, 122, 240, 30, 151, 153, 247, 151},
        Q = new byte[] {231, 199, 50, 29, 21, 126, 75, 238, 165, 177, 106, 200, 57, 112, 108, 122, 74, 127, 150, 214, 195, 201, 64, 96, 0, 222, 134, 172, 69, 101, 209, 129, 213, 18, 50, 202, 95, 18, 188, 69, 164, 176, 117, 27, 6, 139, 74, 8, 61, 178, 110, 248, 72, 29, 4, 76, 195, 222, 249, 187, 63, 147, 194, 231},
        Modulus = new byte[] {189, 38, 129, 203, 191, 154, 166, 80, 184, 154, 185, 149, 93, 86, 109, 123, 9, 134, 117, 21, 152, 2, 41, 81, 6, 161, 75, 54, 147, 157, 204, 75, 177, 130, 166, 219, 54, 93, 37, 154, 10, 179, 76, 216, 40, 41, 216, 172, 163, 165, 171, 4, 210, 45, 175, 97, 214, 120, 150, 73, 89, 35, 39, 128, 157, 105, 31, 231, 175, 174, 129, 80, 168, 239, 228, 14, 207, 206, 184, 90, 124, 166, 165, 114, 184, 209, 13, 57, 71, 12, 23, 69, 107, 73, 213, 80, 2, 141, 239, 130, 21, 129, 102, 231, 237, 118, 170, 191, 108, 226, 231, 86, 92, 225, 184, 174, 72, 15, 20, 115, 74, 1, 134, 33, 206, 67, 215, 65},
    };

    rsa.ImportParameters(rsaParameters);

    var decrypted = rsa.Decrypt(new byte[]{43, 243, 196, 226, 169, 144, 254, 151, 45, 0, 216, 92, 167, 98, 18, 137, 20, 39, 234, 99, 147, 12, 255, 74, 157, 94, 46, 190, 169, 161, 150, 249, 138, 132, 212, 38, 252, 132, 7, 109, 80, 194, 251, 162, 60, 171, 156, 67, 157, 113, 142, 196, 225, 135, 27, 3, 139, 35, 72, 137, 246, 43, 145, 182, 197, 242, 253, 221, 66, 20, 96, 190, 161, 94, 228, 157, 146, 211, 108, 77, 172, 105, 110, 37, 87, 182, 126, 240, 16, 91, 178, 223, 116, 56, 135, 77, 111, 247, 187, 20, 223, 96, 187, 145, 18, 146, 37, 193, 71, 161, 71, 203, 16, 15, 17, 190, 121, 223, 233, 61, 126, 50, 217, 119, 135, 147, 56, 91}, true);
}

The variable decrypted will contain the following byte sequence:

36, 92, 119, 11, 33, 187, 194, 98, 6, 93, 234, 80, 122, 99, 158, 21, 82, 87, 94, 95, 198, 180, 250, 39, 84, 174, 162, 202, 217, 75, 150, 160

While running the follwing JavaScript code will result in an empty array.

let rsa = new System.Security.Cryptography.RSACryptoServiceProvider(1024);

let rsaParameters = new System.Security.Cryptography.RSAParameters();
rsaParameters.D = [83, 2, 72, 41, 251, 127, 74, 7, 216, 204, 6, 47, 215, 147, 107, 94, 120, 146, 170, 101, 124, 157, 78, 54, 129, 219, 93, 31, 169, 110, 177, 206, 114, 43, 109, 64, 124, 190, 42, 23, 77, 36, 220, 207, 107, 232, 217, 111, 2, 46, 93, 60, 51, 100, 219, 55, 235, 160, 102, 11, 240, 115, 110, 12, 209, 32, 199, 123, 59, 223, 117, 80, 108, 187, 249, 222, 104, 96, 25, 196, 33, 46, 7, 2, 68, 193, 125, 167, 43, 200, 253, 150, 20, 151, 153, 224, 95, 54, 96, 222, 62, 154, 161, 205, 4, 35, 50, 106, 168, 104, 130, 136, 196, 206, 241, 213, 116, 197, 120, 61, 156, 166, 253, 253, 211, 32, 192, 37 ];
rsaParameters.DP = [140, 55, 185, 180, 167, 87, 181, 75, 29, 239, 103, 114, 171, 239, 159, 109, 97, 149, 194, 92, 155, 239, 145, 59, 17, 21, 125, 203, 18, 46, 30, 243, 203, 254, 132, 113, 241, 239, 69, 138, 234, 183, 223, 68, 103, 224, 132, 26, 195, 38, 10, 0, 254, 17, 26, 168, 134, 130, 126, 186, 233, 161, 75, 13];
rsaParameters.DQ = [192, 93, 194, 73, 3, 66, 188, 80, 200, 178, 116, 150, 211, 157, 108, 91, 31, 35, 192, 20, 101, 144, 205, 222, 32, 58, 144, 121, 192, 107, 94, 52, 44, 156, 155, 12, 253, 85, 118, 111, 50, 221, 197, 131, 245, 173, 123, 119, 85, 133, 151, 182, 119, 91, 165, 234, 244, 146, 70, 160, 211, 134, 150, 183];
rsaParameters.Exponent = [1, 0, 1];
rsaParameters.InverseQ = [68, 124, 205, 56, 249, 77, 65, 126, 45, 165, 101, 65, 253, 121, 110, 174, 50, 108, 34, 215, 181, 146, 107, 192, 233, 189, 251, 120, 45, 112, 231, 182, 182, 135, 56, 112, 154, 83, 136, 31, 211, 63, 86, 254, 110, 120, 94, 174, 233, 157, 217, 202, 191, 16, 231, 255, 48, 195, 3, 161, 1, 64, 97, 69];
rsaParameters.P = [208, 234, 226, 108, 191, 99, 90, 7, 113, 245, 148, 27, 208, 240, 224, 55, 129, 119, 171, 213, 51, 130, 33, 143, 223, 129, 17, 246, 193, 244, 40, 49, 83, 16, 186, 181, 153, 81, 131, 41, 79, 230, 234, 98, 209, 182, 123, 49, 187, 209, 48, 187, 56, 33, 3, 244, 153, 122, 240, 30, 151, 153, 247, 151];
rsaParameters.Q = [231, 199, 50, 29, 21, 126, 75, 238, 165, 177, 106, 200, 57, 112, 108, 122, 74, 127, 150, 214, 195, 201, 64, 96, 0, 222, 134, 172, 69, 101, 209, 129, 213, 18, 50, 202, 95, 18, 188, 69, 164, 176, 117, 27, 6, 139, 74, 8, 61, 178, 110, 248, 72, 29, 4, 76, 195, 222, 249, 187, 63, 147, 194, 231];
rsaParameters.Modulus = [189, 38, 129, 203, 191, 154, 166, 80, 184, 154, 185, 149, 93, 86, 109, 123, 9, 134, 117, 21, 152, 2, 41, 81, 6, 161, 75, 54, 147, 157, 204, 75, 177, 130, 166, 219, 54, 93, 37, 154, 10, 179, 76, 216, 40, 41, 216, 172, 163, 165, 171, 4, 210, 45, 175, 97, 214, 120, 150, 73, 89, 35, 39, 128, 157, 105, 31, 231, 175, 174, 129, 80, 168, 239, 228, 14, 207, 206, 184, 90, 124, 166, 165, 114, 184, 209, 13, 57, 71, 12, 23, 69, 107, 73, 213, 80, 2, 141, 239, 130, 21, 129, 102, 231, 237, 118, 170, 191, 108, 226, 231, 86, 92, 225, 184, 174, 72, 15, 20, 115, 74, 1, 134, 33, 206, 67, 215, 65];

rsa.ImportParameters(rsaParameters);
let decrypted = rsa.Decrypt([43, 243, 196, 226, 169, 144, 254, 151, 45, 0, 216, 92, 167, 98, 18, 137, 20, 39, 234, 99, 147, 12, 255, 74, 157, 94, 46, 190, 169, 161, 150, 249, 138, 132, 212, 38, 252, 132, 7, 109, 80, 194, 251, 162, 60, 171, 156, 67, 157, 113, 142, 196, 225, 135, 27, 3, 139, 35, 72, 137, 246, 43, 145, 182, 197, 242, 253, 221, 66, 20, 96, 190, 161, 94, 228, 157, 146, 211, 108, 77, 172, 105, 110, 37, 87, 182, 126, 240, 16, 91, 178, 223, 116, 56, 135, 77, 111, 247, 187, 20, 223, 96, 187, 145, 18, 146, 37, 193, 71, 161, 71, 203, 16, 15, 17, 190, 121, 223, 233, 61, 126, 50, 217, 119, 135, 147, 56, 91], true);

console.log(decrypted);

But for instance, using the same RSA paramters but using the following encrypted byte array, we are getting the correct result in both programs:

var decrypted = rsa.Decrypt(new byte[]{55, 60, 151, 232, 112, 191, 217, 122, 81, 162, 169, 53, 115, 89, 192, 84, 138, 35, 69, 123, 78, 106, 190, 212, 251, 218, 88, 241, 184, 89, 4, 75, 133, 240, 121, 58, 40, 76, 190, 128, 141, 142, 30, 218, 170, 207, 3, 104, 107, 208, 0, 7, 227, 42, 186, 4, 17, 82, 139, 135, 127, 236, 185, 248, 136, 211, 185, 127, 169, 138, 165, 225, 113, 6, 31, 117, 123, 16, 91, 39, 103, 94, 113, 156, 42, 42, 167, 191, 179, 26, 7, 154, 88, 236, 113, 129, 93, 72, 97, 226, 205, 238, 207, 193, 200, 158, 192, 195, 73, 66, 6, 178, 213, 120, 238, 70, 219, 98, 66, 116, 177, 86, 84, 177, 183, 77, 239, 77});
let decrypted = rsa.Decrypt([55, 60, 151, 232, 112, 191, 217, 122, 81, 162, 169, 53, 115, 89, 192, 84, 138, 35, 69, 123, 78, 106, 190, 212, 251, 218, 88, 241, 184, 89, 4, 75, 133, 240, 121, 58, 40, 76, 190, 128, 141, 142, 30, 218, 170, 207, 3, 104, 107, 208, 0, 7, 227, 42, 186, 4, 17, 82, 139, 135, 127, 236, 185, 248, 136, 211, 185, 127, 169, 138, 165, 225, 113, 6, 31, 117, 123, 16, 91, 39, 103, 94, 113, 156, 42, 42, 167, 191, 179, 26, 7, 154, 88, 236, 113, 129, 93, 72, 97, 226, 205, 238, 207, 193, 200, 158, 192, 195, 73, 66, 6, 178, 213, 120, 238, 70, 219, 98, 66, 116, 177, 86, 84, 177, 183, 77, 239, 77], true);
console.log(decrypted);
12, 110, 218, 185, 192, 128, 248, 51, 241, 81, 54, 181, 114, 250, 222, 130, 249, 37, 163, 1, 70, 137, 181, 43, 119, 35, 50, 31, 117, 139, 74, 64

I'm assuming the issue is not on my end (correct me if I'm wrong).

Bug: System.Text.Debug

At line 719
bE = b1 << 18 | (b2 << 12)(b3 << 6) | b4;

Error: Uncaught (in promise): TypeError: (b2 << 12) is not a function
TypeError: (b2 << 12) is not a function

PaddingMode is undefined

I try to implement the library; the RSA encryption in a project, I copied the example and run the project but don´t work, It show me this error;

TypeError: System.Security.Cryptography.PaddingMode is undefined

This is the code that I used:

	function GetNewRsaProvider(dwKeySize) {
		// Create a new instance of RSACryptoServiceProvider.
		if (!dwKeySize) dwKeySize = 512;
		return new System.Security.Cryptography.RSACryptoServiceProvider(dwKeySize);
	}

	function GetRsaKey(includePrivateParameters) {
		var keyParams = xmlParamsDefault;
		// ------------------------------------------------
		// RSA Keys
		// ------------------------------------------------
		var rsa = GetNewRsaProvider();
		if (keyParams[0] === "<") {
			// Import parameters from xml.
			rsa.FromXmlString(keyParams);
		} else {
			var keyBlob = System.Convert.FromBase64String(keyParams);
			rsa.ImportCspBlob(keyBlob);
		}
		return rsa.ExportParameters(includePrivateParameters);
	}

    function fn_encriptar(Texto, Grifo) {
		var decryptedBytes = System.Text.Encoding.UTF8.GetBytes(Texto);
		var doOaepPadding = false;
		// ------------------------------------------------
		// Encrypt
		// ------------------------------------------------
		var rsa = GetNewRsaProvider();
		// Import the RSA Key information.
		rsa.ImportParameters(GetRsaKey(false));
		// Encrypt the passed byte array and specify OAEP padding.
		var encryptedBytes = rsa.Encrypt(decryptedBytes, doOaepPadding);
		var encryptedString = System.Convert.ToBase64String(encryptedBytes)
		// ------------------------------------------------
		// Display the encrypted data.
		//var encryptedString = System.BitConverter.ToString(encryptedBytes, "");
		return encryptedString;
    }

`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.