中文加密无法解密成中文

sz_lgp 2005-10-04 05:37:46
以下是本人写的一个加密解密类,用这个类生成实例后可以对数字、英文加密解密,但中文加密后不能还原,不知为什么?另外加密用的“密钥”和“密值”有什么要求?也就是KEY和IV有什么要求?



Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Class 加密解密
Private 密钥 As String
Private 密值 As String
Public WriteOnly Property set密钥()
Set(ByVal Value)
密钥 = Value
End Set
End Property
Public WriteOnly Property set密值()
Set(ByVal Value)
密值 = Value
End Set
End Property
Public Sub 加密(ByVal 加密文件 As String, ByVal 加密后文件 As String)
Try

Dim myDESProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider

myDESProvider.Key = System.Text.UTF8Encoding.UTF8.GetBytes(密钥)
myDESProvider.IV = System.Text.UTF8Encoding.UTF8.GetBytes(密值)


Dim myICryptoTransform As ICryptoTransform = _
myDESProvider.CreateEncryptor(myDESProvider.Key, myDESProvider.IV)

Dim ProcessFileStream As FileStream = New _
FileStream(加密文件, FileMode.Open, FileAccess.Read)

Dim resultFileStream As FileStream = New _
FileStream(加密后文件, FileMode.Open, FileAccess.Write)

Dim myCryptoStream As CryptoStream = New _
CryptoStream(resultFileStream, myICryptoTransform, CryptoStreamMode.Write)

Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte

ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)

myCryptoStream.Close()
ProcessFileStream.Close()



Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

End Sub

Public Function 解密(ByVal 解密文件 As String) As String
Try
Dim myDESProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider

myDESProvider.Key = System.Text.UTF8Encoding.UTF8.GetBytes(密钥)
myDESProvider.IV = System.Text.UTF8Encoding.UTF8.GetBytes(密值)

Dim DecryptedFile As FileStream = New FileStream(解密文件, FileMode.Open, FileAccess.Read)
Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateDecryptor(myDESProvider.Key, myDESProvider.IV)
Dim myCryptoStream As CryptoStream = New CryptoStream(DecryptedFile, myICryptoTransform, CryptoStreamMode.Read)

Dim myDecStreamReader As New StreamReader(myCryptoStream)

解密 = myDecStreamReader.ReadToEnd
myCryptoStream.Close()
myDecStreamReader.Close()

Catch ex As Exception

MessageBox.Show(ex.ToString())
End Try
End Function
End Class
...全文
246 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sz_lgp 2005-10-18
  • 打赏
  • 举报
回复
谢谢啦!
yzg100 2005-10-08
  • 打赏
  • 举报
回复
KEY自已指定,1-253之间。
yzg100 2005-10-08
  • 打赏
  • 举报
回复
'
'btnDecrypt
'
Me.btnDecrypt.Enabled = False
Me.btnDecrypt.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnDecrypt.Location = New System.Drawing.Point(148, 278)
Me.btnDecrypt.Name = "btnDecrypt"
Me.btnDecrypt.Size = New System.Drawing.Size(86, 23)
Me.btnDecrypt.TabIndex = 2
Me.btnDecrypt.Text = "解密字符串"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.TextBox4)
Me.GroupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.GroupBox2.Location = New System.Drawing.Point(5, 144)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(362, 128)
Me.GroupBox2.TabIndex = 1
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "解密后的字符串"
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(6, 20)
Me.TextBox4.Multiline = True
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.ReadOnly = True
Me.TextBox4.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox4.Size = New System.Drawing.Size(350, 102)
Me.TextBox4.TabIndex = 0
Me.TextBox4.Text = ""
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.TextBox3)
Me.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.GroupBox1.Location = New System.Drawing.Point(4, 6)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(362, 132)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "要解密的字符串"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(6, 16)
Me.TextBox3.Multiline = True
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox3.Size = New System.Drawing.Size(350, 108)
Me.TextBox3.TabIndex = 0
Me.TextBox3.Text = ""
'
'Button3
'
Me.Button3.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Button3.Location = New System.Drawing.Point(298, 344)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 3
Me.Button3.Text = "退出"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(388, 372)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.TabControl1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "Form1"
Me.Text = "加密字符串"
Me.TabControl1.ResumeLayout(False)
Me.TabPage1.ResumeLayout(False)
Me.GroupBox4.ResumeLayout(False)
Me.GroupBox3.ResumeLayout(False)
Me.TabPage2.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New FolderBrowserDialog
If frm.ShowDialog() = DialogResult.OK Then
TextBox1.Text = frm.SelectedPath
End If
End Sub




Private CSP As SymmetricAlgorithm = New DESCryptoServiceProvider

'对称算法的机密密钥和初始化向量数组,最长8位
Private Shared k() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}

'加密字符串
Public Function EncryptString(ByVal Value As String) As String
Try
Dim ct As ICryptoTransform
Dim ms As MemoryStream
Dim cs As CryptoStream
Dim byt As Byte()
CSP.Key = k
CSP.IV = k
ct = CSP.CreateEncryptor(CSP.Key, CSP.IV)

byt = Encoding.UTF8.GetBytes(Value)

ms = New MemoryStream
cs = New CryptoStream(ms, ct, CryptoStreamMode.Write)
cs.Write(byt, 0, byt.Length)
cs.FlushFinalBlock()

cs.Close()

Return Convert.ToBase64String(ms.ToArray())
Catch EX As Exception
Throw EX
End Try
End Function
'解密字符串
Public Function DecryptString(ByVal Value As String) As String
Try
Dim ct As ICryptoTransform
Dim ms As MemoryStream
Dim cs As CryptoStream
Dim byt As Byte()
CSP.Key = k
CSP.IV = k
ct = CSP.CreateDecryptor(CSP.Key, CSP.IV)

