台灣最大程式設計社群網站
線上人數
1814
 
會員總數:246038
討論主題:189625
歡迎您免費加入會員
討論區列表 >> VB6 >> VB 2005!!加解密尋求幫忙
[]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
VB 2005!!加解密尋求幫忙
價值 : 20 QP  點閱數:2601 回應數:0
樓主

甘甘
門外漢
0 1
6 0
發送站內信

1.這一段程式只能做到讀取一個 .TXT的檔案,對它做加密、簽章和解密(這是一連串的),但是我想要在它加密和簽章後可以按儲存,存成另一個.TXT檔和直接讀取加密過的 .TXT檔做解密的動作,希望大大可以幫個忙。

2.希望大大可以提供一些用VB寫的加解密書本或可參考的網頁,剛碰這些東西很多都看不懂所以需要一些資源,另外可以幫我詳細說明下面的程式碼和寫完之後的程式碼,謝謝。


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

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form 設計工具產生的程式碼 "

Public Sub New()
MyBase.New()

'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()

'在 InitializeComponent() 呼叫之後加入所有的初始設定

End Sub

'Form 覆寫 Dispose 以清除元件清單。
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

'為 Windows Form 設計工具的必要項
Private components As System.ComponentModel.IContainer

'注意: 以下為 Windows Form 設計工具所需的程序
'您可以使用 Windows Form 設計工具進行修改。
'請勿使用程式碼編輯器來修改這些程序。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
Friend WithEvents ToolStripSeparator1 As System.Windows.Forms.ToolStripSeparator
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents TSButton_open As System.Windows.Forms.ToolStripButton
Friend WithEvents TSButton_save As System.Windows.Forms.ToolStripButton
Friend WithEvents Label6 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.Label1 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button2 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Button3 = New System.Windows.Forms.Button
Me.Label4 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.ToolStripSeparator1 = New System.Windows.Forms.ToolStripSeparator
Me.TSButton_save = New System.Windows.Forms.ToolStripButton
Me.TSButton_open = New System.Windows.Forms.ToolStripButton
Me.ToolStrip1.SuspendLayout()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(162, 220)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(200, 40)
Me.Label1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(51, 428)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "加密"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(51, 57)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(311, 147)
Me.TextBox1.TabIndex = 2
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(164, 428)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 3
Me.Button2.Text = "簽章"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(162, 295)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(200, 40)
Me.Label2.TabIndex = 5
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(162, 356)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(200, 40)
Me.Label3.TabIndex = 6
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(287, 428)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(75, 23)
Me.Button3.TabIndex = 8
Me.Button3.Text = "驗證/解密"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(49, 228)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(48, 32)
Me.Label4.TabIndex = 9
Me.Label4.Text = "RSA密文"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(49, 295)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(48, 32)
Me.Label5.TabIndex = 10
Me.Label5.Text = "簽章資料"
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(49, 368)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(48, 40)
Me.Label6.TabIndex = 11
Me.Label6.Text = "驗證解密的結果"
'
'ToolStrip1
'
Me.ToolStrip1.BackColor = System.Drawing.SystemColors.InactiveCaptionText
Me.ToolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.TSButton_open, Me.TSButton_save, Me.ToolStripSeparator1})
Me.ToolStrip1.Location = New System.Drawing.Point(0, 0)
Me.ToolStrip1.Name = "ToolStrip1"
Me.ToolStrip1.Size = New System.Drawing.Size(486, 25)
Me.ToolStrip1.TabIndex = 12
Me.ToolStrip1.Text = "ToolStrip1"
'
'OpenFileDialog1
'
Me.OpenFileDialog1.FileName = "OpenFileDialog1"
'
'ToolStripSeparator1
'
Me.ToolStripSeparator1.Name = "ToolStripSeparator1"
Me.ToolStripSeparator1.Size = New System.Drawing.Size(6, 25)
'
'TSButton_save
'
Me.TSButton_save.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
Me.TSButton_save.Image = CType(resources.GetObject("TSButton_save.Image"), System.Drawing.Image)
Me.TSButton_save.ImageTransparentColor = System.Drawing.Color.Magenta
Me.TSButton_save.Name = "TSButton_save"
Me.TSButton_save.Size = New System.Drawing.Size(23, 22)
Me.TSButton_save.Text = "儲存檔案"
'
'TSButton_open
'
Me.TSButton_open.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
Me.TSButton_open.Image = CType(resources.GetObject("TSButton_open.Image"), System.Drawing.Image)
Me.TSButton_open.ImageTransparentColor = System.Drawing.Color.Magenta
Me.TSButton_open.Name = "TSButton_open"
Me.TSButton_open.Size = New System.Drawing.Size(23, 22)
Me.TSButton_open.Text = "開啟舊檔"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.BackColor = System.Drawing.SystemColors.ControlLight
Me.ClientSize = New System.Drawing.Size(486, 486)
Me.Controls.Add(Me.ToolStrip1)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ToolStrip1.ResumeLayout(False)
Me.ToolStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

