Расшифруйте столбец VARCHAR (макс.). Шифрование столбца выполняется в ASP. NET с использованием encryptionhelper. - PullRequest
0 голосов
/ 24 апреля 2020

Это класс, который шифрует поле суммы и сохраняет его в базе данных. Этот класс также расшифровывает сумму, чтобы показать ее в интерфейсе.

public static class EncryptionHelper
   {
       private const string EncryptionKey = @"+#q\l0OhcIeCu1eECp%{Nol&C+Z#_JgLsA0PbfK4+No9!IZDADSbN}j*U&}J7O%+";
       public static string Encrypt(decimal input)
       {
           string textData = input.ToString();
           byte[] clearBytes = Encoding.Unicode.GetBytes(textData);
           using (Aes encryptor = Aes.Create())
           {
               Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
               encryptor.Key = pdb.GetBytes(32);
               encryptor.IV = pdb.GetBytes(16);
               using (MemoryStream ms = new MemoryStream())
               {
                   using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                   {
                       cs.Write(clearBytes, 0, clearBytes.Length);
                       cs.Close();
                   }
                   textData = Convert.ToBase64String(ms.ToArray());
               }
           }
           return textData;
       }

       public static string Encrypt(string input)
       {
           string textData = input.ToString();
           byte[] clearBytes = Encoding.Unicode.GetBytes(textData);
           using (Aes encryptor = Aes.Create())
           {
               Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
               encryptor.Key = pdb.GetBytes(32);
               encryptor.IV = pdb.GetBytes(16);
               using (MemoryStream ms = new MemoryStream())
               {
                   using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                   {
                       cs.Write(clearBytes, 0, clearBytes.Length);
                       cs.Close();
                   }
                   textData = Convert.ToBase64String(ms.ToArray());
               }
           }
           return textData;
       }

       public static decimal Decrypt(string input)
       {
           if (!string.IsNullOrEmpty(input))
           {
               input = input.Replace(" ", "+");
               try
               {
                   byte[] cipherBytes = Convert.FromBase64String(input);
                   using (Aes encryptor = Aes.Create())
                   {
                       Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                       encryptor.Key = pdb.GetBytes(32);
                       encryptor.IV = pdb.GetBytes(16);
                       using (MemoryStream ms = new MemoryStream())
                       {
                           using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                           {
                               cs.Write(cipherBytes, 0, cipherBytes.Length);
                               cs.Close();
                           }
                           input = Encoding.Unicode.GetString(ms.ToArray());
                       }
                   }
                   return Convert.ToDecimal(input);
               }
               catch
               {
                   throw;
               }
           }
           else
           {
               return 0;
           }
       }

       public static object Decrypt(object input)
       {
           if (input.ToString() is string)
           {
               var str = Convert.ToString(input);
               str = str.Replace(" ", "+");
               try
               {
                   byte[] cipherBytes = Convert.FromBase64String(str);
                   using (Aes encryptor = Aes.Create())
                   {
                       Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                       encryptor.Key = pdb.GetBytes(32);
                       encryptor.IV = pdb.GetBytes(16);
                       using (MemoryStream ms = new MemoryStream())
                       {
                           using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                           {
                               cs.Write(cipherBytes, 0, cipherBytes.Length);
                               cs.Close();
                           }
                           str = Encoding.Unicode.GetString(ms.ToArray());
                       }
                   }

               }
               catch
               {
                   throw;
               }
               return str;
           }
           else
           {
               return string.Empty;
           }
       }
   }
} 

Теперь я хочу расшифровать поле «Сумма» с помощью запроса SQL. тип данных, в котором хранятся эти зашифрованные значения: VARCHAR(MAX).

this is the Amount column which decryption I want to do in SQL.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...