Padding is invalid and cannot be removed [Cryptography]

F

floppyzedolfin

Hi there.

I'm coding an encryption / decryption program.
At this very moment, I think I should be pretty close from the end,
but there's something blocking me on my way.

There's a "Padding is invalid and cannot be removed" error raised when
closing the cryptostream (or FlushFinalBlock-ing it).
For what I have read, Padding errors are due to an incorrect padding :
PKCS7 is recommended.
But sadly, even using rijndaelAlg.Padding = Padding.PKCS7; , the error
is still raised.


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
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(); // Here's where things are bad :(
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();
}
}
 
R

rossum

// 3 : decrypt the file using the rijndael keys
Rijndael rijndaelAlg = Rijndael.Create();
rijndaelAlg.Mode = CipherMode.CBC;
/* rijndaelAlg.Padding = PaddingMode.PKCS7; */
Assuming you have correctly copied your code, you could try
uncommenting this line.
ICryptoTransform rijndaelDecryptor = rijndaelAlg.CreateDecryptor(Key_byte, IV_byte);

If that wasn't the problem, then try cutting down your code to a
*minimal* program that exhibits the same problem. Often this process
will show you where the problem is.

rossum
 
F

floppyzedolfin

Assuming you have correctly copied your code, you could try
uncommenting this line.


I've tried it before commenting it - and it was completely
unsuccessful. And it's the same for other padding modes (such as None,
Zeros, ANSIX923 or ISO10126)

There must be something I've got wrong, but I can't see what :(
 
R

rossum

I've tried it before commenting it - and it was completely
unsuccessful. And it's the same for other padding modes (such as None,
Zeros, ANSIX923 or ISO10126)

There must be something I've got wrong, but I can't see what :(

There are lots of things wrong with it, it wouldn't even compile
correctly on my machine.
public string Enc_File
{
get {return enc_file};
Your }; should be ;}
set {enc_file = value}; Ditto.

public string Enc_Key
Same again.

public string Enc_IV
And again.

byte[] bEncFile = new byte[(int)fstf.Length];
Unmatched ]
cstf.Write(bEncFile, 0, (int)bEncFile.Length)
Missing ;
byte[] bDecFile = new byte[(int)fstef.Length];
Unmatched ]
fstef.Read(bDecFile, 0, (int)bDecFile.Length];
Same again.
cstef.Write(bDecFile, 0, (int)bDecFile.Length]
And again.
static void ByteToFile(byte[] b_data, string FileName);
Do you *really* want a semicolon at the end of that line?
{
FileStream fst = new FileStream(FileName, FileMode.OpenOrCreate);
fst.Write(b_data, 0, (int)b_data.Length);
fst.Close();
}

If you want us to help, then it is in your interest to post
*compilable* code. We should be able to cut and paste from your
posting into our compilers and get it to compile first time. Your
code fails this test. Compile your code and when it compiles OK cut
and paste it into your posting.

rossum
 

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

Top