关于DEC加密解密显示的问题...

coo_key 2005-01-26 04:52:13
///
/// 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

...全文
198 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
coo_key 2005-01-26
  • 打赏
  • 举报
回复
没有人知道阿?
coo_key 2005-01-26
  • 打赏
  • 举报
回复
如果我密码是6位就会报错〉。。
我跟踪了一下,从数据库读出来之后Ctype成byte()之后便成16位的,写入的时候是8位的,所以出错..但是我从数据库读出来后把后面的0截断掉,传一个8位的解密还是有问题,晕

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