using System;
using System.IO;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.Security;
using System.Security.Cryptography;
/**//*
* .Net Frame due to ownership CLR Provides rich library support for previous use with very little code C Encryption algorithms that are difficult to implement in older languages. This class implements 1 Some commonly used confidential algorithms for reference. Among them MD5 Algorithm to return Int the ToString String. See before for an algorithm that returns alphanumeric results Blog The article
*/
namespace Digital processing of archives
{
/**//// <summary>
/// The name of the class: HashEncrypt
/// Action: to the incoming string Hash Operation, return pass Hash A string encrypted by an algorithm.
/// Attribute: [none]
/// Constructor amount parameter:
/// IsReturnNum: Whether to return as a encrypted character Byte code
/// IsCaseSensitive : case sensitive.
/// Method: this class is provided MD5 . SHA1 . SHA256 . SHA512 Etc. 4 The length of the encrypted string increases in turn.
/// </summary>
public class HashEncrypt
{
//private string strIN;
private bool isReturnNum;
private bool isCaseSensitive;
/**//// <summary>
/// Class, which is provided MD5 . SHA1 . SHA256 . SHA512 Etc. 4 The length of the encrypted string increases in turn.
/// </summary>
/// <param name="IsCaseSensitive"> Case sensitive </param>
/// <param name="IsReturnNum"> Whether to return as a encrypted character Byte code </param>
public HashEncrypt(bool IsCaseSensitive, bool IsReturnNum)
{
this.isReturnNum = IsReturnNum;
this.isCaseSensitive = IsCaseSensitive;
}
private string getstrIN(string strIN)
{
//string strIN = strIN;
if (strIN.Length == 0)
{
strIN = "~NULL~";
}
if (isCaseSensitive == false)
{
strIN = strIN.ToUpper();
}
return strIN;
}
public string MD5Encrypt(string strIN)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
MD5 md5 = new MD5CryptoServiceProvider();
tmpByte = md5.ComputeHash(GetKeyByteArray(getstrIN(strIN)));
md5.Clear();
return GetStringValue(tmpByte);
}
public string SHA1Encrypt(string strIN)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
SHA1 sha1 = new SHA1CryptoServiceProvider();
tmpByte = sha1.ComputeHash(GetKeyByteArray(strIN));
sha1.Clear();
return GetStringValue(tmpByte);
}
public string SHA256Encrypt(string strIN)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
SHA256 sha256 = new SHA256Managed();
tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN));
sha256.Clear();
return GetStringValue(tmpByte);
}
public string SHA512Encrypt(string strIN)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
SHA512 sha512 = new SHA512Managed();
tmpByte = sha512.ComputeHash(GetKeyByteArray(strIN));
sha512.Clear();
return GetStringValue(tmpByte);
}
/**//// <summary>
/// use DES Encryption ( Added by niehl 2005-4-6 )
/// </summary>
/// <param name="originalValue"> A string to be encrypted </param>
/// <param name="key"> The key ( The maximum length 8)</param>
/// <param name="IV"> Initialization vector ( The maximum length 8)</param>
/// <returns> The encrypted string </returns>
public string DESEncrypt(string originalValue, string key, string IV)
{
// will key and IV Processing into 8 A character
key += "12345678";
IV += "12345678";
key = key.Substring(0, 8);
IV = IV.Substring(0, 8);
SymmetricAlgorithm sa;
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
sa = new DESCryptoServiceProvider();
sa.Key = Encoding.UTF8.GetBytes(key);
sa.IV = Encoding.UTF8.GetBytes(IV);
ct = sa.CreateEncryptor();
byt = Encoding.UTF8.GetBytes(originalValue);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
public string DESEncrypt(string originalValue, string key)
{
return DESEncrypt(originalValue, key, key);
}
/**//// <summary>
/// use DES Decryption ( Added by niehl 2005-4-6 )
/// </summary>
/// <param name="encryptedValue"> A string to be decrypted </param>
/// <param name="key"> The key ( The maximum length 8)</param>
/// <param name="IV">m Initialization vector ( The maximum length 8)</param>
/// <returns> The decrypted string </returns>
public string DESDecrypt(string encryptedValue, string key, string IV)
{
// will key and IV Processing into 8 A character
key += "12345678";
IV += "12345678";
key = key.Substring(0, 8);
IV = IV.Substring(0, 8);
SymmetricAlgorithm sa;
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
sa = new DESCryptoServiceProvider();
sa.Key = Encoding.UTF8.GetBytes(key);
sa.IV = Encoding.UTF8.GetBytes(IV);
ct = sa.CreateDecryptor();
byt = Convert.FromBase64String(encryptedValue);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
public string DESDecrypt(string encryptedValue, string key)
{
return DESDecrypt(encryptedValue, key, key);
}
private string GetStringValue(byte[] Byte)
{
string tmpString = "";
if (this.isReturnNum == false)
{
ASCIIEncoding Asc = new ASCIIEncoding();
tmpString = Asc.GetString(Byte);
}
else
{
int iCounter;
for (iCounter = 0; iCounter < Byte.Length; iCounter++)
{
tmpString = tmpString + Byte[iCounter].ToString();
}
}
return tmpString;
}
private byte[] GetKeyByteArray(string strKey)
{
ASCIIEncoding Asc = new ASCIIEncoding();
int tmpStrLen = strKey.Length;
byte[] tmpByte = new byte[tmpStrLen - 1];
tmpByte = Asc.GetBytes(strKey);
return tmpByte;
}
}
}