加密函数:http://community.csdn.net/Expert/topic/4174/4174058.xml?temp=.7768824解密函数:
Public Function DESDecryption(ByVal cipherText As String, ByVal key As String)
Dim Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}
Dim des As New DESCryptoServiceProvider
Dim iba(cipherText.Length / 2 - 1) As Byte
Dim x, i As Integer
For x = 0 To cipherText.Length / 2 - 1
i = Convert.ToInt32(cipherText.Substring(x * 2, 2), 16)
iba(x) = i
Next
des.Key = CreateKey(key)
des.IV = Vector
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor, CryptoStreamMode.Write)
cs.Write(iba, 0, iba.Length)
cs.FlushFinalBlock()
Dim ret As New System.Text.StringBuilder
Return System.Text.Encoding.Default.GetString(ms.ToArray())
End Function
Public Function DESDecryption(ByVal cipherText As String, ByVal key As String)
Dim Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}
Dim des As New DESCryptoServiceProvider
Dim iba(cipherText.Length / 2 - 1) As Byte
Dim x, i As Integer
For x = 0 To cipherText.Length / 2 - 1
i = Convert.ToInt32(cipherText.Substring(x * 2, 2), 16)
iba(x) = i
Next
des.Key = CreateKey(key)
des.IV = Vector
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor, CryptoStreamMode.Write)
cs.Write(iba, 0, iba.Length)
cs.FlushFinalBlock()
Dim ret As New System.Text.StringBuilder
Return System.Text.Encoding.Default.GetString(ms.ToArray())
End Function
解决方案 »
- 以前用table做的,现在想改成 div+css的应该怎么做?
- 自定义控件参数传递问题
- asp.net中的样式表引用了,但是没有效果
- aspx如何连接access数据库?
- 奇怪为什么我的程序运行的好好的 会出现这样的问题
- remoting 发布到IIS ===============================
- 将目录读取到TreeView 控件中后,并且显示在网页上,如何在程序中,让用户改变每个节点的名字》???
- 怎样用流打开mht文件??????????????????
- 各位老大~!~!让我无法入睡的问题~!在线等待~!多多帮忙~!
- System.data.SqlClient 与 System.data.OleDb
- 诚心帮忙的+我qq
- 正则高手进
和你加密的 des.Key,des.IV一样吗?不一样解密出来结果会不同的
/// DES加密字符串
/// </summary>
/// <param name="strInput"></param>
/// <param name="strKey"></param>
/// <param name="strIV"></param>
/// <returns></returns>
public string EncryString(string strInput,string strKey,string strIV)
{
if(strKey.Length > 8)
{
strKey = strKey.Substring(0,8);
}
else if(strKey.Length < 8)
{
for(int i = 0; i < 3; i ++)
{
strKey = strKey + strKey;
}
strKey = strKey.Substring(0,8);
}
else
{
byte[] btIV = System.Text.Encoding.UTF8.GetBytes(strKey);
byte[] key = System.Text.Encoding.UTF8.GetBytes(strIV);
DESCryptoServiceProvider mcsp = new DESCryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(strInput);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,mcsp.CreateEncryptor(key,btIV),CryptoStreamMode.Write);
cs.Write(bytValue,0,bytValue.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
} /// <summary>
/// DES解密字符串
/// </summary>
/// <param name="strInput"></param>
/// <param name="strKey"></param>
/// <param name="strIV"></param>
/// <returns></returns>
public string DecryString(string strInput,string strKey,string strIV)
{
if(strKey.Length > 8)
{
strKey = strKey.Substring(0,8);
}
else if(strKey.Length < 8)
{
for(int i = 0; i < 3; i ++)
{
strKey = strKey + strKey;
}
strKey = strKey.Substring(0,8);
}
else
{
byte[] btIV = System.Text.Encoding.UTF8.GetBytes(strIV);
byte[] key = System.Text.Encoding.UTF8.GetBytes(strKey);
DESCryptoServiceProvider mcsp = new DESCryptoServiceProvider(); byte[] bytValue = Convert.FromBase64String(strInput); MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,mcsp.CreateDecryptor(key,btIV),CryptoStreamMode.Write);
cs.Write(bytValue,0,bytValue.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
}这是C#的,只要strKey和strIV一样,加密和解密结果一样
把这个
else
{
byte[] btIV =
....................................
中的else去掉
static string strIV = "12345678";
private Byte[] Key = Encoding.UTF8.GetBytes(strKey.Substring(0,8));
private Byte[] IV = Encoding.UTF8.GetBytes(strIV.Substring(0,8)); //'加密函数
public String Encrypt(String strText)
{
try
{
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
} //'解密函数
public String Decrypt(String strText)
{ byte[] inputByteArray = new Byte[strText.Length];
try
{
inputByteArray = Convert.FromBase64String(strText);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}