溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

VB.NET加密文件的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2021-07-20 00:21:29 來(lái)源:億速云 閱讀:578 作者:chen 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“VB.NET加密文件的實(shí)現(xiàn)方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“VB.NET加密文件的實(shí)現(xiàn)方法”吧!

VB.NET編程環(huán)境可以幫助開(kāi)發(fā)人員打造一個(gè)性能穩(wěn)定,安全可靠的編程平臺(tái),幫助開(kāi)發(fā)人員大大提高編程效率。本文介紹使用XOR加密算法對(duì)數(shù)據(jù)進(jìn)行加密, 這是一種很簡(jiǎn)單的算法,使用了盡量簡(jiǎn)單的VB編程方法,通俗易懂。我們可以采用更安全的算法如DES算法,IDEA算法等。

在窗體中添加一RichTextBox,設(shè)置其N(xiāo)ame屬性為SourceFile,Multiline屬性為T(mén)rue,ScrollBars屬性為3-both。添加一MainMenu,設(shè)置一菜單項(xiàng)“文件”,其下有“打開(kāi)”,“保存”,“加密”,“解密”,“算子”等子菜單項(xiàng)。

以下是大致的VB.NET加密文件的程序界面:

  1. Public Class Form1   

  2. Inherits System.Windows.Forms.Form   

  3. Dim strNum As String   

  4. Private Sub Form1_Load(ByVal eventSender
     As System.Object, ByVal eventArgs 
    As System.EventArgs) Handles MyBase.Load   

  5. strNum = "password"   

  6. End Sub   

  7. Private Sub Form1_Resize(ByVal event
    Sender As System.Object, ByVal eventArgs
     As System.EventArgs) Handles MyBase.Resize   

  8. SourceFile.Width = Form1.DefInstance.Width   

  9. SourceFile.Height = Form1.DefInstance.Height   

  10. End Sub  

解密過(guò)程,對(duì)xor算法而言,解密和VB.NET加密文件的過(guò)程是完全一樣的

  1. Private Function XorOut(ByRef 
    strNum As String, ByRef strText 
    As String) As String   

  2. Dim i As Integer   

  3. Dim XorValue1 As Short   

  4. Dim XorValue2 As Short   

  5. Dim strResult As String   

  6. For i = 1 To Len(strText)   

  7. XorValue1 = Asc(Mid(strText, i, 1))   

  8. XorValue2 = Asc(Mid(strNum, 
    (i Mod Len(strNum)) + 1, 1))   

  9. strResultstrResult = strResult & 
    Chr(XorValue1 Xor XorValue2)   

  10. Next   

  11. XorOut = strResult   

  12. End Function   

  13.  

  14. Private Sub MenuDeEncrypt_Click
    (ByVal sender As System.Object, 
    ByVal e As System.EventArgs) 
    Handles MenuDeEncrypt.Click   

  15. SourceFile.Text = XorOut(strNum, 
    (SourceFile.Text))   

  16. End Sub   

  17.  

  18. Private Sub MenuEncrypt_Click
    (ByVal sender As System.Object,
     ByVal e As System.EventArgs) 
    Handles MenuEncrypt.Click   

  19. SourceFile.Text = XorOut(strNum,
     (SourceFile.Text))   

  20. End Sub   

  21.  

  22. Private Sub MenuOpen_Click(ByVal 
    sender As System.Object, ByVal e
     As System.EventArgs) Handles 
    MenuOpen.Click   

  23. Dim openFile1 As New OpenFileDialog   

  24. ' Determine whether the user 
    selected a file from the OpenFileDialog.   

  25. If (openFile1.ShowDialog() = 
    DialogResult.OK) _   

  26. And (openFile1.FileName.Length > 0) Then   

  27.  

  28. ' Load the contents of the 
    file into the RichTextBox.   

  29. SourceFile.LoadFile(openFile1.FileName, _   

  30. RichTextBoxStreamType.PlainText)   

  31. End If   

  32. End Sub   

  33.  

  34. Private Sub MenuSave_Click(ByVal 
    sender As System.Object, ByVal e As
     System.EventArgs) Handles MenuSave.Click   

  35. ' Create a SaveFileDialog to request 
    a path and file name to save to.   

  36. Dim saveFile1 As New SaveFileDialog   

  37. ' Initialize the SaveFileDialog to 
    specify the RTF extension for the file.   

  38. 'saveFile1.DefaultExt = "*.rtf"   

  39. 'saveFile1.Filter = "RTF Files|*.rtf"   

  40. ' Determine if the user selected a 
    file name from the saveFileDialog.   

  41. If (saveFile1.ShowDialog() = DialogResult.OK) _   

  42. And (saveFile1.FileName.Length) > 0 Then   

  43. ' Save the contents of the 
    RichTextBox into the file.   

  44. SourceFile.SaveFile(saveFile1.FileName, _   

  45. RichTextBoxStreamType.PlainText)   

  46. End If   

  47. End Sub   

  48. Private Sub MenuNum_Click(ByVal 
    sender As System.Object, ByVal e 
    As System.EventArgs) Handles MenuNum.Click   

  49. strNum = InputBox("請(qǐng)輸入加密算子",
     "設(shè)置加密算子")   

  50. End Sub   

  51. End Class  

到此,相信大家對(duì)“VB.NET加密文件的實(shí)現(xiàn)方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI