Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Security.Cryptography;
- namespace Rextester
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- //Your code goes here
- Console.WriteLine(AESEncryption.Decrypt("1", "2", "3", "4")); //Пароль (шифруемый текст), пароль для шифрации(ключ), соль, вектор
- }
- public static class AESEncryption
- {
- #region Static Functions
- public static string Decrypt(string CipherText, string Password,
- string Salt, string InitialVector,
- string HashAlgorithm = "SHA1", int PasswordIterations = 2,
- int KeySize = 256)
- {
- //if (string.IsNullOrEmpty(CipherText))
- // return "";
- byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
- byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
- byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
- PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
- byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
- RijndaelManaged SymmetricKey = new RijndaelManaged();
- SymmetricKey.Mode = CipherMode.CBC;
- byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
- int ByteCount = 0;
- using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
- {
- using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
- {
- using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
- {
- ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
- MemStream.Close();
- CryptoStream.Close();
- }
- }
- }
- SymmetricKey.Clear();
- return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
- }
- #endregion
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement