using System;
/// <summary>
/// TestEncrypt 的摘要说明。
/// </summary>
public class TestEncrypt
{
public TestEncrypt()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 根据传入参数生成加密后的验证码
/// </summary>
/// <param name="Params">传入参数(如:request.Account +request.TimeStamp)</param>
/// <returns></returns>
public string GenerateAuthenticators(string Params)
{
string Result="";
byte[] Key=HexStringToByteArray("FCB5023DA5545ED8680A2D07806DBDCA6ACDCF47E08F6A2D");
byte[] IV=new byte[]{1,2,3,4,5,6,7,8};
byte[] Encrypted;

//Encrypt(Key,IV,System.Convert.FromBase64String(Cryptogram.ComputeHashString(Params)),out Encrypted);
Encrypt(Key,IV,((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("SHA1")).ComputeHash(System.Text.Encoding.GetEncoding("utf-8").GetBytes(Params)),out Encrypted);
Result=System.Convert.ToBase64String(Encrypted);
return Result;
}
/// <summary>
/// 验证认证码是否正确      
/// </summary>
/// <param name="Auth">用户传入的认证码</param>
///<param name="Params">组合后的参数字符串(各参数有序组合)</param>
/// <returns></returns>
public bool CheckAuthenticator(string Auth,string Params)
{
string NewAuth = GenerateAuthenticators(Params);
return Auth==NewAuth?true:false;
}
/// <summary>
/// 使用指定的密钥和向量对一个byte数组进行TripleDES加密
/// </summary>
/// <param name="KEY">TripleDES密钥</param>
/// <param name="IV">TripleDES向量</param>
/// <param name="TobeEncrypted">准备进行TripleDES加密的byte数组</param>
/// <param name="Encrypted">加密后得到的byte数组</param>
/// <returns>如果加密成功就返回真,否则返回假。</returns>
public bool Encrypt(byte[] KEY ,byte[] IV ,byte[] TobeEncrypted, out  byte[] Encrypted )
{
if(KEY==null || IV==null)
throw new Exception("KEY不能为空!");
Encrypted=null;
try
{
byte[] tmpiv={0,1,2,3,4,5,6,7};
for(int ii=0;ii<8;ii++)
{
tmpiv[ii]=IV[ii];
}
byte[] tmpkey={0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7};
for(int ii=0;ii<KEY.Length;ii++)
{
tmpkey[ii]=KEY[ii];
}
System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
//tridesencrypt.Dispose();
System.Security.Cryptography.ICryptoTransform tridesencrypt = des.CreateEncryptor(tmpkey,tmpiv);
//tridesencrypt = des.CreateEncryptor(KEY,tmpiv);
Encrypted = tridesencrypt.TransformFinalBlock( TobeEncrypted,0,TobeEncrypted.Length);
//tridesencrypt.Dispose();
des.Clear();
return true;
}
catch(Exception e)
{
throw new Exception("发生了严重错误:"+e.Message.Trim());
}
catch
{
throw new Exception("发生了其他错误!");
}
}

/// <summary>
/// 将byte数组的16进制字符串表示形式转化为实际的byte数组。如"FF0D10"转化后即为byte[]{255, 13, 16}
/// </summary>
/// <param name="s">byte数组的16进制字符串表示形式</param>
/// <returns>实际的byte数组</returns>
private byte[] HexStringToByteArray(string s)
{
Byte[] buf=new byte[s.Length/2];
for(int i=0;i<buf.Length;i++)
{
buf[i]=(byte)(chr2hex(s.Substring(i*2,1))*0x10+chr2hex(s.Substring(i*2+1,1)));
}
return buf;
}
private byte chr2hex(string chr)
{
switch(chr)
{
case "0":
return 0x00;
case "1":
return 0x01;
case "2":
return 0x02;
case "3":
return 0x03;
case "4":
return 0x04;
case "5":
return 0x05;
case "6":
return 0x06;
case "7":
return 0x07;
case "8":
return 0x08;
case "9":
return 0x09;
case "A":
return 0x0a;
case "B":
return 0x0b;
case "C":
return 0x0c;
case "D":
return 0x0d;
case "E":
return 0x0e;
case "F":
return 0x0f;
}
return 0x00;
}
        static void Main(string[] args)
        {
            TestEncrypt te=new TestEncrypt();
            string str=te.GenerateAuthenticators("123456");
            Console.WriteLine(str);
        }
}