Random bytes on the beginning of encrypted file

L

Lech Migdal

Hi all

I am having a problem with decrypting files using TripleDES. Every time I
decrypt something, first 8 bytes are random. The rest of the file is a'ok.

Maybe someone has encountered such a issue ? I would appreciate any help.

Here is the code for decrypting:

FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.Create,
FileAccess.Write);
fout.SetLength(0);
char[] tempKey = tdesKeyText.ToCharArray();
byte[] tdesKey = new byte[tempKey.Length];

for (int i=0; i<tdesKey.Length; i++) {
tdesKey=Convert.ToByte(tempKey);
}

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream decStream = new CryptoStream(fout,
tdes.CreateDecryptor(tdesKey, tdesIV), CryptoStreamMode.Write);

int data;
while ((data=fin.ReadByte())!=-1)
decStream.WriteByte((byte) data);

Thank you in advance

Leszek Migdal
 
J

Jon Skeet [C# MVP]

Lech Migdal said:
I am having a problem with decrypting files using TripleDES. Every time I
decrypt something, first 8 bytes are random. The rest of the file is a'ok.

<snip>

Could you show the code which writes the encrypted file to start with?
 
L

Lech Migdal

Uzytkownik "Jon Skeet said:
a'ok.

<snip>

Could you show the code which writes the encrypted file to start with?

Here it's :

FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.Create,
FileAccess.Write);
fout.SetLength(0);
char[] tempKey = tdesKeyText.ToCharArray();
byte[] tdesKey = new byte[tempKey.Length];
for (int i=0; i<tdesKey.Length; i++) {
tdesKey=Convert.ToByte(tempKey);
}

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout,
tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write);

int data;
while ((data=fin.ReadByte())!=-1)
encStream.WriteByte((byte) data);

encStream.Close();
fin.Close();
fout.Close();

regards
Leszek Migdal
 
J

Jon Skeet [C# MVP]

Lech Migdal said:
Could you show the code which writes the encrypted file to start with?

Here it's :

FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.Create,
FileAccess.Write);
fout.SetLength(0);
char[] tempKey = tdesKeyText.ToCharArray();
byte[] tdesKey = new byte[tempKey.Length];
for (int i=0; i<tdesKey.Length; i++) {
tdesKey=Convert.ToByte(tempKey);
}

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout,
tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write);


Hmm.

It's not as nice as it might be in various ways, but I can't see any
reason why it shouldn't work.

Could you combine those two bits of code as a short but complete sample
application which we could very easily test?
 
G

Guest

Was there ever a resolution to this issue? I am experiencing the same problem. Thanks

----- Lech Migdal wrote: ----

Hi al

I am having a problem with decrypting files using TripleDES. Every time
decrypt something, first 8 bytes are random. The rest of the file is a'ok

Maybe someone has encountered such a issue ? I would appreciate any help

Here is the code for decrypting

FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read)
FileStream fout = new FileStream(outName, FileMode.Create
FileAccess.Write)
fout.SetLength(0)
char[] tempKey = tdesKeyText.ToCharArray()
byte[] tdesKey = new byte[tempKey.Length]

for (int i=0; i<tdesKey.Length; i++)
tdesKey=Convert.ToByte(tempKey)


TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()
CryptoStream decStream = new CryptoStream(fout
tdes.CreateDecryptor(tdesKey, tdesIV), CryptoStreamMode.Write)

int data
while ((data=fin.ReadByte())!=-1
decStream.WriteByte((byte) data)

Thank you in advanc

Leszek Migda
 
J

Jon Skeet [C# MVP]

Jason Tilton said:
Was there ever a resolution to this issue? I am experiencing the same
problem. Thanks.

Yes, we resolved it over email. The problem was that Leszek was
specifying null as the initialisation vector, which meant that it took
a random value. So long as you use the same initialisation vector (and
key, obviously) for encryption and decryption, you should be okay.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Decrypt String Issue 2
convert to c++ 1
Cryptostream truncation 2
Dataset encryption 3
Encryption/Decryption Changes File Size 4

Top