/// <summary>
/// 對數(shù)據(jù)進行加密
/// </summary>
/// <param name="encryptstring">需要加密的數(shù)據(jù)</param>
/// <returns></returns>
public string DESEncrypt(string encryptstring)
{
string strRtn;
try
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider(); //des進行加密
PasswordDeriveBytes encryptkey = new PasswordDeriveBytes(encryptstring, null);//產(chǎn)生key
byte[] key = encryptkey.GetBytes(8);
byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring);//取到密碼的字節(jié)流
MemoryStream ms = new MemoryStream(); //用來存儲加密后的數(shù)據(jù)
CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write); //進行加密
cs.Write(data,0,data.Length );
cs.FlushFinalBlock(); //用緩沖區(qū)的當前狀態(tài)更新基礎數(shù)據(jù)源或存儲庫,隨后清除緩沖區(qū)
strRtn = Convert.ToBase64String(ms.ToArray()); //將整個流內(nèi)容寫入字節(jié)數(shù)組,轉換到字符串返回
return strRtn;
}
catch (Exception ex)
{
//彈出捕獲的異常信息
MessageBox.Show("錯誤:"+ex.Message ,"錯誤消息提示框",MessageBoxButtons.OKCancel,MessageBoxIcon.Error );
return null ;
}
}
/// <summary>
/// 對數(shù)據(jù)進行解密
/// </summary>
/// <param name="decryptstring">需要解密的數(shù)據(jù)</param>
/// <returns></returns>
public string DESDecrypt(string decryptstring)
{
string strRtn;
try
{
//定義訪問數(shù)據(jù)加密標準(DES)算法的加密服務提供程序(CSP)版本的包裝對象
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
PasswordDeriveBytes encryptkey = new PasswordDeriveBytes(decryptstring, null);//產(chǎn)生key
byte[] key = encryptkey.GetBytes(8);
byte[] data = Convert.FromBase64String(decryptstring);
MemoryStream ms = new MemoryStream(); //用來存儲解密后的數(shù)據(jù)
CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length); //進行解密
cs.FlushFinalBlock(); //用緩沖區(qū)的當前狀態(tài)更新基礎數(shù)據(jù)源或存儲庫,隨后清除緩沖區(qū)
strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray()); //將整個流內(nèi)容寫入字節(jié)數(shù)組
return strRtn;
}
catch (Exception ex)
{
//彈出捕獲的異常信息
MessageBox.Show("錯誤:" + ex.Message, "錯誤消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
}
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請
點擊舉報。