[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 ~