using System;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Security ;
using System.Security.Cryptography ;
using System.Diagnostics ;
namespace app1_jm
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class jm //加密解密类
{
/// <summary>
/// 转换string到Byte树组
/// </summary>
/// <param name="s">要转换的字符串</param>
/// <returns>转换的Byte数组</returns>
public Byte[] StringToByteArray(String s)
{
/*
Char[] ca = s.ToCharArray();
Byte[] ba = new Byte[ca.Length];
for(int i=0; i<ba.Length; i++) ba[i] = (Byte)ca[i];
return ba;*/
return Encoding.UTF8.GetBytes(s) ;
}
/// <summary>
/// 转换Byte数组到字符串
/// </summary>
/// <param name="a_arrByte">Byte数组</param>
/// <returns>字符串</returns>
public string ByteArrayToString(Byte[] a_arrByte)
{
/*
//char[] ca = new char[a_arrByte.Length] ;
for(int i = 0 ; i < a_arrByte.Length ; i ++)
{
result += (char)a_arrByte[i] ;
}*/
return Encoding.UTF8.GetString(a_arrByte) ;
}
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <returns>加密后并经base64编码的字符串</returns>
/// <remarks>静态方法,采用默认ascii编码</remarks>
public string Encrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
public static string Encode(byte[] source)
{
byte[] dest;
dest = Base64Encode(source);
string ret;
ret = new ASCIIEncoding().GetString(dest);
// ret = ret.Replace("+","~");
// ret = ret.Replace("/","@");
// ret = ret.Replace("=","$");
//ret = HttpUtility.UrlEncode(ret);
return ret;
}
//改进的base64的解码
public static byte[] Decode(string source)
{
string dest;
// dest = source.Replace("~","+");
// dest = dest.Replace("@","/");
// dest = dest.Replace("$","=");
dest = source;//HttpUtility.UrlDecode(source);
byte[] ret;
ret = Base64Decode(new ASCIIEncoding().GetBytes(dest));
return ret;
}
//原始base64编码
public static byte[] Base64Encode(byte[] source)
{
if((source==null)||(source.Length ==0))
throw new ArgumentException("source is not valid");
ToBase64Transform tb64 = new ToBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;