📄 mainform.vb
字号:
Imports system.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class MainForm
#Region "Step 1 - Creating Asymmetric Keys"
Private Sub BtnCreateAsmmetricKeys_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnCreateAsmmetricKeys.Click
CreateAsymmetricKey(Me.TxtPrivateKey1, Me.TxtPublicKey1)
End Sub
Private Sub BtnCreateAsmmetricKeys2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnCreateAsmmetricKeys2.Click
CreateAsymmetricKey(Me.TxtPrivateKey2, Me.TxtPublicKey2)
End Sub
Private Sub CreateAsymmetricKey(ByVal txtPrivate As TextBox, _
ByVal txtPublic As TextBox)
Dim RSA As New RSACryptoServiceProvider()
txtPrivate.Text = RSA.ToXmlString(True)
txtPublic.Text = RSA.ToXmlString(False)
End Sub
#End Region
#Region "Step 2 - Creating Symmetric Keys"
Private Sub BtnCreateSymmetric_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnCreateSymmetric.Click
Dim TDES As New TripleDESCryptoServiceProvider()
TDES.GenerateIV()
TDES.GenerateKey()
Me.TxtSymmetricIV.Text = Convert.ToBase64String(TDES.IV)
Me.TxtSymmetricKey.Text = Convert.ToBase64String(TDES.Key)
End Sub
#End Region
#Region "Step 3 - Encrypt, Hash and Sign Symmetric Key"
Private Sub BtnEncryptKey_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnEncryptKey.Click
EncryptSymmetricKey()
Me.TxtHashValue.Text = Convert.ToBase64String _
(CreateSymmetricKeyHash(Me.TxtEncryptedKey.Text))
SignSymmetricKeyHash()
End Sub
Private Sub EncryptSymmetricKey()
Dim iv, key As Byte()
Dim encryptedIV, encryptedkey As Byte()
iv = Convert.FromBase64String(Me.TxtSymmetricIV.Text)
key = Convert.FromBase64String(Me.TxtSymmetricKey.Text)
'Load the RSACryptoServiceProvider class using
'only the public key
Dim RSA As New RSACryptoServiceProvider()
RSA.FromXmlString(Me.TxtPublicKey1.Text)
'Encrypt the Symmetric Key
encryptedIV = RSA.Encrypt(iv, False)
encryptedkey = RSA.Encrypt(key, False)
'Create a single byte array containing both the IV and Key
'so that we only need to encrypt and distribute a single value
Dim keyOutput(2 * 4 - 1 + encryptedIV.Length + encryptedkey.Length) As Byte
Array.Copy(BitConverter.GetBytes(encryptedIV.Length), 0, keyOutput, 0, 4)
Array.Copy(BitConverter.GetBytes(encryptedkey.Length), 0, keyOutput, 4, 4)
Array.Copy(encryptedIV, 0, keyOutput, 8, encryptedIV.Length)
Array.Copy(encryptedkey, 0, keyOutput, 8 + encryptedIV.Length, _
encryptedkey.Length)
Me.TxtEncryptedKey.Text = Convert.ToBase64String(keyOutput)
End Sub
Private Function CreateSymmetricKeyHash(ByVal inputString As String) As Byte()
'Retrieve the bytes for this string
Dim UE As New UnicodeEncoding()
Dim MessageBytes As Byte() = UE.GetBytes(inputString)
'Use the SHA1Managed provider to hash the input string
Dim SHhash As New SHA1Managed()
Return SHhash.ComputeHash(MessageBytes)
End Function
Private Sub SignSymmetricKeyHash()
'The value to hold the signed value.
Dim SignedHashValue() As Byte
'Load the RSACryptoServiceProvider using the
'private key as we will be signing
Dim RSA As New RSACryptoServiceProvider
RSA.FromXmlString(Me.TxtPrivateKey2.Text)
'Create the signature formatter and generate the signature
Dim RSAFormatter As New RSAPKCS1SignatureFormatter(RSA)
RSAFormatter.SetHashAlgorithm("SHA1")
SignedHashValue = RSAFormatter.CreateSignature _
(Convert.FromBase64String(Me.TxtHashValue.Text))
Me.TxtSymmetricSignature.Text = Convert.ToBase64String(SignedHashValue)
End Sub
#End Region
#Region "Step 4 - Transfer and Validate Key Information"
Private Sub BtnRetrieveKeyInfo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnRetrieveKeyInfo.Click
Me.TxtRetrievedKey.Text = Me.TxtEncryptedKey.Text
Me.TxtRetrievedSignature.Text = Me.TxtSymmetricSignature.Text
Me.TxtRetrievedPublicKey.Text = Me.TxtPublicKey2.Text
End Sub
Private Sub BtnValidate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnValidate.Click
'Create the expected hash from the retrived public key
Dim HashValue, SignedHashValue As Byte()
HashValue = CreateSymmetricKeyHash(Me.TxtRetrievedKey.Text)
'Generate the expected signature
Dim RSA As New RSACryptoServiceProvider()
RSA.FromXmlString(Me.TxtRetrievedPublicKey.Text)
Dim RSADeformatter As New RSAPKCS1SignatureDeformatter(RSA)
RSADeformatter.SetHashAlgorithm("SHA1")
SignedHashValue = Convert.FromBase64String(Me.TxtRetrievedSignature.Text)
'Validate against received signature
If RSADeformatter.VerifySignature(HashValue, SignedHashValue) Then
Me.TxtRetrievedKey.BackColor = Color.Green
Else
Me.TxtRetrievedKey.BackColor = Color.Red
End If
End Sub
#End Region
#Region "Step 5 - Decrypt Symmetric key"
Private Sub BtnDecryptKeyInformation_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnDecryptKeyInformation.Click
Dim iv, key As Byte()
'Retrieve the iv and key arrays from the single array
Dim keyOutput As Byte() = Convert.FromBase64String(Me.TxtRetrievedKey.Text)
ReDim iv(BitConverter.ToInt32(keyOutput, 0) - 1)
ReDim key(BitConverter.ToInt32(keyOutput, 4) - 1)
Array.Copy(keyOutput, 8, iv, 0, iv.Length)
Array.Copy(keyOutput, 8 + iv.Length, key, 0, key.Length)
'Load the RSACryptoServiceProvider class using Susan's private key
Dim RSA As New RSACryptoServiceProvider()
RSA.FromXmlString(Me.TxtPrivateKey1.Text)
'Decrypt the symmetric key and IV.
Me.TxtDecryptedIV.Text = Convert.ToBase64String(RSA.Decrypt(iv, False))
Me.TxtDecryptedKey.Text = Convert.ToBase64String(RSA.Decrypt(key, False))
End Sub
#End Region
#Region "Step 6 - Sending a Message"
Private Sub btnSendAToB_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnSendAToB.Click
Me.TxtMessageAEncrypted.Text = EncryptData(Me.TxtMessageA.Text, _
Me.TxtDecryptedIV.Text, _
Me.TxtDecryptedKey.Text)
End Sub
Private Sub BtnSendBToA_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BtnSendBToA.Click
Me.TxtMessageBEncrypted.Text = EncryptData(Me.TxtMessageB.Text, _
Me.TxtSymmetricIV.Text, _
Me.TxtSymmetricKey.Text)
End Sub
Private Function EncryptData(ByVal data As String, ByVal iv As String, _
ByVal key As String) As String
Dim KeyBytes As Byte() = Convert.FromBase64String(key)
Dim IVBytes As Byte() = Convert.FromBase64String(iv)
'Create the output stream
Dim strm As New IO.MemoryStream
'Create the TripleDES class to do the encryption
Dim Triple As New TripleDESCryptoServiceProvider()
'Create a CryptoStream with the output stream and encryption algorithm
Dim CryptStream As New CryptoStream(strm, _
Triple.CreateEncryptor(KeyBytes, IVBytes), _
CryptoStreamMode.Write)
'Write the text to be encrypted
Dim SWriter As New StreamWriter(CryptStream)
SWriter.WriteLine(data)
SWriter.Close()
Return Convert.ToBase64String(strm.ToArray)
End Function
#End Region
#Region "Step 7 - Receiving a Message"
Private Sub TxtMessageAEncrypted_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TxtMessageAEncrypted.TextChanged
Me.TxtReceivedMessageFromA.Text = DecryptData( _
Me.TxtMessageAEncrypted.Text, _
Me.TxtSymmetricIV.Text, _
Me.TxtSymmetricKey.Text)
End Sub
Private Sub TxtMessageBEncrypted_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TxtMessageBEncrypted.TextChanged
Me.TxtReceivedMessageFromB.Text = DecryptData( _
Me.TxtMessageBEncrypted.Text, _
Me.TxtDecryptedIV.Text, _
Me.TxtDecryptedKey.Text)
End Sub
Private Function DecryptData(ByVal data As String, ByVal iv As String, _
ByVal key As String) As String
Dim KeyBytes As Byte() = Convert.FromBase64String(key)
Dim IVBytes As Byte() = Convert.FromBase64String(iv)
'Create the input stream from the encrypted data
Dim strm As New IO.MemoryStream(Convert.FromBase64String(data))
'Create the TripleDES class to do the decryption
Dim Triple As New TripleDESCryptoServiceProvider()
'Create a CryptoStream with the input stream and decryption algorithm
Dim CryptStream As New CryptoStream(strm, _
Triple.CreateDecryptor(KeyBytes, IVBytes), _
CryptoStreamMode.Read)
'Read the stream.
Dim SReader As New StreamReader(CryptStream)
Return SReader.ReadToEnd
End Function
#End Region
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -