You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
5.2 KiB
151 lines
5.2 KiB
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace DealerSelection.Api.CommonUtil
|
|
{
|
|
|
|
public static class AESEncryption
|
|
{
|
|
|
|
public static byte[] EncryptString(string plainText, byte[] key, byte[] iv)
|
|
{
|
|
// Instantiate a new Aes object to perform string symmetric encryption
|
|
Aes encryptor = Aes.Create();
|
|
|
|
encryptor.Mode = CipherMode.CBC;
|
|
|
|
// Set key and IV
|
|
byte[] aesKey = new byte[32];
|
|
Array.Copy(key, 0, aesKey, 0, 32);
|
|
encryptor.Key = aesKey;
|
|
encryptor.IV = iv;
|
|
|
|
// Instantiate a new MemoryStream object to contain the encrypted bytes
|
|
MemoryStream memoryStream = new MemoryStream();
|
|
|
|
// Instantiate a new encryptor from our Aes object
|
|
ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();
|
|
|
|
// Instantiate a new CryptoStream object to process the data and write it to the
|
|
// memory stream
|
|
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);
|
|
|
|
// Convert the plainText string into a byte array
|
|
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
|
|
|
|
// Encrypt the input plaintext string
|
|
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
|
|
|
|
// Complete the encryption process
|
|
cryptoStream.FlushFinalBlock();
|
|
|
|
// Convert the encrypted data from a MemoryStream to a byte array
|
|
byte[] cipherBytes = memoryStream.ToArray();
|
|
|
|
// Close both the MemoryStream and the CryptoStream
|
|
memoryStream.Close();
|
|
cryptoStream.Close();
|
|
|
|
// Convert the encrypted byte array to a base64 encoded string
|
|
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
|
|
|
|
// Return the encrypted data as a string
|
|
return cipherBytes;
|
|
}
|
|
|
|
public static string DecryptString(string cipherText, string key)
|
|
{
|
|
// Instantiate a new Aes object to perform string symmetric encryption
|
|
Aes encryptor = Aes.Create();
|
|
|
|
encryptor.Mode = CipherMode.CBC;
|
|
|
|
byte[] salt = Convert.FromBase64String(cipherText).Take(16).ToArray();
|
|
byte[] IVV = Convert.FromBase64String(cipherText).Skip(16).Take(16).ToArray();
|
|
|
|
|
|
byte[] aesKey;
|
|
using (var pbkdf2 = new Rfc2898DeriveBytes(key, salt, 65536, HashAlgorithmName.SHA256))
|
|
{
|
|
aesKey = pbkdf2.GetBytes(32);
|
|
}
|
|
|
|
// Set key and IV
|
|
//byte[] aesKey = new byte[32];
|
|
//Array.Copy(key, 0, aesKey, 0, 32);
|
|
encryptor.Key = aesKey;
|
|
encryptor.IV = IVV;
|
|
|
|
|
|
|
|
|
|
// Instantiate a new MemoryStream object to contain the encrypted bytes
|
|
MemoryStream memoryStream = new MemoryStream();
|
|
|
|
// Instantiate a new encryptor from our Aes object
|
|
ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();
|
|
|
|
// Instantiate a new CryptoStream object to process the data and write it to the
|
|
// memory stream
|
|
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);
|
|
|
|
// Will contain decrypted plaintext
|
|
string plainText = string.Empty;
|
|
|
|
try
|
|
{
|
|
// Convert the ciphertext string into a byte array
|
|
byte[] cipherBytes = Convert.FromBase64String(cipherText).Skip(32).ToArray();
|
|
|
|
// Decrypt the input ciphertext string
|
|
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
|
|
|
|
// Complete the decryption process
|
|
cryptoStream.FlushFinalBlock();
|
|
|
|
// Convert the decrypted data from a MemoryStream to a byte array
|
|
byte[] plainBytes = memoryStream.ToArray();
|
|
|
|
// Convert the decrypted byte array to string
|
|
plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
|
|
}
|
|
finally
|
|
{
|
|
// Close both the MemoryStream and the CryptoStream
|
|
memoryStream.Close();
|
|
cryptoStream.Close();
|
|
}
|
|
|
|
// Return the decrypted data as a string
|
|
return plainText;
|
|
}
|
|
|
|
public static dynamic ReturnEncKey(string password,string message)
|
|
{
|
|
|
|
byte[] salt = new byte[16];
|
|
byte[] encKey;
|
|
using (var rng = RandomNumberGenerator.Create())
|
|
{
|
|
rng.GetBytes(salt);
|
|
}
|
|
|
|
using (var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 65536, HashAlgorithmName.SHA256))
|
|
{
|
|
encKey = pbkdf2.GetBytes(32);
|
|
}
|
|
|
|
Random random = new Random();
|
|
|
|
byte[] iv = new byte[16];
|
|
random.NextBytes(iv);
|
|
byte[] encrypted = EncryptString(message, encKey, iv);
|
|
byte[] finalstring = salt.Concat(iv).Concat(encrypted).ToArray();
|
|
string encstring = Convert.ToBase64String(finalstring);
|
|
|
|
return encstring;
|
|
}
|
|
|
|
}
|
|
|
|
}
|