C#的DES加密解密模块加密解密16进制文本的代码或例子?

 我来答
ID孙冲
2010-11-28
知道答主
回答量:43
采纳率:0%
帮助的人:21.4万
展开全部
''' <summary>
''' 二进制文件流加密类
''' </summary>
''' <remarks></remarks>
Public Class EncryptionDES

''' <summary>
''' 默认密钥向量
''' </summary>
''' <remarks></remarks>
Private Shared Keys As Byte() = New Byte() {&H12, &H34, &H56, 120, &H90, &HAB, &HCD, &HEF}

''' <summary>
''' 文件加密
''' </summary>
''' <param name="encryptBy">待加密文件流</param>
''' <param name="encryptKey">密钥</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function EncryptDES(ByVal encryptBy As Byte(), ByVal encryptKey As String) As Byte()
Dim mStream As MemoryStream = Nothing
Dim cStream As CryptoStream = Nothing
Try
Dim rgbKey As Byte() = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8))
Dim rgbIV As Byte() = Keys
Dim dCSP As New DESCryptoServiceProvider
mStream = New MemoryStream
cStream = New CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)
cStream.Write(encryptBy, 0, encryptBy.Length)
cStream.FlushFinalBlock()
Return mStream.ToArray
Catch
Return encryptBy
Finally
mStream.Dispose()
mStream.Close()
cStream.Flush()
cStream.Dispose()
cStream.Close()
End Try
End Function

''' <summary>
''' 文件解密
''' </summary>
''' <param name="decryptBy">待解密文件流</param>
''' <param name="decryptKey">密钥</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function DecryptDES(ByVal decryptBy As Byte(), ByVal decryptKey As String) As Byte()
Dim mStream As MemoryStream = Nothing
Dim cStream As CryptoStream = Nothing
Try
Dim rgbKey As Byte() = Encoding.UTF8.GetBytes(decryptKey)
Dim rgbIV As Byte() = Keys
Dim DCSP As New DESCryptoServiceProvider
mStream = New MemoryStream
cStream = New CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write)
cStream.Write(decryptBy, 0, decryptBy.Length)
cStream.FlushFinalBlock()
Return mStream.ToArray
Catch
Return decryptBy
Finally
mStream.Dispose()
mStream.Close()
cStream.Flush()
cStream.Dispose()
cStream.Close()
End Try
End Function

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim by As Byte()
Dim sfd As New SaveFileDialog
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)
sfd.Filter = "test(*.test)|*.testa"
If sfd.ShowDialog = DialogResult.OK Then
Dim stream As Stream = File.Open(sfd.FileName, FileMode.Create)
Dim formatter As New BinaryFormatter()
formatter.Serialize(stream, tClass)
stream.Close()

by = File.ReadAllBytes(sfd.FileName)
File.WriteAllBytes(sfd.FileName, EncryptionDES.EncryptDES(by, "05320532"))
End If
End Sub
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jkchenwo
2010-11-28 · TA获得超过4300个赞
知道小有建树答主
回答量:1019
采纳率:0%
帮助的人:548万
展开全部
下载一个加密解密的类,直接调用方法就可以了,比如common类里面的加密方法名叫addMi(string a),在修改密码时获得密码是可以这也调用
string pwd=common.addMi(txtPwd.Text);
解密调用解密的方法
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式