📄 form1.vb
字号:
Imports System.IO
Imports System.Security.Cryptography
Imports Microsoft.Practices.Mobile.PasswordAuthentication
Public Class Form1
Private encryptionKey() As Byte
Private Sub menuItemSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemSave.Click
' Ask user for a password from which we can derive a key
encryptionKey = GetEncryptionKey()
' Encrypt the data
Dim encryptedData As String = EncryptData(encryptionKey, textBox1.Text)
' Create the output file
Using sw As StreamWriter = New StreamWriter("crypt.bin", False)
sw.Write(encryptedData)
End Using
' Display
textBox1.Text = encryptedData
End Sub
Private Function GetEncryptionKey() As Byte()
Dim password As String
Using frm As GetPasswordForm = New GetPasswordForm()
frm.ShowDialog()
password = frm.Password
End Using
Return DeriveKeyFromPassword(password)
End Function
Private Function DeriveKeyFromPassword(ByVal password As String) As Byte()
Dim key() As Byte
Using provider As RsaAesCryptographyProvider = New RsaAesCryptographyProvider("DevelopersHandbook")
Dim crypto As CryptNativeHelper = New CryptNativeHelper(provider)
key = crypto.GetPasswordDerivedKey(password)
End Using
Return key
End Function
Private Function EncryptData(ByVal key() As Byte, ByVal plainText As String) As String
' Get the bytes to encrypt
Dim plaintextByte() As Byte = System.Text.Encoding.Unicode.GetBytes(plainText)
' Create a Rijndael instance - automatically generates symmetric key
' and an initialization vector
Dim rijndael As RijndaelManaged = New RijndaelManaged()
' Set encryption mode
rijndael.Mode = CipherMode.ECB
rijndael.Padding = PaddingMode.PKCS7
' Create a random initialization vector
rijndael.GenerateIV()
Dim iv() As Byte = rijndael.IV
Dim encodedText As String = ""
' Define memory stream which will be used to hold encrypted data.
Dim memStrm As MemoryStream = New MemoryStream()
' Write the IV length and the IV
memStrm.Write(BitConverter.GetBytes(iv.Length), 0, 4)
memStrm.Write(iv, 0, iv.Length)
' Create a symmetric encryptor
Using encryptor As ICryptoTransform = rijndael.CreateEncryptor(key, iv)
' Create a CryptStream to write to the output file
Dim cryptStrm As CryptoStream = New CryptoStream(memStrm, encryptor, CryptoStreamMode.Write)
' Write the content to be encrypted
cryptStrm.Write(plaintextByte, 0, plaintextByte.Length)
cryptStrm.FlushFinalBlock()
' Convert encrypted data from a memory stream into a byte array.
Dim cipherTextBytes() As Byte = memStrm.ToArray()
' Close the treams
memStrm.Close()
cryptStrm.Close()
' Convert encrypted data into a base64-encoded string.
encodedText = Convert.ToBase64String(cipherTextBytes)
End Using
Return encodedText
End Function
Private Function DecryptData(ByVal key() As Byte, ByVal encryptedData As String) As String
Dim retStr As String = ""
' Create a symmetric decryptor
Dim rijndael As RijndaelManaged = New RijndaelManaged()
rijndael.Mode = CipherMode.ECB
rijndael.Padding = PaddingMode.PKCS7
' Convert our ciphertext into a byte array.
Dim cipherTextBytes() As Byte = Convert.FromBase64String(encryptedData)
' Define memory stream to use to read encrypted data.
Dim inStream As MemoryStream = New MemoryStream(cipherTextBytes)
' Read the IV length from the buffer
Dim ivLength As Integer = BitConverter.ToInt32(cipherTextBytes, 0)
' Reposition to after 'length' bytes in stream
inStream.Position = 4
' Read the IV from the input stream
Dim iv() As Byte = New Byte(ivLength) {}
inStream.Read(iv, 0, ivLength)
Using decryptor As ICryptoTransform = rijndael.CreateDecryptor(key, iv)
' Create a CryptStream to read from the file
Dim cryptStrm As CryptoStream = New CryptoStream(inStream, decryptor, CryptoStreamMode.Read)
' Create another MemoryStream for the output
Dim memStrm As MemoryStream = New MemoryStream()
Dim buffer() As Byte = New Byte(2048) {}
Dim totalbytes As Integer = 0
Do
Dim bytesRead As Integer = cryptStrm.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit Do
End If
memStrm.Write(buffer, 0, bytesRead)
totalbytes += bytesRead
Loop
' Write the content to be encrypted
memStrm.Flush()
memStrm.Seek(0, SeekOrigin.Begin)
' Get the string from the bytes we read
retStr = System.Text.Encoding.Unicode.GetString(memStrm.GetBuffer(), 0, totalbytes)
cryptStrm.Close()
End Using
Return retStr
End Function
Private Sub menuItemRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemRestore.Click
' Ask user for a password from which we can derive the key
encryptionKey = GetEncryptionKey()
' Read encrypted data from the file
Dim fileContents As String
Using sr As StreamReader = New StreamReader("crypt.bin")
fileContents = sr.ReadToEnd()
End Using
' Write decrypted data back into the TextBox
textBox1.Text = DecryptData(encryptionKey, fileContents)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -