Hola!
Sto criptando un file PDF usando uno stream ma purtroppo sebbene mi fa l'encrypt non mi esegue correttamente il decripty!
Qualcuno sa dirmi perchè? Dove sbaglio?
Ho tradotto il codice in vb.net, lo posto così potete vedere.
Imports System
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class crypter
Private Shared keyb() As Byte
Private Shared ivb(15) As Byte
Public Shared keyFile As String = "c:\key.txt" ' New IniReader(Application.ExecutablePath.Replace("bin\debug\burstpdf.exe", "") + "ini\option.ini").ReadString("CRYPT", "KeyValue")
Public Sub New()
MyBase.New()
End Sub
Private Shared Function LoadKey() As Byte()
Dim i As Integer = 0
For Each s As String In "10,61,25,12,122,120,80,248,13,182,196,212,176,46,23,85".Split(",")
ivb(i) = CInt(s)
i += 1
Next
Dim key(31) As Byte
Dim sr As StreamReader
sr = File.OpenText(keyFile)
i = 0
Do While (i < 32)
Dim str As String = sr.ReadLine()
If Not str = "" Then
key(i) = Byte.Parse(str)
End If
i += 1
Loop
Return key
End Function
'*** cripta la stringa
Public Shared Function EncryptString(ByVal src As String) As String
keyb = LoadKey()
Dim p As Byte() = Encoding.ASCII.GetBytes(src.ToCharArray())
Dim encodedBytes() As Byte
Dim ms As New MemoryStream()
Dim rv As New RijndaelManaged()
Dim cs As New CryptoStream(ms, rv.CreateEncryptor(keyb, ivb), CryptoStreamMode.Write)
Try
cs.Write(p, 0, p.Length)
cs.FlushFinalBlock()
encodedBytes = ms.ToArray()
Catch ex As Exception
Finally
ms.Close()
cs.Close()
End Try
Return Convert.ToBase64String(encodedBytes)
End Function
'*** decripta
Public Shared Function DecryptString(ByVal src As String) As String
If (src <> String.Empty) Then
keyb = LoadKey()
Dim p As Byte() = Convert.FromBase64String(src)
Dim initialText(p.Length) As Byte
Dim rv As New RijndaelManaged()
Dim ms As New MemoryStream(p)
Dim cs As New CryptoStream(ms, rv.CreateDecryptor(keyb, ivb), CryptoStreamMode.Read)
Try
cs.Read(initialText, 0, initialText.Length)
Catch ex As Exception
Finally
ms.Close()
cs.Close()
End Try
Dim sb As New StringBuilder()
Dim stmp As String = Convert.ToBase64String(initialText)
For i As Integer = 0 To initialText.Length - 1
sb.Append(initialText(i))
Next
Return sb.ToString()
End If
Return src
End Function
End Class
Il file key.txt è così strutturato:
10
61
25
12
122
120
80
248
13
182
196
212
176
46
23
85
10
61
25
12
122
120
80
248
13
182
196
212
176
46
23
85