473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Crypto removing white spaces from byte array

I'm sorry if this is the wrong group but I couldn't find one relating to
cryptography.

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?

Thanks!!

Brian


Jun 2 '06 #1
1 1928

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

Jun 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stu | last post by:
Hi, Im reading a file in from disk as a byte array then passing it to a memory stream for decryption using crypto api functions. What I have found is that you need to reduce the array length by 2...
8
by: Peter O'Reilly | last post by:
I have an HTML form with a textarea input box. When the user conducts a post request (e.g. clicks the submit button), an HTML preview page is presented to them with the information they have...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
5
by: Jonathan Ng | last post by:
Hi, I was wondering if there was a way to include the white spaces in a string. Currently, I am using: scanf("%s", &input); However, this doesn't include the 'space' character or any other...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
0
by: Cory Baker | last post by:
Hi all! I am trying to encrypt data with C# and decrypt the same data using VC++ 6, both using a password. I believe that I am very close on this one but am currently stuck and am receiving...
2
by: Mark | last post by:
I have been playing around with encrypting passwords using a class found in a MS KB (see farther down). It seems to work great so long as the original password is comprised of characters on the...
6
by: SenseiHitokiri | last post by:
I have some code that converts a string into base64 for some encryption. It was written on the 1.1 framework but I am trying to get it to work on 2.0. It throws exceptions on the...
3
by: ayan4u | last post by:
well i need to deal with white spaces in charecter arrays... with static arrays its fine.. char ss; cin.getline(ss, sizeof ss); .... //deals with white spaces
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.