Imports System.Text
Imports System.Security.Cryptography
Module ModSecurity
Function EnText(ByVal Text As String, ByVal sKey As String) As String
Text = Text.ToLower
Dim des As New DESCryptoServiceProvider
Dim inputByteArray() As Byte
inputByteArray = Encoding.Default.GetBytes(Text)
des.Key = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))
des.IV = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))
Dim ms As New System.IO.MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor, CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim ret As New StringBuilder
Dim b As Byte
For Each b In ms.ToArray()
ret.AppendFormat("{0:X2}", b)
Next
Return ret.ToString()
End Function
Function DeText(ByVal Text As String, ByVal sKey As String) As String
Dim des As New DESCryptoServiceProvider
Dim len As Integer
len = Text.Length /2-1 Dim inputByteArray(len) As Byte
Dim x, i As Integer
For x =0 To len
i = Convert.ToInt32(Text.Substring(x *2, 2), 16)
inputByteArray(x) = CType(i, Byte)
Next
des.Key = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))
des.IV = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))
Dim ms As New System.IO.MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor, CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Encoding.Default.GetString(ms.ToArray)
End Function
End Module
"========加密解密========"#region"========加密解密========" /**////<summary> /// 加密数据
///</summary> ///<param name="Text"></param> ///<param name="sKey"></param> ///<returns></returns> publicstaticstring EnText(string Text,string sKey)
{
DESCryptoServiceProvider des =new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}",b);
} return ret.ToString();
} /**////<summary> /// 解密数据
///</summary> ///<param name="Text"></param> ///<param name="sKey"></param> ///<returns></returns> publicstaticstring DeText(string Text,string sKey)
{
DESCryptoServiceProvider des =new DESCryptoServiceProvider();
int len;
len=Text.Length/2;
byte[] inputByteArray =newbyte[len];
int x,i;
for(x=0;x<len;x++)
{
i = Convert.ToInt32(Text.Substring(x *2, 2), 16);
inputByteArray[x]=(byte)i;
} des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
} #endregion
posted on
2005-03-31 16:53dragonpro
阅读(803)
评论(0)
收藏举报