当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET RSACryptoServiceProvider类代码示例

本文整理汇总了VB.NET中System.Security.Cryptography.RSACryptoServiceProvider的典型用法代码示例。如果您正苦于以下问题:VB.NET RSACryptoServiceProvider类的具体用法?VB.NET RSACryptoServiceProvider怎么用?VB.NET RSACryptoServiceProvider使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了RSACryptoServiceProvider类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: RSACSPSample

' 导入命名空间
Imports System.Security.Cryptography
Imports System.Text

 _

Class RSACSPSample


    Shared Sub Main()
        Try
            'Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New UnicodeEncoding()

            'Create byte arrays to hold original, encrypted, and decrypted data.
            Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to Encrypt")
            Dim encryptedData() As Byte
            Dim decryptedData() As Byte

            'Create a new instance of RSACryptoServiceProvider to generate
            'public and private key data.
            Using RSA As New RSACryptoServiceProvider

                'Pass the data to ENCRYPT, the public key information 
                '(using RSACryptoServiceProvider.ExportParameters(false),
                'and a boolean flag specifying no OAEP padding.
                encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(False), False)

                'Pass the data to DECRYPT, the private key information 
                '(using RSACryptoServiceProvider.ExportParameters(true),
                'and a boolean flag specifying no OAEP padding.
                decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(True), False)

                'Display the decrypted plaintext to the console. 
                Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData))
            End Using
        Catch e As ArgumentNullException
            'Catch this exception in case the encryption did
            'not succeed.
            Console.WriteLine("Encryption failed.")
        End Try
    End Sub


    Public Shared Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim encryptedData() As Byte
            'Create a new instance of RSACryptoServiceProvider.
            Using RSA As New RSACryptoServiceProvider

                'Import the RSA Key information. This only needs
                'toinclude the public key information.
                RSA.ImportParameters(RSAKeyInfo)

                'Encrypt the passed byte array and specify OAEP padding.  
                'OAEP padding is only available on Microsoft Windows XP or
                'later.  
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding)
            End Using
            Return encryptedData
            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Public Shared Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim decryptedData() As Byte
            'Create a new instance of RSACryptoServiceProvider.
            Using RSA As New RSACryptoServiceProvider
                'Import the RSA Key information. This needs
                'to include the private key information.
                RSA.ImportParameters(RSAKeyInfo)

                'Decrypt the passed byte array and specify OAEP padding.  
                'OAEP padding is only available on Microsoft Windows XP or
                'later.  
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding)
                'Catch and display a CryptographicException  
                'to the console.
            End Using
            Return decryptedData
        Catch e As CryptographicException
            Console.WriteLine(e.ToString())

            Return Nothing
        End Try
    End Function
End Class
开发者ID:VB.NET开发者,项目名称:System.Security.Cryptography,代码行数:94,代码来源:RSACryptoServiceProvider

示例2: RSACryptoServiceProvider

Try

    'Create a new RSACryptoServiceProvider object. 
    Dim RSA As New RSACryptoServiceProvider()

    'Export the key information to an RSAParameters object.
    'Pass false to export the public key information or pass
    'true to export public and private key information.
    Dim RSAParams As RSAParameters = RSA.ExportParameters(False)


Catch e As CryptographicException
    'Catch this exception in case the encryption did
    'not succeed.
    Console.WriteLine(e.Message)
End Try
开发者ID:VB.NET开发者,项目名称:System.Security.Cryptography,代码行数:16,代码来源:RSACryptoServiceProvider

示例3: New RSACryptoServiceProvider

' 导入命名空间
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO


Public Class Tester
    Public Shared Sub Main

        Dim myRSAProvide As New RSACryptoServiceProvider()

        Dim strCrypt As String
        Dim strResult As String
        Dim bteCrypt() As Byte
        Dim bteResult() As Byte
        Try
            strCrypt = "12345678"
            bteCrypt = Encoding.ASCII.GetBytes(strCrypt)
            bteResult = myRSAProvide.Encrypt(bteCrypt, False)
            Console.WriteLine(Encoding.ASCII.GetString(bteResult))

        Catch ex As CryptographicException
            Console.WriteLine(ex.Message)
        End Try

        Dim strResault As String
        Dim bteDecrypt() As Byte
        Try
            bteDecrypt = myRSAProvide.Decrypt(bteResult, False)
            strResault = Encoding.ASCII.GetString(bteDecrypt)
            Console.WriteLine(strResault)
        Catch ex As CryptographicException
            Console.WriteLine(ex.Message)
        End Try

    End Sub
End Class
开发者ID:VB程序员,项目名称:System.Security.Cryptography,代码行数:38,代码来源:RSACryptoServiceProvider


注:本文中的System.Security.Cryptography.RSACryptoServiceProvider类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。