⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encryptpasswordrsa.vb

📁 Microsoft Mobile Development Handbook的代码,有C#,VB,C++的
💻 VB
字号:
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Imports System.Xml

Class EncryptPasswordRSA
    Public Sub SavePassword(ByVal cleartext As String)
        Dim rsaProvider As RSACryptoServiceProvider = New RSACryptoServiceProvider()
        'Create a new instance of RSAParameters.
        Dim RSAKeyInfo As RSAParameters = New RSAParameters()

        RSAKeyInfo = ReadPublicKeyXML(RSAKeyInfo)

        'Import key parameters into RSA.
        rsaProvider.ImportParameters(RSAKeyInfo)

        'Encrypt the supplied data
        Dim cipherText() As Byte = rsaProvider.Encrypt(New UnicodeEncoding().GetBytes(cleartext), False) 'Save the Cipher-TextImports (System.IO.StreamWriter sw =Shadows System.IO.StreamWriter(GetApplicationDirectory() Function "\UserKeyCipher.txt",False))() As +sw.Write(Convert.ToBase64String(cipherText))sw.Flush()End FunctionEnd Sub Private Function ReadPublicKeyXML(ByVal RSAKeyInfo As RSAParameters) As RSAParameters' Read the public key from the XML file with a reader.Imports (Stream stm = File.OpenRead(GetApplicationDirectory() + "\PublicKey.xml")){Dim reader As XmlTextReader =  New XmlTextReader(stm)' Do performant ref comparisonDim ev As String =  reader.NameTable.Add("RSAKeyValue")While reader.Read()If reader.LocalName = ev Then' Process it!Dim eventDepth As Integer =  reader.Depthreader.Read()While reader.Depth > eventDepthIf reader.MoveToContent() = XmlNodeType.Element ThenSelect Case reader.NameCase "Modulus"RSAKeyInfo.Modulus = Convert.FromBase64String(reader.ReadString())breakCase "Exponent"RSAKeyInfo.Exponent = Convert.FromBase64String(reader.ReadString())breakEnd SelectEnd Ifreader.Read()End WhileEnd IfEnd While}

        'Save the Cipher-Text
        Using sw As StreamWriter = New StreamWriter(GetApplicationDirectory() + "\UserKeyCipher.txt", False)
            sw.Write(Convert.ToBase64String(cipherText))
            sw.Flush()
        End Using

    End Sub

    Private Function ReadPublicKeyXML(ByVal RSAKeyInfo As RSAParameters) As RSAParameters
        ' Read the public key from the XML file with a reader.
        Using stm As Stream = File.OpenRead(GetApplicationDirectory() + "\PublicKey.xml")
            Dim reader As XmlTextReader = New XmlTextReader(stm)
            ' Do performant ref comparison
            Dim ev As String = reader.NameTable.Add("RSAKeyValue")
            While reader.Read()
                If reader.LocalName = ev Then
                    ' Process it!
                    Dim eventDepth As Integer = reader.Depth
                    reader.Read()
                    While reader.Depth > eventDepth
                        If reader.MoveToContent() = XmlNodeType.Element Then
                            Select Case reader.Name
                                Case "Modulus"
                                    RSAKeyInfo.Modulus = Convert.FromBase64String(reader.ReadString())
                                    Exit While
                                Case "Exponent"
                                    RSAKeyInfo.Exponent = Convert.FromBase64String(reader.ReadString())
                                    Exit While
                            End Select
                        End If
                        reader.Read()
                    End While
                End If
            End While
        End Using

        Return RSAKeyInfo
    End Function

    Private Function GetApplicationDirectory() As String
        Return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()(0).FullyQualifiedName)
    End Function
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -