C#源碼,用.Net封裝的類庫寫的加密和解密的方法,很容易轉換成VB.Net
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace MyDes
{
/// <summary>
/// DES加/解密與MD5加密
/// </summary>
public class My_Des
{
#region 私人屬性
/// <summary>
/// MD5加密的字串
/// </summary>
private string md5Str = null;
/// <summary>
/// DES加密的字串
/// </summary>
private string encryptStr = null;
/// <summary>
/// DES解密的字串
/// </summary>
private string decryptStr = null;
/// <summary>
/// DES密匙
/// </summary>
private string mydesKey = null;
/// <summary>
/// 返回的字串
/// </summary>
private string mydesStr = null;
/// <summary>
/// 錯誤資訊
/// </summary>
private string messAge = null;
#endregion
#region 公用屬性
/// <summary>
/// MD5加密字串
/// </summary>
public string MD5Str
{
get { return md5Str; }
set { md5Str = value; }
}
/// <summary>
/// DES加密的字串
/// </summary>
public string EncryptStr
{
get { return encryptStr; }
set { encryptStr = value; }
}
/// <summary>
///DES 解密的字串
/// </summary>
public string DecryptStr
{
get { return decryptStr; }
set { decryptStr = value; }
}
/// <summary>
/// DES密匙
/// </summary>
public string MyDesKey
{
get { return mydesKey; }
set { mydesKey = value; }
}
/// <summary>
/// 返回的字串
/// </summary>
public string MyDesStr
{
get { return mydesStr; }
set { mydesStr = value; }
}
/// <summary>
/// 錯誤資訊
/// </summary>
public string Message
{
get { return messAge; }
set { messAge = value; }
}
#endregion
#region 公用方法
/// <summary>
/// 執行DES加密
/// </summary>
public void DesEncrypt()
{
try
{
byte[] MyStr_E = Encoding.UTF8.GetBytes(this.encryptStr);
byte[] MyKey_E = Encoding.UTF8.GetBytes(this.mydesKey);
DESCryptoServiceProvider MyDes_E = new DESCryptoServiceProvider();
MyDes_E.Key = MyKey_E;
MyDes_E.IV = MyKey_E;
MemoryStream MyMem_E = new MemoryStream();
CryptoStream MyCry_E = new CryptoStream(MyMem_E, MyDes_E.CreateEncryptor(), CryptoStreamMode.Write);
MyCry_E.Write(MyStr_E, 0, MyStr_E.Length);
MyCry_E.FlushFinalBlock();
MyCry_E.Close();
this.mydesStr = Convert.ToBase64String(MyMem_E.ToArray());
}
catch (Exception Error)
{
this.messAge = "DES加密出錯:"+Error.Message;
}
}
/// <summary>
/// 執行DES解密
/// </summary>
public void DesDecrypt()
{
try
{
byte[] MyStr_D = Convert.FromBase64String(this.decryptStr);
byte[] MyKey_D = Encoding.UTF8.GetBytes(this.mydesKey);
DESCryptoServiceProvider MyDes_D = new DESCryptoServiceProvider();
MyDes_D.Key = MyKey_D;
MyDes_D.IV = MyKey_D;
MemoryStream MyMem_D = new MemoryStream();
CryptoStream MyCry_D = new CryptoStream(MyMem_D, MyDes_D.CreateDecryptor(), CryptoStreamMode.Write);
MyCry_D.Write(MyStr_D, 0, MyStr_D.Length);
MyCry_D.FlushFinalBlock();
MyCry_D.Close();
this.mydesStr = Encoding.UTF8.GetString(MyMem_D.ToArray());
}
catch (Exception Error)
{
this.messAge ="DES解密出錯:" +Error.Message;
}
}
/// <summary>
/// 執行MD5加密
/// </summary>
public void MD5JiaMi()
{
MD5CryptoServiceProvider MyMD5 = new MD5CryptoServiceProvider();
try
{
Byte[] MyMD5_Str = MyMD5.ComputeHash(Encoding.UTF8.GetBytes(this.md5Str));
this.MyDesStr = Encoding.UTF8.GetString(MyMD5_Str);
}
catch (Exception Error)
{
this.messAge ="MD5加密出錯:"+ Error.Message;
}
}
#endregion
}
}