using System;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Crypto3DES 的摘要说明。
/// </summary>

public class Crypto3DES
{
    
public Crypto3DES()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }


    
private System.Text.Encoding encoding;

    
/// <summary>
    
/// 获取密匙
    
/// </summary>

    public string Key
    
{
        
get
        
{
            
return System.Configuration.ConfigurationSettings.AppSettings[ "cryptoKey" ];
        }

    }


    
/// <summary>
    
/// 获取或设置加密解密的编码
    
/// </summary>

    public System.Text.Encoding Encoding
    
{
        
get
        
{
            
if( encoding == null )
            
{
                encoding 
= System.Text.Encoding.UTF8;
            }

            
return encoding;
        }


        
set
        
{
            encoding 
= value;
        }

    }


    
/// <summary>
    
/// 加密
    
/// </summary>
    
/// <param name="strString"></param>
    
/// <param name="strKey"></param>
    
/// <param name="encoding"></param>
    
/// <returns></returns>

    public string Encrypt3DES( string strString )
    
{
        TripleDESCryptoServiceProvider DES 
= new TripleDESCryptoServiceProvider();

        DES.Key 
= Encoding.GetBytes( this.Key );
        DES.Mode 
= CipherMode.ECB;

        ICryptoTransform DESEncrypt 
= DES.CreateEncryptor();

        
byte[] Buffer = encoding.GetBytes(strString);

        
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
    }


    
/// <summary>
    
/// 解密
    
/// </summary>
    
/// <param name="strString"></param>
    
/// <param name="strKey"></param>
    
/// <returns></returns>

    public string Decrypt3DES( string strString )
    
{   
        TripleDESCryptoServiceProvider DES 
= new TripleDESCryptoServiceProvider();

        DES.Key 
= Encoding.UTF8.GetBytes( this.Key );
        DES.Mode 
= CipherMode.ECB;
        ICryptoTransform DESDecrypt 
= DES.CreateDecryptor();

        
byte[] Buffer = Convert.FromBase64String(strString);
        
return UTF8Encoding.UTF8.GetString( DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length) );
    }

}