- Регистрация
- 25.06.2011
- Сообщения
- 1 547
- Благодарностей
- 1 311
- Баллы
- 113
Обращаем Ваше внимание на то, что данный пользователь заблокирован.
Не рекомендуем проводить с 7make какие-либо сделки.
Класс реализует такие популярные алгоритмы шифрования:
MD5/SHA/SHA256/SHA384/SHA512/AES/3DES/DES
RAW
MD5/SHA/SHA256/SHA384/SHA512/AES/3DES/DES
RAW
C#:
using System;
using System.Security.Cryptography;
using System.Text;
namespace Cryptography
{
/// <summary>
/// Static class help work crypto hashes
/// </summary>
public static class Crypto
{
#region MD5
/// <summary>
/// Calculate MD5 hash
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
public static string HashMD5(string phrase)
{
if (phrase == null)
return null;
var encoder = new UTF8Encoding();
var md5Hasher = new MD5CryptoServiceProvider();
var hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(phrase));
return ByteArrayToHexString(hashedDataBytes);
}
#endregion
#region SHA
/// <summary>
/// Calculate SHA hash
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
public static string HashSHA1(string phrase)
{
if (phrase == null)
return null;
var encoder = new UTF8Encoding();
var sha1Hasher = new SHA1CryptoServiceProvider();
var hashedDataBytes = sha1Hasher.ComputeHash(encoder.GetBytes(phrase));
return ByteArrayToHexString(hashedDataBytes);
}
/// <summary>
/// Calculate SHA256 hash
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
public static string HashSHA256(string phrase)
{
if (phrase == null)
return null;
var encoder = new UTF8Encoding();
var sha256Hasher = new SHA256CryptoServiceProvider();
var hashedDataBytes = sha256Hasher.ComputeHash(encoder.GetBytes(phrase));
return ByteArrayToHexString(hashedDataBytes);
}
/// <summary>
/// Calculate SHA384 hash
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
public static string HashSHA384(string phrase)
{
if (phrase == null)
return null;
var encoder = new UTF8Encoding();
var sha384Hasher = new SHA384CryptoServiceProvider();
var hashedDataBytes = sha384Hasher.ComputeHash(encoder.GetBytes(phrase));
return ByteArrayToHexString(hashedDataBytes);
}
/// <summary>
/// Calculate SHA512 hash
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
public static string HashSHA512(string phrase)
{
if (phrase == null)
return null;
var encoder = new UTF8Encoding();
var sha512Hasher = new SHA512CryptoServiceProvider();
var hashedDataBytes = sha512Hasher.ComputeHash(encoder.GetBytes(phrase));
return ByteArrayToHexString(hashedDataBytes);
}
#endregion
#region AES
/// <summary>
/// Encrypt AES
/// </summary>
/// <param name="phrase"></param>
/// <param name="key"></param>
/// <param name="hashKey"></param>
/// <returns></returns>
public static string EncryptAES(string phrase, string key, bool hashKey = true)
{
if (phrase == null || key == null)
return null;
var keyArray = HexStringToByteArray(hashKey ? HashMD5(key) : key);
var toEncryptArray = Encoding.UTF8.GetBytes(phrase);
byte[] result;
using (var aes = new AesCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
})
{
var cTransform = aes.CreateEncryptor();
result = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
aes.Clear();
}
return ByteArrayToHexString(result);
}
/// <summary>
/// Decrypt AES
/// </summary>
/// <param name="hash"></param>
/// <param name="key"></param>
/// <param name="hashKey"></param>
/// <returns></returns>
public static string DecryptAES(string hash, string key, bool hashKey = true)
{
if (hash == null || key == null)
return null;
var keyArray = HexStringToByteArray(hashKey ? HashMD5(key) : key);
var toEncryptArray = HexStringToByteArray(hash);
var aes = new AesCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var cTransform = aes.CreateDecryptor();
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
aes.Clear();
return Encoding.UTF8.GetString(resultArray);
}
#endregion
#region 3DES
/// <summary>
/// Encrypt 3DES
/// </summary>
/// <param name="phrase"></param>
/// <param name="key"></param>
/// <param name="hashKey"></param>
/// <returns></returns>
public static string EncryptTripleDES(string phrase, string key, bool hashKey = true)
{
var keyArray = HexStringToByteArray(hashKey ? HashMD5(key) : key);
var toEncryptArray = Encoding.UTF8.GetBytes(phrase);
var tdes = new TripleDESCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var cTransform = tdes.CreateEncryptor();
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return ByteArrayToHexString(resultArray);
}
/// <summary>
/// Decrypt DES
/// </summary>
/// <param name="hash"></param>
/// <param name="key"></param>
/// <param name="hashKey"></param>
/// <returns></returns>
public static string DecryptTripleDES(string hash, string key, bool hashKey = true)
{
var keyArray = HexStringToByteArray(hashKey ? HashMD5(key) : key);
var toEncryptArray = HexStringToByteArray(hash);
var tdes = new TripleDESCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var cTransform = tdes.CreateDecryptor();
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Encoding.UTF8.GetString(resultArray);
}
#endregion
#region Helpers
/// <summary>
/// Convert byte array to hex string
/// </summary>
/// <param name="inputArray"></param>
/// <returns></returns>
internal static string ByteArrayToHexString(byte[] inputArray)
{
if (inputArray == null)
return null;
var o = new StringBuilder("");
for (var i = 0; i < inputArray.Length; i++)
o.Append(inputArray[i].ToString("X2"));
return o.ToString();
}
/// <summary>
/// Convert hex string to byte array
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
internal static byte[] HexStringToByteArray(string inputString)
{
if (inputString == null)
return null;
if (inputString.Length == 0)
return new byte[0];
if (inputString.Length%2 != 0)
throw new Exception(
"Hex strings have an even number of characters and you have got an odd number of characters!");
var num = inputString.Length/2;
var bytes = new byte[num];
for (var i = 0; i < num; i++)
{
var x = inputString.Substring(i*2, 2);
try
{
bytes[i] = Convert.ToByte(x, 16);
}
catch (Exception ex)
{
throw new Exception("Part of your \"hex\" string contains a non-hex value.", ex);
}
}
return bytes;
}
#endregion
}
}