本文整理汇总了VB.NET中System.Security.Cryptography.CryptoStream类的典型用法代码示例。如果您正苦于以下问题:VB.NET CryptoStream类的具体用法?VB.NET CryptoStream怎么用?VB.NET CryptoStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CryptoStream类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: RijndaelExample
' 导入命名空间
Imports System.IO
Imports System.Security.Cryptography
Class RijndaelExample
Public Shared Sub Main()
Try
Dim original As String = "Here is some data to encrypt!"
' Create a new instance of the Rijndael
' class. This generates a new key and initialization
' vector (IV).
Using myRijndael = Rijndael.Create()
' Encrypt the string to an array of bytes.
Dim encrypted As Byte() = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV)
' Decrypt the bytes to a string.
Dim roundtrip As String = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV)
'Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original)
Console.WriteLine("Round Trip: {0}", roundtrip)
End Using
Catch e As Exception
Console.WriteLine("Error: {0}", e.Message)
End Try
End Sub
Shared Function EncryptStringToBytes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
' Check arguments.
If plainText Is Nothing OrElse plainText.Length <= 0 Then
Throw New ArgumentNullException("plainText")
End If
If Key Is Nothing OrElse Key.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
If IV Is Nothing OrElse IV.Length <= 0 Then
Throw New ArgumentNullException("IV")
End If
Dim encrypted() As Byte
' Create an Rijndael object
' with the specified key and IV.
Using rijAlg = Rijndael.Create()
rijAlg.Key = Key
rijAlg.IV = IV
' Create an encryptor to perform the stream transform.
Dim encryptor As ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV)
' Create the streams used for encryption.
Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
Using swEncrypt As New StreamWriter(csEncrypt)
'Write all data to the stream.
swEncrypt.Write(plainText)
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
End Using
' Return the encrypted bytes from the memory stream.
Return encrypted
End Function 'EncryptStringToBytes
Shared Function DecryptStringFromBytes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
' Check arguments.
If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
Throw New ArgumentNullException("cipherText")
End If
If Key Is Nothing OrElse Key.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
If IV Is Nothing OrElse IV.Length <= 0 Then
Throw New ArgumentNullException("IV")
End If
' Declare the string used to hold
' the decrypted text.
Dim plaintext As String = Nothing
' Create an Rijndael object
' with the specified key and IV.
Using rijAlg = Rijndael.Create()
rijAlg.Key = Key
rijAlg.IV = IV
' Create a decryptor to perform the stream transform.
Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV)
' Create the streams used for decryption.
Using msDecrypt As New MemoryStream(cipherText)
Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Using srDecrypt As New StreamReader(csDecrypt)
' Read the decrypted bytes from the decrypting stream
' and place them in a string.
plaintext = srDecrypt.ReadToEnd()
End Using
End Using
End Using
End Using
Return plaintext
End Function 'DecryptStringFromBytes
End Class
开发者ID:VB.NET开发者,项目名称:System.Security.Cryptography,代码行数:117,代码来源:CryptoStream
示例2: New CryptoStream
' 导入命名空间
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Data.OleDb
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Data
Imports System.Security
Imports System.Security.Cryptography
Public Class MainClass
Shared Sub Main()
Dim fs As FileStream = New FileStream("DSencrypted.dat",FileMode.Create, FileAccess.Write)
Dim MyDS As DataSet = New DataSet()
Dim MyDS2 As DataSet = New DataSet()
Dim Connection As SqlConnection = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Connection.Open()
Dim MyDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Employee", Connection)
MyDA.Fill(MyDS, "Employee")
Dim DES As DESCryptoServiceProvider = New DESCryptoServiceProvider()
Dim DESencrypter As ICryptoTransform = DES.CreateEncryptor()
Dim cryptStream As CryptoStream = New CryptoStream(fs, DESencrypter, CryptoStreamMode.Write)
MyDS.WriteXml(cryptStream, XmlWriteMode.WriteSchema)
cryptStream.Close()
Dim fsRead As FileStream = New FileStream("DSencrypted.dat", _
FileMode.Open, FileAccess.Read)
Dim DESdecrypter As ICryptoTransform = DES.CreateDecryptor()
Dim decryptStream As CryptoStream = New CryptoStream(fsRead, _
DESdecrypter, CryptoStreamMode.Read)
Dim plainStreamR As XmlTextReader = New XmlTextReader(decryptStream)
MyDS2.ReadXml(plainStreamR, XmlReadMode.ReadSchema)
End Sub
End Class
开发者ID:VB程序员,项目名称:System.Security.Cryptography,代码行数:43,代码来源:CryptoStream
注:本文中的System.Security.Cryptography.CryptoStream类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论