62 lines
No EOL
2.6 KiB
C#
62 lines
No EOL
2.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace WebsitePanel.WebDavPortal.Cryptography
|
|
{
|
|
public class Rfc2898Cryptography : ICryptography
|
|
{
|
|
public string PasswordHash { get; set; }
|
|
public string SaltKey { get; set; }
|
|
public string VIKey { get; set; }
|
|
|
|
public Rfc2898Cryptography(string passwordHash, string saltKey, string viKey)
|
|
{
|
|
PasswordHash = passwordHash;
|
|
SaltKey = saltKey;
|
|
VIKey = viKey;
|
|
}
|
|
|
|
public string Encrypt(string plainText)
|
|
{
|
|
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
|
|
|
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256/8);
|
|
var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC, Padding = PaddingMode.Zeros};
|
|
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
|
|
|
|
byte[] cipherTextBytes;
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
|
|
{
|
|
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
|
|
cryptoStream.FlushFinalBlock();
|
|
cipherTextBytes = memoryStream.ToArray();
|
|
cryptoStream.Close();
|
|
}
|
|
memoryStream.Close();
|
|
}
|
|
return Convert.ToBase64String(cipherTextBytes);
|
|
}
|
|
|
|
public string Decrypt(string encryptedText)
|
|
{
|
|
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
|
|
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256/8);
|
|
var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC, Padding = PaddingMode.None};
|
|
|
|
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
|
|
var memoryStream = new MemoryStream(cipherTextBytes);
|
|
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
|
|
var plainTextBytes = new byte[cipherTextBytes.Length];
|
|
|
|
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
|
|
memoryStream.Close();
|
|
cryptoStream.Close();
|
|
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
|
|
}
|
|
}
|
|
} |