[Not afraid of pitfalls] Node. js encrypts C # for decryption,

Source: Internet
Author: User

[Not afraid of pitfalls] Node. js encrypts C # for decryption,

I do not know much about AES encryption and decryption. To solve Node. js encryption, but cannot decrypt C #, I searched a lot of related articles on the Internet.

But most of them are Node. js vs Java or Java vs C # Two-way encryption and decryption code, but Node. js vs C # is not available #.

Then, through repeated experiments, we found a solution.

I'm not talking about it anymore. I'm not afraid of code. Let's rush with me!

 

Node. js Encryption

Var crypto = require ('crypto '); var secretKey = 'Password'; var aesEncrypt = function (data) {var cipher = crypto. createCipher ('aes-128-ecb ', secretKey); return cipher. update (data, 'utf8', 'hex') + cipher. final ('hex');} console. log (aesEncrypt ('Hello world! '); // Output c552d8545e864fd8f8b73e442cca9276. Note that this is a 16-Bit String.

C # decryption

Static void Main (string [] args) {Console. WriteLine (Decrypt ("c552d8545e864fd8f8b73e442cca9276"); // output hello world !} Private static string Decrypt (string toDecrypt) {byte [] keyArray = get_key (); // Pit 1: MD5 conversion byte [] toEncryptArray = HexToByte (toDecrypt ); // pitfall 2: A 16-bit string needs to be converted into a byte array RijndaelManaged rDel = new RijndaelManaged (); rDel. key = keyArray; // rDel. keySize = 128; // Pit 3: rDel. the KeySize is already 128, but after this sentence is added, it will not be decrypted correctly // rDel. blockSize = 128; // Pit 3: rDel. blockSize is already 128, but after this sentence is added, it will not decrypt rDel correctly. mode = CipherMode. ECB; rDel. padding = PaddingMode. PKCS7; ICryptoTransform cTransform = rDel. createDecryptor (); byte [] resultArray = cTransform. transformFinalBlock (toEncryptArray, 0, toEncryptArray. length); return UTF8Encoding. UTF8.GetString (resultArray);} private static byte [] get_key () {string key = "password"; byte [] result = Encoding. UTF8.GetBytes (key); MD5 md5 = new MD5CryptoServiceProvider (); return md5.ComputeHash (result);} private static byte [] HexToByte (string msg) {// msg = msg. replace ("", ""); // remove the space byte [] comBuffer = new byte [msg. length/2]; for (int I = 0; I <msg. length; I + = 2) {comBuffer [I/2] = (byte) Convert. toByte (msg. substring (I, 2), 16);} return comBuffer ;}

 

C # encryption, node. js decryption, to be continued ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.