468,291 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

"Bad Data" error and issues with Streams

I'm actually encoding an encryption / decryption program.
The encryption programes takes a file path in parameter, and encrypts
the contents of the file and stores that into another file.

I'm using AES for it is quick, and RSA to encrypt AES, to transmit AES
keys (it'll run on two separate computers).

Please notice that
- this is my first C# code :)
- this is the first time I use Streams
- I used ASCIIEncoding for byte[] <-string, but I don't know if it
uses ASCII 128 or ASCII 256 (and it's rather important).

I have the following issues :
- the file encrypted with the AES algorithm is smaller in size than
the original one (ratio is about 1:1.7). Is it possible that the
Streams end too early ?
- a "Bad Data" error is raised when importing RSAParam, in decrypt.

Thanks in advance.
Here's the code.
using System;
using System.Collections.Generic,
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace Project
class EncryptedData
// contains the names of the files where encrypted data will be

public string Enc_File
get {return enc_file};
set {enc_file = value};
string enc_file;

public string Enc_Key
get {return enc_key};
set {enc_key = value};
string enc_key;

public string Enc_IV
get {return enc_IV};
set {enc_IV = value};
string enc_IV;

class LetsDoIt
const int RSA_KEY_SIZE = 4096;

static void Main()
RSACryptoServiceProvider RSACrypto = new

EncryptedData encFiles = new EncryptedData();

encFiles = encrypt("toEncrypt.txt",

string decFile = decrypt(encFiles,
catch (Exception e) { Console.WriteLine("Error in Main: {0}",
e.Message); }

static EncryptedData encrypt(string FileToEncrypt, RSAParameters
// Part 1 : encrypting data
// 1 : create a Rijndael instance.
Rijndael rijndaelAlg = Rijndael.Create();
ICryptoTransformer rijndaelEncryptor =
rijndael.CreateEncryptor(rijndaelAlg.Key, rijndaelAlg.IV);

// 2 : open and read file
FileStream fstf = File.Open(FileToEncrypt, FileMode.OpenOrCreate);
StreamReader strf = new StreamReader(fst);
string file_contents = str.ReadToEnd();

EncryptedData encryptedFiles = new EncryptedData();
encryptedFiles.Enc_File = "encryptedFile";

// 3 : creating a new file, and writing encrypted data into it
FileStream fstef = new FileSream(encryptedFiles.Enc_File,
CryptoStream cstf = new CryptoStream(fstef, rijndaelEncryptor,
StreamWriter stw = new StreamWriter(cstf);

// 4 : closing streams

// Part 2 : encrypting keys
// 1 : create a RSA instance, and import the public keys
RSACryptoServiceProvider RSA = new

// 2 : encrypt Rijndael keys
byte[] EncKey_byte = RSA.Encrypt(rijndaelAlg.Key, false);
byte[] EncIV_byte = RSA.Encrypt(rijndaelAlg.IV, false);

ASCIIEncoding ByteConverter = new ASCIIEncoding();
string EncKey_string = ByteConverter.GetString(EncKey_byte);
string EncIV_string = ByteConverter.GetString(EncIV_byte);

// 3 : finally, writing encrypted keys into files
encryptedFiles.Enc_Key = "encryptedKey";
encryptedFiles.Enc_IV = "encryptedIV";
FileStream fstK = new FileStream(encryptedFiles.Enc_Key,
FileStream fstI = new FileStream(encryptedFiles.Enc_IV,
StreamWriter stwK = new StreamWriter(fstK);
StreamWriter stwI = new StreamWriter(fstI);

// 4 : closing streams

return encryptedFiles;
catch (Exception e) { Console.WriteLine("Error in encrypt: {0}",
e.Message); }

static string decrypt(EncryptedData encData, RSAParameters RSAParam)
// 1 : get files' contents
FileStream fstK = File.Open(encData.Enc_Key, FileMode.Open);
FileStream fstI = File.Open(encData.Enc_I, FileMode.Open);
StreamReader stwK = new StreamReader(fstK);
StreamReader stwI = new StreamReader(fstI);

string EncKey_string = stwK.ReadToEnd();
string EncIV_Key = stwI.ReadToEnd();

ASCIIEncoding ByteConverter = new ASCIIEncoding();
byte[] EncKey_byte = ByteConverter.GetBytes(EncKey_string);
byte[] EncIV_byte = ByteConverter.GetBytes(EncIV_string);

// 2 : decrypt keys with RSA algorithm
RSACryptoServiceProvider RSA = RSACryptoServiceProvider();

byte[] Key_byte = RSA.Decrypt(EncKey_byte, false);
byte[] IV_byte = RSA.Decrypt(EncIV_byte, false);

// 3 : decrypt the file using the rijndael keys
Rijndael rijndaelAlg = Rijndael.Create();
ICryptoTransform rijndaelDecryptor =
rijndaelAlg.CreateDecryptor(Key_byte, IV_byte);

FileStream fstef = File.Open(encData.Enc_File, FileMode.Open);
CryptoStream cstef = new CryptoStream(fstef, rijndaelDecryptor,
StreamReader stref = new StreamReader(cstef);

// 4 : writing retrieved contents into file
string contents;
string DecFile = "DecFile.txt";
FileStream fstf = File.Open(DecFile, FileMode.OpenOrCreate);
StreamWriter stwf = new StreamWriter(fstf);

contents = stref.ReadToEnd();

// 5 : closing streams

return DecFile;
catch (Exception e) { Console.WriteLine("Error in decrypt: {0}",
e.Message); }

Jul 17 '07 #1
4 5697
"floppyzedolfin" <fl************@gmail.comwrote in message
news:11*********************@i38g2000prf.googlegro ups.com...
- this is my first C# code :)
- this is the first time I use Streams
- I used ASCIIEncoding for byte[] <-string, but I don't know if it
uses ASCII 128 or ASCII 256 (and it's rather important).
I have not examined in detail the whole of your code, but I can tell
you that this is a no-no. Every time that you are using cryptographic keys
or you are manipulating encrypted data, you are working arrays of bytes, and
it is critical that you preserve each and everyone of the bits in those
bytes. This means that you cannot use any kind of Encoding. Specifically,
ASCIIEncoding uses 7-bit ASCII, so you are definitely going to lose
information, but even if you use another Encoding, it is going to perform
some conversion on the characters and you will be losing bits.
This means that you should avoid converting any of your arrays of bytes
into strings (unless you use something more sophisticated such as Base64
encoding). Which in turn means that you should not be using StreamReaders or
StreamWriters, which read and write strings. Instead, use FileStreams, which
take arrays of bytes as the data to read or write.
Jul 17 '07 #2
On 17 juil, 12:49, "Alberto Poblacion" <earthling-
Instead, use FileStreams, which
take arrays of bytes as the data to read or write.
Yep, that sounds good.

I'll give more information later (for those who may be interested).

Jul 17 '07 #3
Well, files do have the same size by now, but I get some "out of
bonds" errors when decrypting.
And sometimes, "Bad Data" :(

Jul 17 '07 #4
floppyzedolfin <fl************@gmail.comwrote:
Well, files do have the same size by now, but I get some "out of
bonds" errors when decrypting.
And sometimes, "Bad Data" :(
Could you post a short but complete program which demonstrates the

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Norbert Munkel | last post: by
5 posts views Thread by Alfredo Magallón Arbizu | last post: by
16 posts views Thread by lawrence k | last post: by
6 posts views Thread by James Yang | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.