19. april 2006 - 19:31Der er
17 kommentarer og 1 løsning
Padding is invalid and cannot be removed
Hey,
Min applikation smider en exception når jeg forsøger at dekryptere tekst fra en fil. Koden er stort set den fra VS2005's code snippet for dekryptering af en fil.
Jeg får følgende fejl: Padding is invalid and cannot be removed.
Min kode:
Public Function Decrypt(ByVal name As String) As String Dim loc As String = String.Format("{0}\{1}.txt", Locations.SafeItemsDirectory, name) Dim stream As FileStream = File.Open(loc, FileMode.OpenOrCreate)
Dim alg As Rijndael = Rijndael.Create alg.IV = _iv alg.Key = _password
Dim crypto As New CryptoStream(Stream, alg.CreateDecryptor(), CryptoStreamMode.Read) Dim reader As New StreamReader(crypto)
Return reader.ReadToEnd ' Her går det galt
reader.Close() crypto.Close() stream.Close() End Function
Public Sub Encrypt(ByVal name As String, ByVal data As String) Dim loc As String = String.Format("{0}\{1}.txt", Locations.SafeItemsDirectory, name) Dim stream As FileStream = File.Open(loc, FileMode.OpenOrCreate)
Dim alg As Rijndael = Rijndael.Create alg.IV = _iv alg.Key = _password
Dim crypto As New CryptoStream(Stream, alg.CreateEncryptor(), CryptoStreamMode.Write) Dim writer As New StreamWriter(crypto)
writer.WriteLine(data)
writer.Close() crypto.Close() stream.Close() End Sub
Imports System Imports System.IO Imports System.Text Imports System.Security.Cryptography
Public Class MainClass Public Shared Sub Save(data As String, key As String, fnm As String) Dim stm As Stream = New FileStream(fnm, FileMode.Create, FileAccess.Write) Dim alg As Rijndael = Rijndael.Create alg.IV = New Byte() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } alg.Key = Encoding.Default.GetBytes(key) Dim crypto As New CryptoStream(stm, alg.CreateEncryptor(), CryptoStreamMode.Write) Dim b As Byte() = Encoding.Default.GetBytes(data) crypto.Write(b, 0, b.Length) crypto.Close() End Sub Public Shared Function Load(fnm As String, key As String) As String Dim n As Integer = (New FileInfo(fnm)).Length Dim stm As Stream = New FileStream(fnm, FileMode.Open, FileAccess.Read) Dim alg As Rijndael = Rijndael.Create alg.IV = New Byte() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } alg.Key = Encoding.Default.GetBytes(key) Dim crypto As New CryptoStream(stm, alg.CreateDecryptor(), CryptoStreamMode.Read) Dim b(n) As Byte n = crypto.Read(b, 0, b.Length) crypto.Close() return Encoding.Default.GetString(b, 0, n) End Function Public Shared Sub Main(ByVal args As String()) Save("Dette er en lille test", "012345670123456701234567", "C:\cryp.dat") Console.WriteLine(Load("C:\cryp.dat", "012345670123456701234567")) End Sub End Class
Jeg undrer mig dog over at det andet ikke ville virke, men nu er kryptografi og streams heller ikke lige noget jeg er super god til.
Tak for hjælpen.
Synes godt om
Ny brugerNybegynder
Din løsning...
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.