HighTechTalks DotNet Forums  

[C#] "Padding is invalid and cannot be removed" error

Dotnet Security microsoft.public.dotnet.security


Discuss [C#] "Padding is invalid and cannot be removed" error in the Dotnet Security forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
floppyzedolfin
 
Posts: n/a

Default [C#] "Padding is invalid and cannot be removed" error - 07-19-2007 , 07:18 AM






Hi there.

I'm coding an encryption / decryption program.
I use the Rijndael algorithm to encrypt and decrypt data, and ensure
transmission of the keys through a RSA encryption.

An error is raised when I close the cryptostream, "Padding is invalid
and cannot be removed". I've done some research about that kind of
error, and every answer I could find was "use ' rijndaelAlg.Padding =
PaddingMode.PKCS7; ' in encryption and decryption". That's what I've
tried, but without succesful result .

Any idea of if I made a huge mistake, or if I have forgotten an
insignifiant detail ?

Thanks a lot.



Here's the code for that program:


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
stored


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()
{
try
{
RSACryptoServiceProvider RSACrypto =
new
RSACryptoServiceProvider(RSA_KEY_SIZE);


EncryptedData encFiles = new
EncryptedData();


encFiles = encrypt("toEncrypt.txt",
RSACrypto.ExportParameters(false));


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


static EncryptedData encrypt(string FileToEncrypt,
RSAParameters
RSAParam)
{
try
{
// Part 1 : encrypting data
// 1 : create a Rijndael instance.
Rijndael rijndaelAlg =
Rijndael.Create();
rijndaelAlg.Mode = CipherMode.CBC;
/* rijndaelAlg.Padding = PaddingMode.PKCS7; */
rijndaelAlg.GenerateKey();
rijndaelAlg.GenerateIV();
ICryptoTransformer rijndaelEncryptor
=
rijndael.CreateEncryptor(rijndaelAlg.Key, rijndaelAlg.IV);


// 2 : open source and destination
files
FileStream fstf =
File.Open(FileToEncrypt, FileMode.OpenOrCreate);

EncryptedData encryptedFiles = new
EncryptedData();
encryptedFiles.Enc_File =
"encryptedFile";
FileStream fstef = new
FileSream(encryptedFiles.Enc_File,
FileMode.OpenOrCreate);
// 3 : Encrypting data
CryptoStream cstf = new
CryptoStream(fstef, rijndaelEncryptor,
CryptoStreamMode.Write);
byte[] bEncFile = new byte[(int)fstf.Length];
fstf.Read(bEncFile, 0, (int)bEncFile.Length);
cstf.Write(bEncFile, 0, (int)bEncFile.Length)


// 4 : closing streams
cstf.Close();
fstef.Close();
fstf.Close();


// Part 2 : encrypting keys
// 1 : create a RSA instance, and
import the public keys
RSACryptoServiceProvider RSA = new
RSACryptoServiceProvider(RSA_KEY_SIZE);
RSA.ImportParameters(RSAParam);


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

encryptedFiles.Enc_Key = "Enc_Key";
encryptedFiles.Enc_IV = "Enc_IV";

ByteToFile(EncKey_byte, encryptedFiles.Enc_Key);
ByteToFile(EncIV_byte, encryptedFiles.Enc_IV);


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


static string decrypt(EncryptedData encData,
RSAParameters RSAParam)
{
try
{
// 1 : get files' contents
byte[] EncKey_byte =
FileToByte(encData.Enc_Key);
byte[] EncIV_byte = FileToByte(encData.Enc_IV);


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


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();
rijndaelAlg.Mode = CipherMode.CBC;
/* rijndaelAlg.Padding = PaddingMode.PKCS7; */
ICryptoTransform rijndaelDecryptor =
rijndaelAlg.CreateDecryptor(Key_byte, IV_byte);


FileStream fstef =
File.Open(encData.Enc_File, FileMode.Open);
string DecFile = "dec_file";
FileStream fstf = File.Open(DecFile, FileMode.OpenOrCreate);
CryptoStream cstef = new
CryptoStream(fstef, rijndaelDecryptor,
CryptoStreamMode.Write);
byte[] bDecFile = new byte[(int)fstef.Length];
fstef.Read(bDecFile, 0, (int)bDecFile.Length];
cstef.Write(bDecFile, 0, (int)bDecFile.Length]

// 4 : Closing Streams
cstef.Close(); // problem occurs here.
fstef.Close();
fstf.Close();

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

static byte[] FileToByte(string FileName)
{
FileStream fst = new FileStream(FileName, FileMode.Open);
byte[] b_data = new byte[(int)fst.Length];
fst.Read(b_data, 0, (int)b_data.Length);
fst.Close();
return b_data;
}

static void ByteToFile(byte[] b_data, string FileName);
{
FileStream fst = new FileStream(FileName, FileMode.OpenOrCreate);
fst.Write(b_data, 0, (int)b_data.Length);
fst.Close();
}
}


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.