C#的DES加密解密模块加密解密16进制文本的代码或例子?
2个回答
展开全部
''' <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
''' 二进制文件流加密类
''' </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
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询