VB.Net Tutorial/Security/DES — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
DES Encrypt/Decrypt
Imports System.Security.Cryptography
Imports System.Text.ASCIIEncoding
Imports System.IO
Public Class Tester
Public Shared Sub Main
"Dim str As String = "1234567890asdfgh"
"must be exactly 16 characters long
Dim sSource, sKey, sIV, sResult As String
sSource = "string"
Dim objDES As New DESCryptoServiceProvider()
Dim objStream As New MemoryStream(ASCII.GetBytes(sSource), False)
sKey = "12345678"
sIV = "12345678"
objDES.IV = ASCII.GetBytes(sIV)
objDES.Key = ASCII.GetBytes(sKey)
sResult = ASCII.GetChars(objDES.CreateEncryptor().TransformFinalBlock(ASCII.GetBytes(sSource), 0, sSource.Length))
Console.WriteLine(sResult)
"sResult = ASCII.GetChars(objDES.CreateDecryptor().TransformFinalBlock(ASCII.GetBytes(sSource), 0, sSource.Length))
"Console.WriteLine(sResult)
End Sub
End Class
Use DES to Decrypt a file
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Try
Dim myDESProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider()
myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("12345678")
myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("12345678")
Dim DecryptedFile As FileStream = New FileStream("testDes.txt", FileMode.Open, FileAccess.Read)
Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateDecryptor(myDESProvider.Key, myDESProvider.IV)
Dim myCryptoStream As CryptoStream = New CryptoStream(DecryptedFile, myICryptoTransform, CryptoStreamMode.Read)
Dim myDecStreamReader As New StreamReader(myCryptoStream)
Dim myDecStreamWriter As New StreamWriter("test.txt")
myDecStreamWriter.Write(myDecStreamReader.ReadToEnd())
myCryptoStream.Close()
myDecStreamReader.Close()
myDecStreamWriter.Close()
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub
End Class
Use DES to encrypt a file
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Try
Dim myDESProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider()
myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("12345678")
myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("12345678")
Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateEncryptor(myDESProvider.Key, myDESProvider.IV)
Dim ProcessFileStream As FileStream = New FileStream("test.txt", FileMode.Open, FileAccess.Read)
Dim ResultFileStream As FileStream = New FileStream("testDes.txt", FileMode.Create, FileAccess.Write)
Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.Close()
ProcessFileStream.Close()
ResultFileStream.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Class