byt = Convert.FromBase64String(Value)

ms = New MemoryStream
cs = New CryptoStream(ms, ct, CryptoStreamMode.Write)
cs.Write(byt, 0, byt.Length)
cs.FlushFinalBlock()

cs.Close()

Return Encoding.UTF8.GetString(ms.ToArray())
Catch EX As Exception
Throw EX
End Try
End Function

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
btnEncrypt.Enabled = Not TextBox1.Text.Equals("")
End Sub

Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
Try
TextBox5.Text = EncryptString(TextBox1.Text)
Catch EX As Exception
TextBox5.Text = "加密失败!"
End Try
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
btnDecrypt.Enabled = Not TextBox3.Text.Equals("")
End Sub

Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
Try
TextBox4.Text = DecryptString(TextBox3.Text)
Catch ex As Exception
TextBox3.Text = ""
TextBox4.Text = "解密失败,请输入加密过的字符串!"
End Try
End Sub

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Select Case TabControl1.SelectedIndex
Case 0
Text = "加密字符串"
Case 1
Text = "解密字符串"
End Select
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Application.Exit()
End Sub
End Class
yzg100 2005-10-08
  • 打赏
  • 举报
回复
可以呀。给你一段源代码。

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()
Application.EnableVisualStyles()
'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox4 As System.Windows.Forms.GroupBox
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents btnEncrypt As System.Windows.Forms.Button
Friend WithEvents btnDecrypt As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TabControl1 = New System.Windows.Forms.TabControl
Me.TabPage1 = New System.Windows.Forms.TabPage
Me.GroupBox4 = New System.Windows.Forms.GroupBox
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.GroupBox3 = New System.Windows.Forms.GroupBox
Me.btnEncrypt = New System.Windows.Forms.Button
Me.TabPage2 = New System.Windows.Forms.TabPage
Me.btnDecrypt = New System.Windows.Forms.Button
Me.GroupBox2 = New System.Windows.Forms.GroupBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Button3 = New System.Windows.Forms.Button
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.GroupBox4.SuspendLayout()
Me.GroupBox3.SuspendLayout()
Me.TabPage2.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Button1.Location = New System.Drawing.Point(316, 18)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(44, 104)
Me.Button1.TabIndex = 0
Me.Button1.Text = "..."
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(6, 18)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox1.Size = New System.Drawing.Size(306, 104)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'TabControl1
'
Me.TabControl1.Controls.Add(Me.TabPage1)
Me.TabControl1.Controls.Add(Me.TabPage2)
Me.TabControl1.Location = New System.Drawing.Point(4, 6)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(380, 332)
Me.TabControl1.TabIndex = 2
'
'TabPage1
'
Me.TabPage1.Controls.Add(Me.GroupBox4)
Me.TabPage1.Controls.Add(Me.GroupBox3)
Me.TabPage1.Controls.Add(Me.btnEncrypt)
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Size = New System.Drawing.Size(372, 306)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "加密"
'
'GroupBox4
'
Me.GroupBox4.Controls.Add(Me.TextBox5)
Me.GroupBox4.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.GroupBox4.Location = New System.Drawing.Point(6, 142)
Me.GroupBox4.Name = "GroupBox4"
Me.GroupBox4.Size = New System.Drawing.Size(362, 130)
Me.GroupBox4.TabIndex = 5
Me.GroupBox4.TabStop = False
Me.GroupBox4.Text = "加密后的字符串"
'
'TextBox5
'
Me.TextBox5.Location = New System.Drawing.Point(6, 16)
Me.TextBox5.Multiline = True
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.ReadOnly = True
Me.TextBox5.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox5.Size = New System.Drawing.Size(352, 108)
Me.TextBox5.TabIndex = 0
Me.TextBox5.Text = ""
'
'GroupBox3
'
Me.GroupBox3.Controls.Add(Me.TextBox1)
Me.GroupBox3.Controls.Add(Me.Button1)
Me.GroupBox3.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.GroupBox3.Location = New System.Drawing.Point(4, 6)
Me.GroupBox3.Name = "GroupBox3"
Me.GroupBox3.Size = New System.Drawing.Size(364, 130)
Me.GroupBox3.TabIndex = 4
Me.GroupBox3.TabStop = False
Me.GroupBox3.Text = "选择本地目录或输入要加密的字符串"
'
'btnEncrypt
'
Me.btnEncrypt.Enabled = False
Me.btnEncrypt.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnEncrypt.Location = New System.Drawing.Point(132, 278)
Me.btnEncrypt.Name = "btnEncrypt"
Me.btnEncrypt.Size = New System.Drawing.Size(112, 23)
Me.btnEncrypt.TabIndex = 3
Me.btnEncrypt.Text = "字符串加密"
'
'TabPage2
'
Me.TabPage2.Controls.Add(Me.btnDecrypt)
Me.TabPage2.Controls.Add(Me.GroupBox2)
Me.TabPage2.Controls.Add(Me.GroupBox1)
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Size = New System.Drawing.Size(372, 306)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "解密"
sz_lgp 2005-10-06
  • 打赏
  • 举报
回复
高手都去泡吧了?这里是冷深秋了。
projectmanager 2005-10-06
  • 打赏
  • 举报
回复

====================================================================
本人有大量源代码(各种行业),如有哪位需要,请和我联系。
projectmastercn@yahoo.com.cn
haoya1 2005-10-06
  • 打赏
  • 举报
回复
是因用了UTF-8编码,而又搞不清偏移向量(IV)的问题,UTF-8必以239,187,191开头的,否则出现乱码的,而可能由于向量问题把这些编码丢了。

建议用二进制进行调试,如果二进制准确无误那么符形式也就正确了。

16,552

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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