Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.IO;
  13. using System.Security.Cryptography;
  14.  
  15. namespace Rextester
  16. {
  17.     public class Program
  18.     {
  19.         public static void Main(string[] args)
  20.         {
  21.             //Your code goes here
  22.             Console.WriteLine(AESEncryption.Decrypt("1", "2", "3", "4")); //Пароль (шифруемый текст), пароль для шифрации(ключ), соль, вектор
  23.         }
  24.        
  25.         public static class AESEncryption
  26.         {
  27. #region Static Functions
  28.             public static string Decrypt(string CipherText, string Password,
  29.                 string Salt, string InitialVector,
  30.                 string HashAlgorithm = "SHA1", int PasswordIterations = 2,
  31.                 int KeySize = 256)
  32.             {
  33.                 //if (string.IsNullOrEmpty(CipherText))
  34.                   //  return "";
  35.                 byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
  36.                 byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
  37.                 byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
  38.                
  39.                 PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
  40.                 byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
  41.                 RijndaelManaged SymmetricKey = new RijndaelManaged();
  42.                 SymmetricKey.Mode = CipherMode.CBC;
  43.                 byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
  44.                 int ByteCount = 0;
  45.                 using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
  46.                 {
  47.                     using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
  48.                     {
  49.                         using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
  50.                         {
  51.  
  52.                             ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
  53.                             MemStream.Close();
  54.                             CryptoStream.Close();
  55.                         }
  56.                     }
  57.                 }
  58.                 SymmetricKey.Clear();
  59.                 return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
  60.             }
  61.  
  62.             #endregion
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement