70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
public class Cryptor
|
|
{
|
|
private const string ENCRYPTION_KEY = "721e51s73er2i3ds50a41exe123abce9";
|
|
|
|
public static string Hash(string value)
|
|
{
|
|
return GetSha1Hash(value);
|
|
}
|
|
|
|
public static string Encrypt(string value)
|
|
{
|
|
if (value == null || value.Length < 1)
|
|
{
|
|
return "";
|
|
}
|
|
return GetEncryptedString(value);
|
|
}
|
|
|
|
public static string Decrypt(string value)
|
|
{
|
|
if (value == null || value.Length < 1)
|
|
{
|
|
return "";
|
|
}
|
|
return GetDecryptedString(value);
|
|
}
|
|
|
|
private static string GetSha1Hash(string strToEncrypt)
|
|
{
|
|
byte[] bytes = new UTF8Encoding().GetBytes(strToEncrypt);
|
|
byte[] array = new SHA1CryptoServiceProvider().ComputeHash(bytes);
|
|
string text = "";
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
text += Convert.ToString(array[i], 16).PadLeft(2, '0');
|
|
}
|
|
return text.PadLeft(32, '0');
|
|
}
|
|
|
|
private static string GetEncryptedString(string toEncrypt)
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes("721e51s73er2i3ds50a41exe123abce9");
|
|
byte[] bytes2 = Encoding.UTF8.GetBytes(toEncrypt);
|
|
byte[] array = new RijndaelManaged
|
|
{
|
|
Key = bytes,
|
|
Mode = CipherMode.ECB,
|
|
Padding = PaddingMode.PKCS7
|
|
}.CreateEncryptor().TransformFinalBlock(bytes2, 0, bytes2.Length);
|
|
return Convert.ToBase64String(array, 0, array.Length);
|
|
}
|
|
|
|
private static string GetDecryptedString(string toDecrypt)
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes("721e51s73er2i3ds50a41exe123abce9");
|
|
byte[] array = Convert.FromBase64String(toDecrypt);
|
|
byte[] bytes2 = new RijndaelManaged
|
|
{
|
|
Key = bytes,
|
|
Mode = CipherMode.ECB,
|
|
Padding = PaddingMode.PKCS7
|
|
}.CreateDecryptor().TransformFinalBlock(array, 0, array.Length);
|
|
return Encoding.UTF8.GetString(bytes2);
|
|
}
|
|
}
|