#End Region
Dim myRsa As New RSACryptoServiceProvider '產生RSA的公私鑰
Dim SignRsa As New RSACryptoServiceProvider '產生簽章的公私鑰
Dim RsaPub As New RSAParameters 'RSA的公鑰
Dim RsaPri As New RSAParameters 'RSA的私鑰
Dim RsaPubSign As New RSAParameters '簽章的公鑰
Dim RsaPriSign As New RSAParameters '簽章的私鑰
Dim StringData As String
Dim ByteData() As Byte
Dim Encrypt() As Byte
Dim HashData() As Byte
Dim SignData() As Byte
Dim VerHashData() As Byte
Dim VerSignData() As Byte
Dim Decrypt() As Byte
Dim sign As Boolean


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

RsaPub = myRsa.ExportParameters(False) 'RSA的公鑰
RsaPri = myRsa.ExportParameters(True) 'RSA的私鑰
RsaPubSign = SignRsa.ExportParameters(False) '簽章的公鑰
RsaPriSign = SignRsa.ExportParameters(True) '簽章的私鑰

OpenFileDialog1.Filter = "文字檔 (*.txt)|*.txt"
SaveFileDialog1.Filter = "文字檔 (*.txt)|*.txt"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim RsaNew As New RSACryptoServiceProvider

RsaNew.ImportParameters(RsaPub) '載入RSA的公鑰
StringData = TextBox1.Text
ByteData = Encoding.Unicode.GetBytes(StringData)
Encrypt = RsaNew.Encrypt(ByteData, False) 'RSA加密
Label1.Text = Encoding.Unicode.GetString(Encrypt) '顯示RSA加密後的密文

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim rsanew As New RSACryptoServiceProvider
Dim hash As New SHA1Managed

rsanew.ImportParameters(RsaPriSign) '載入簽章的私鑰
HashData = hash.ComputeHash(Encrypt) '密文轉成hash
SignData = rsanew.SignHash(HashData, CryptoConfig.MapNameToOID("SHA1"))
Label2.Text = Encoding.Unicode.GetString(SignData) '顯示簽章後的資料

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Try
Dim rsanew As New RSACryptoServiceProvider
Dim hash As New SHA1Managed

rsanew.ImportParameters(RsaPubSign) '載入簽章的公鑰
VerHashData = hash.ComputeHash(Encrypt) '密文轉成hash

If rsanew.VerifyHash(VerHashData, CryptoConfig.MapNameToOID("SHA1"), SignData) Then '驗證簽章資料是否符合

Dim NewRsa As New RSACryptoServiceProvider

NewRsa.ImportParameters(RsaPri) '載入RSA的私鑰
Decrypt = NewRsa.Decrypt(Encrypt, False) 'RSA解密
Label3.Text = Encoding.Unicode.GetString(Decrypt) '顯示解密後的資料

Else

Label3.Text = "驗證錯誤"

End If

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

Private Sub TSButton_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSButton_open.Click
If (OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
Dim SReader As New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
TextBox1.Text = TextBox1.Text & SReader.ReadToEnd
SReader.Close()
End If
End Sub

Private Sub TSButton_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSButton_save.Click
If (SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
Dim SWriter As New StreamWriter(SaveFileDialog1.FileName, True, System.Text.Encoding.Default)
SWriter.Write(TextBox1.Text)
SWriter.Close()
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

搜尋相關Tags的文章: [ VB 2005!!加解密尋求幫忙 ] ,
本篇文章發表於2008-08-12 01:22
目前尚無任何回覆
   

回覆
如要回應,請先登入.