Как расшифровать данные в Node.js, зашифрованные с помощью AES в. NET? - PullRequest
1 голос
/ 28 января 2020

Как расшифровать данные в Node.js, зашифрованные с использованием AES. NET?

Существует две функции:

1) PasswordEncrypt: to шифровать данные

2) PasswordDecrypt: для расшифровки данных

Я пытаюсь расшифровать данные в Node.js, который зашифрован с помощью функции passwordEncrypt в. NET, я пытался перенести функцию но я не могу найти альтернативу для функции "new Rfc2898DeriveBytes ()", которая используется в обеих функциях ради цели cryptographi c.

Обратите внимание, что обе функции для шифрования и Расшифровка с использованием AES добавляется в код в NET, и они работают нормально.

Любая помощь будет оценена.

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace Rextester
{
    public class Program
    {             
        public static void Main(string[] args)
        {
            string EncryptionKey = "Suraj1234";
            string encrypted_data = "";
            string dencrypted_data = "{'blk_referenceType':'SURVEY','blk_referenceId':10458,'surveyTitle':'','userid':9780206,'role':'Management','FirstName':'Suraj','LastName':'Chandgude','EmailAddress':'','BuildingId':5616,'BuildingName':'H2O Road','OccupancyId':2331719,'UnitName':'Bldg Mgmt','IsMgmtUnit':true,'TimeStampUTC':'2020 - 01 - 24T10: 02:11.0700916Z','expirationdate':'0001 - 01 - 01T00: 00:00','language':'en - US'}";
            encrypted_data = passwordEncrypt(dencrypted_data, EncryptionKey);
            Console.WriteLine("Encrypt DATA - " + encrypted_data);
            dencrypted_data = passwordDecrypt(encrypted_data, EncryptionKey);
            Console.WriteLine("DECRYPTED DATA - " + dencrypted_data);
        }

         public static string passwordEncrypt(string inText, string key)
        {
            byte[] bytesBuff = Encoding.Unicode.GetBytes(inText);
            using (Aes aes = Aes.Create())
            {
                Rfc2898DeriveBytes crypto = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                aes.Key = crypto.GetBytes(32);
                aes.IV = crypto.GetBytes(16);
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cStream.Write(bytesBuff, 0, bytesBuff.Length);
                        cStream.Close();
                    }
                    inText = Convert.ToBase64String(mStream.ToArray());
                }
            }
            return inText;
        }

        public static string passwordDecrypt(string cryptTxt,string key)
        {
        cryptTxt = cryptTxt.Replace(" ", "+");
            cryptTxt = cryptTxt.Replace("%20", "+");
            cryptTxt = cryptTxt.Replace("%2F", "/");
            byte[] bytesBuff = Convert.FromBase64String(cryptTxt);
            using (Aes aes = Aes.Create())
            {
                Rfc2898DeriveBytes crypto = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                aes.Key = crypto.GetBytes(32);
                aes.IV = crypto.GetBytes(16);
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cStream.Write(bytesBuff, 0, bytesBuff.Length);
                        cStream.Close();
                    }
                    cryptTxt = Encoding.Unicode.GetString(mStream.ToArray());
                }
            }
            return cryptTxt;
        }
    }
}
...