Brian Mitchell wrote:
I have a byte array that I am encrypting using the System.Cryptography
classes and it encrypts just fine. However when I decrypt the array the
decryptor removes all the white spaces from the array (byte values of 255).
I've tried setting the padding to none but it doesn't work. As long as the
array doesn't have any white spaces in there it encrypts and decrypts
normally.
Anyone have any ideas on how to fix this?
Could you show us some code (ideally, the smallest example that goes
wrong)? I just knocked this together and it performs as expected:
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Module Module1
Sub Main()
Dim plaintext() As Byte = New Byte() {&H12, &H34, &H56, &H78,
&HFF, &H9A, &HFF, &HBC, &HFF, &HDE}
Console.Write("starting plaintext: ")
Console.WriteLine(OutHex(plaintext))
Dim algo As SymmetricAlgorithm = RijndaelManaged.Create
algo.GenerateKey()
algo.GenerateIV()
Dim key() As Byte = algo.Key
Dim iv() As Byte = algo.IV
Dim encryptor As ICryptoTransform = algo.CreateEncryptor
Dim encr() As Byte = encryptor.TransformFinalBlock(plaintext,
0, plaintext.Length)
Console.Write("encrypted : ")
Console.WriteLine(OutHex(encr))
Dim decryptor As ICryptoTransform = algo.CreateDecryptor
Dim decr() As Byte = decryptor.TransformFinalBlock(encr, 0,
encr.Length)
Console.Write("decrypted : ")
Console.WriteLine(OutHex(decr))
Console.ReadLine()
End Sub
Private Function OutHex(ByVal arr() As Byte) As String
Dim out As New StringBuilder(arr.Length * 3)
For index As Integer = 0 To arr.Length - 1
out.Append(arr(index).ToString("X").PadLeft(2, "0"c))
out.Append(" ")
Next
Return out.ToString
End Function
End Module
Output is:
starting plaintext: 12 34 56 78 FF 9A FF BC FF DE
encrypted : EF 64 FA 6A 2E A9 1F 29 9A 01 4C E4 4D 80 77 0A
decrypted : 12 34 56 78 FF 9A FF BC FF DE
As I would expect, there isn't any special treatment for bytes holding
255.
--
Larry Lard
Replies to group please