01. juni 2005 - 11:52Der er
18 kommentarer og 1 løsning
Encryption og decryption med symmetriske keys
Hej,
Jeg sidder og læser bogen "Implementing Security for Applications" som er et MCAD/MCSD training kit...
Der er et stykke som Encryption og decryption med symmetriske keys, hvor der er et exsempel med kryptering af noget tekst (8-16, hvis nogen har bogen).
Hvad der tilsyneladende ikke står, er hvordan man gemmer key'en, men det har jeg selv forsøgt efter bedste evne, og jeg tror det er lykkes.
Mit problem er at jeg nu vil decrypte, og bruge den gemte key. Bogen fortæller kun kort hvad jeg skal ændre ved encrypt-metoden for at kunne decrypte, men jeg får heletiden fejl:
Stream was not readable. Parameter name: stream
Line 23: Dim encryptStream As New CryptoStream(encryptedFile, encryptor, CryptoStreamMode.Read)
_____________________________________
Min kode (EncryptData er metoden fra bogen, resten er hjemmelavede):
Public Sub DecryptData(ByVal unencryptedFileName As String, ByVal encryptedFileName As String)
' Lav stream-objekter Dim unencryptedFile As New FileStream(unencryptedFileName, FileMode.Open, FileAccess.Read) Dim encryptedFile As New FileStream(encryptedFileName, FileMode.OpenOrCreate, FileAccess.Write)
' Lav SymmetricAlgorithm + key Dim alg As SymmetricAlgorithm = New RijndaelManaged alg.Key = Me.ReadKey(unencryptedFileName & ".xkey")
' Læs den ukrypterede fil ind i fileData Dim fileData(CType(unencryptedFile.Length - 1, Integer)) As Byte unencryptedFile.Read(fileData, 0, CType(unencryptedFile.Length, Integer))
' Lav encryptor + cryptostream Dim encryptor As ICryptoTransform = alg.CreateDecryptor Dim encryptStream As New CryptoStream(encryptedFile, encryptor, CryptoStreamMode.Read)
' Skriv til cryptostream encryptStream.Read(fileData, 0, CType(unencryptedFile.Length, Integer))
' Ryd op encryptStream.Close() encryptedFile.Close() unencryptedFile.Close()
End Sub
Public Sub EncryptData(ByVal unencryptedFileName As String, ByVal encryptedFileName As String)
' Lav stream-objekter Dim unencryptedFile As New FileStream(unencryptedFileName, FileMode.Open, FileAccess.Read) Dim encryptedFile As New FileStream(encryptedFileName, FileMode.OpenOrCreate, FileAccess.Write)
' Lav SymmetricAlgorithm + key Dim alg As SymmetricAlgorithm = New RijndaelManaged alg.GenerateKey()
' Læs den ukrypterede fil ind i fileData Dim fileData(CType(unencryptedFile.Length - 1, Integer)) As Byte unencryptedFile.Read(fileData, 0, CType(unencryptedFile.Length, Integer))
' Lav encryptor + cryptostream Dim encryptor As ICryptoTransform = alg.CreateEncryptor Dim encryptStream As New CryptoStream(encryptedFile, encryptor, CryptoStreamMode.Write)
' Skriv til cryptostream encryptStream.Write(fileData, 0, CType(unencryptedFile.Length, Integer))
' Ryd op encryptStream.Close() encryptedFile.Close() unencryptedFile.Close()
End Sub
Private Sub SaveKey(ByVal key As Byte(), ByVal newFileName As String) Dim keyStream As New FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.Write)
keyStream.Write(key, 0, key.Length) keyStream.Close() End Sub
Private Function ReadKey(ByVal keyFileName As String) As Byte() Dim keyStream As New FileStream(keyFileName, FileMode.Open, FileAccess.Read) Dim fileData(CType(keyStream.Length - 1, Integer)) As Byte
Ja, det er vist lidt forvirrende med navnene nu... Jeg har ikke lige renamet dem fordi der stod i bogen at jeg kun behøvede at ændre nogle få ting:
- Læs fra key istedet for at generere en - Brug CreateDecryptor istedet for CreateEncryptor - CryptoStreamMode.Read istedet for CryptoStreamMode.Write - Læse fra CryptoStream istedet for at skrive til CryptoStream
Lige for at vi er på samme side... Nu har jeg skrevet: Dim encryptStream As New CryptoStream(unencryptedFile, encryptor, CryptoStreamMode.Read)
Nu får jeg ingen fejl, men txt-filen som skal indeholde det dekrypterede er blank...
Public Sub DecryptData(ByVal unencryptedFileName As String, ByVal encryptedFileName As String)
' Lav stream-objekter Dim unencryptedFile As New FileStream(unencryptedFileName, FileMode.Open, FileAccess.Read) Dim encryptedFile As New FileStream(encryptedFileName, FileMode.OpenOrCreate, FileAccess.Write)
' Lav SymmetricAlgorithm + key Dim alg As SymmetricAlgorithm = New RijndaelManaged alg.Key = Me.ReadKey(encryptedFileName & ".xkey")
' Læs den ukrypterede fil ind i fileData Dim fileData(CType(unencryptedFile.Length - 1, Integer)) As Byte unencryptedFile.Read(fileData, 0, CType(unencryptedFile.Length, Integer))
' Lav encryptor + cryptostream Dim encryptor As ICryptoTransform = alg.CreateDecryptor Dim encryptStream As New CryptoStream(unencryptedFile, encryptor, CryptoStreamMode.Read)
' Skriv til cryptostream encryptStream.Read(fileData, 0, CType(unencryptedFile.Length, Integer))
' Ryd op encryptStream.Close() encryptedFile.Close() unencryptedFile.Close()
Imports System Imports System.IO Imports System.Security.Cryptography
Public Class EncryptionDemo Public Sub EncryptData(ByVal unencryptedFileName As String, ByVal encryptedFileName As String)
' Lav stream-objekter Dim unencryptedFile As New FileStream(unencryptedFileName, FileMode.Open, FileAccess.Read) Dim encryptedFile As New FileStream(encryptedFileName, FileMode.OpenOrCreate, FileAccess.Write)
' Lav SymmetricAlgorithm + key Dim alg As SymmetricAlgorithm = New RijndaelManaged alg.GenerateKey() Dim iv() As Byte = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } alg.IV = iv
' Læs den ukrypterede fil ind i fileData Dim fileData(CType(unencryptedFile.Length - 1, Integer)) As Byte unencryptedFile.Read(fileData, 0, CType(unencryptedFile.Length, Integer))
' Lav encryptor + cryptostream Dim encryptor As ICryptoTransform = alg.CreateEncryptor Dim encryptStream As New CryptoStream(encryptedFile, encryptor, CryptoStreamMode.Write)
' Skriv til cryptostream encryptStream.Write(fileData, 0, CType(unencryptedFile.Length, Integer))
' Ryd op encryptStream.Close() encryptedFile.Close() unencryptedFile.Close()
End Sub
Public Sub DecryptData(ByVal encryptedFileName As String, ByVal unencryptedFileName As String)
' Lav stream-objekter Dim encryptedFile As New FileStream(encryptedFileName, FileMode.Open, FileAccess.Read) Dim unencryptedFile As New FileStream(unencryptedFileName, FileMode.OpenOrCreate, FileAccess.Write)
' Lav SymmetricAlgorithm + key Dim alg As SymmetricAlgorithm = New RijndaelManaged alg.Key = Me.ReadKey(encryptedFileName & ".xkey") Dim iv() As Byte = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } alg.IV = iv
' Lav encryptor + cryptostream Dim decryptor As ICryptoTransform = alg.CreateDecryptor Dim decryptStream As New CryptoStream(encryptedFile, decryptor, CryptoStreamMode.Read)
' Læs fra cryptostream Dim fileData(CType(encryptedFile.Length - 1, Integer)) As Byte Dim n As Integer n = decryptStream.Read(fileData, 0, CType(encryptedFile.Length, Integer))
' Skriv til fil unencryptedFile.Write(fileData, 0, n)
' Ryd op decryptStream.Close() encryptedFile.Close() unencryptedFile.Close()
End Sub
Private Sub SaveKey(ByVal key As Byte(), ByVal newFileName As String) Dim keyStream As New FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.Write)
keyStream.Write(key, 0, key.Length) keyStream.Close() End Sub
Private Function ReadKey(ByVal keyFileName As String) As Byte() Dim keyStream As New FileStream(keyFileName, FileMode.Open, FileAccess.Read) Dim fileData(CType(keyStream.Length - 1, Integer)) As Byte
keyStream.Read(fileData, 0, fileData.Length) Return fileData End Function
End Class
Public Class TestClass Public Shared Sub Main(ByVal args As String()) Dim demo As EncryptionDemo = New EncryptionDemo demo.EncryptData("C:\1.txt", "C:\2.txt") demo.DecryptData("C:\2.txt", "C:\3.txt") End Sub End Class
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.