关于DEC加密解密显示的问题...
///
/// DEC 加密过程
///
///
///
///
public static string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
}
///
/// DEC 解密过程
///
///
///
///
public static string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
加密解密的函数如上
我现在把这个编译成一个类之后放到VB.net的winform中调用
写入数据库的代码是:
SqlCommand_Update.CommandText = "Update Users set Password = @Password where UserName = '" & user & "'"
SqlCommand_Update.Parameters.Add("@Password", System.Data.SqlDbType.Binary)
Dim pass As String = ZCB.Framework.Security.Encode(Txt_Password.Text)
Dim passByte As Byte()
passByte = Convert.FromBase64String(pass)
SqlCommand_Update.Parameters(0).Value = passByte
Try
SqlCommand_Update.ExecuteNonQuery()
可以正确写入....
读出的代码是:
strSQL = "SELECT Password FROM Users WHERE Password is not Null and UserName = '" + UserName + "'"
If PublicSub.Search(strSQL, True) = True Then
Dim passByte As Byte() = CType(PublicVar.Pub_DataRow.Item(0), Byte())
Dim pass As String = Convert.ToBase64String(passByte)
Txt_Password.Text = ZCB.Framework.Security.Decode(pass)
会在return sr.ReadToEnd();的时候报错误的数据这个错误
我数据库中的格式是Binary 16