using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Applications.Common.Utilities
{
public class Cryptography
{
private readonly byte[] _cryptVector = Encoding.ASCII.GetBytes("yolbutpddklczekf");
private readonly byte[] _cryptKey = Encoding.ASCII.GetBytes("ivcsfemmnqejpxcnkbccebpmzzsuilxv");
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public string EncryptString(string clearText)
{
using (MemoryStream ms = new MemoryStream())
{
byte[] clearTextBytes = Encoding.UTF8.GetBytes(clearText);
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
using (CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(_cryptKey, _cryptVector),
CryptoStreamMode.Write))
{
cs.Write(clearTextBytes, 0, clearTextBytes.Length);
}
return Convert.ToBase64String(ms.ToArray());
}
}
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public string DecryptString(string encryptedText)
{
using (MemoryStream ms = new MemoryStream())
{
byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText);
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
using (CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(_cryptKey, _cryptVector),
CryptoStreamMode.Write))
{
cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
}
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Applications.Common.Utilities
{
public class Cryptography
{
private readonly byte[] _cryptVector = Encoding.ASCII.GetBytes("yolbutpddklczekf");
private readonly byte[] _cryptKey = Encoding.ASCII.GetBytes("ivcsfemmnqejpxcnkbccebpmzzsuilxv");
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public string EncryptString(string clearText)
{
using (MemoryStream ms = new MemoryStream())
{
byte[] clearTextBytes = Encoding.UTF8.GetBytes(clearText);
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
using (CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(_cryptKey, _cryptVector),
CryptoStreamMode.Write))
{
cs.Write(clearTextBytes, 0, clearTextBytes.Length);
}
return Convert.ToBase64String(ms.ToArray());
}
}
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public string DecryptString(string encryptedText)
{
using (MemoryStream ms = new MemoryStream())
{
byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText);
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
using (CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(_cryptKey, _cryptVector),
CryptoStreamMode.Write))
{
cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
}
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
No comments:
Post a Comment