public class SymetricAlgrithmCryptographyHelper<ST, ET>
where ST : SymmetricAlgorithm, new()
where ET : Encoding, new()
{
/// <summary>
/// KeySault is to fill the key if input key sting is too short
/// </summary>
public byte[] keySault = new byte[] { 0x43, 0x87, 0x23, 0x72 };
/// <summary>
/// this is used to generate the key according to the different Algrithm
/// </summary>
/// <param name="key">from user secret key</param>
/// <param name="size">how many bytes</param>
/// <returns></returns>
private byte[] GenerateIndentification(byte[] key, int size)
{
PasswordDeriveBytes pdb =
new PasswordDeriveBytes(key,
keySault);
return pdb.GetBytes(size);
}
private ST sa { get { return new ST(); } }
private ET ecoding;
public SymetricAlgrithmCryptographyHelper()
{
//this.sa = new ST();
this.ecoding = new ET();
}
public byte[] Encrypt(string key, byte[] input)
{
byte[] keyBytes = this.ecoding.GetBytes(key);
return Encrypt(keyBytes, input);
}
public byte[] Encrypt(byte[] key, byte[] input)
{
MemoryStream ms = new MemoryStream();
SymmetricAlgorithm cryptor = this.sa;
cryptor.Key = this.GenerateIndentification(key, cryptor.KeySize / 8);
cryptor.IV = this.GenerateIndentification(key, cryptor.BlockSize / 8);
CryptoStream cs = new CryptoStream(ms,
cryptor.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(input, 0, input.Length);
cs.Close();
return ms.ToArray();
}
public byte[] Decrypt(string key, byte[] input)
{
byte[] keyBytes = this.ecoding.GetBytes(key);
return Decrypt(keyBytes, input);
}
public byte[] Decrypt(byte[] key, byte[] input)
{
MemoryStream ms = new MemoryStream();
SymmetricAlgorithm cryptor = this.sa;
cryptor.Key = this.GenerateIndentification(key, cryptor.KeySize / 8);
cryptor.IV = this.GenerateIndentification(key, cryptor.BlockSize / 8);
CryptoStream cs = new CryptoStream(ms,
cryptor.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(input, 0, input.Length);
cs.Close();
return ms.ToArray();
}
/// <summary>
/// Encrypt to base 64 string
/// </summary>
/// <param name="key">secret key</param>
/// <param name="input">data</param>
/// <returns>Encrypted data string</returns>
public string EncryptToBase64String(string key, string input)
{
return Convert.ToBase64String(Encrypt(key, this.ecoding.GetBytes(input)));
}
/// <summary>
/// Decrypt to original string data
/// </summary>
/// <param name="key">secret key</param>
/// <param name="input">data</param>
/// <returns>Decrypted data</returns>
public string DecryptFromBase64String(string key, string input)
{
return this.ecoding.GetString(Decrypt(key, Convert.FromBase64String(input)));
}
}
使用:
//key
string sSecretKey = "DECPKEY2013";
//內容
string facetexe = "realcontext";
//選取算法實現類,編碼格式
SymetricAlgrithmCryptographyHelper<AesCryptoServiceProvider, UTF8Encoding> helper = new SymetricAlgrithmCryptographyHelper<AesCryptoServiceProvider, UTF8Encoding>();
//加密成64位字符 “CCHq7sCXBkUHgYjWfA4qDQ==”
string encryped = helper.EncryptToBase64String(sSecretKey, facetexe);
//用key解密
string faceback = helper.DecryptFromBase64String(sSecretKey, encryped);