Padding is invalid and cannot be removed [Cryptography]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • floppyzedolfin

    Padding is invalid and cannot be removed [Cryptography]

    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.Pad ding = Padding.PKCS7; , the error
    is still raised.


    Here's the code.


    using System;
    using System.Collecti ons.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
    {
    RSACryptoServic eProvider RSACrypto =
    new
    RSACryptoServic eProvider(RSA_K EY_SIZE);


    EncryptedData encFiles = new
    EncryptedData() ;


    encFiles = encrypt("toEncr ypt.txt",
    RSACrypto.Expor tParameters(fal se));


    string decFile = decrypt(encFile s,
    RSACrypto.Expor tParameters(tru e));
    }
    catch (Exception e) { Console.WriteLi ne("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.Mod e = CipherMode.CBC;
    /* rijndaelAlg.Pad ding = PaddingMode.PKC S7; */
    rijndaelAlg.Gen erateKey();
    rijndaelAlg.Gen erateIV();
    ICryptoTransfor mer rijndaelEncrypt or
    =
    rijndael.Create Encryptor(rijnd aelAlg.Key, rijndaelAlg.IV) ;


    // 2 : open source and destination
    files
    FileStream fstf =
    File.Open(FileT oEncrypt, FileMode.OpenOr Create);

    EncryptedData encryptedFiles = new
    EncryptedData() ;
    encryptedFiles. Enc_File =
    "encryptedFile" ;
    FileStream fstef = new
    FileSream(encry ptedFiles.Enc_F ile,
    FileMode.OpenOr Create);
    // 3 : Encrypting data
    CryptoStream cstf = new
    CryptoStream(fs tef, rijndaelEncrypt or,
    CryptoStreamMod e.Write);
    byte[] bEncFile = new byte[(int)fstf.Lengt h];
    fstf.Read(bEncF ile, 0, (int)bEncFile.L ength);
    cstf.Write(bEnc File, 0, (int)bEncFile.L ength)


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


    // Part 2 : encrypting keys
    // 1 : create a RSA instance, and
    import the public keys
    RSACryptoServic eProvider RSA = new
    RSACryptoServic eProvider(RSA_K EY_SIZE);
    RSA.ImportParam eters(RSAParam) ;


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

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

    ByteToFile(EncK ey_byte, encryptedFiles. Enc_Key);
    ByteToFile(EncI V_byte, encryptedFiles. Enc_IV);


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


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


    // 2 : decrypt keys with RSA
    algorithm
    RSACryptoServic eProvider RSA =
    RSACryptoServic eProvider();
    RSA.ImportParam eters(RSAParam) ;


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


    // 3 : decrypt the file using the
    rijndael keys
    Rijndael rijndaelAlg =
    Rijndael.Create ();
    rijndaelAlg.Mod e = CipherMode.CBC;
    /* rijndaelAlg.Pad ding = PaddingMode.PKC S7; */
    ICryptoTransfor m rijndaelDecrypt or =
    rijndaelAlg.Cre ateDecryptor(Ke y_byte, IV_byte);


    FileStream fstef =
    File.Open(encDa ta.Enc_File, FileMode.Open);
    string DecFile = "dec_file";
    FileStream fstf = File.Open(DecFi le, FileMode.OpenOr Create);
    CryptoStream cstef = new
    CryptoStream(fs tef, rijndaelDecrypt or,
    CryptoStreamMod e.Write);
    byte[] bDecFile = new byte[(int)fstef.Leng th];
    fstef.Read(bDec File, 0, (int)bDecFile.L ength];
    cstef.Write(bDe cFile, 0, (int)bDecFile.L ength]

    // 4 : Closing Streams
    cstef.Close(); // Here's where things are bad :(
    fstef.Close();
    fstf.Close();

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

    static byte[] FileToByte(stri ng FileName)
    {
    FileStream fst = new FileStream(File Name, FileMode.Open);
    byte[] b_data = new byte[(int)fst.Length];
    fst.Read(b_data , 0, (int)b_data.Len gth);
    fst.Close();
    return b_data;
    }

    static void ByteToFile(byte[] b_data, string FileName);
    {
    FileStream fst = new FileStream(File Name, FileMode.OpenOr Create);
    fst.Write(b_dat a, 0, (int)b_data.Len gth);
    fst.Close();
    }
    }

  • rossum

    #2
    Re: Padding is invalid and cannot be removed [Cryptography]

    On Thu, 19 Jul 2007 03:22:34 -0700, floppyzedolfin
    <floppyzedolfin @gmail.comwrote :
    // 3 : decrypt the file using the rijndael keys
    Rijndael rijndaelAlg = Rijndael.Create ();
    rijndaelAlg.Mod e = CipherMode.CBC;
    /* rijndaelAlg.Pad ding = PaddingMode.PKC S7; */
    Assuming you have correctly copied your code, you could try
    uncommenting this line.
    ICryptoTransfor m rijndaelDecrypt or = rijndaelAlg.Cre ateDecryptor(Ke y_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

    Comment

    • floppyzedolfin

      #3
      Re: Padding is invalid and cannot be removed [Cryptography]

      On 19 juil, 12:43, rossum <rossu...@coldm ail.comwrote:
      On Thu, 19 Jul 2007 03:22:34 -0700, floppyzedolfin
      >
      <floppyzedol... @gmail.comwrote :
      // 3 : decrypt the file using the rijndael keys
      Rijndael rijndaelAlg = Rijndael.Create ();
      rijndaelAlg.Mod e = CipherMode.CBC;
      /* rijndaelAlg.Pad ding = PaddingMode.PKC S7; */
      >
      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 :(


      Comment

      • rossum

        #4
        Re: Padding is invalid and cannot be removed [Cryptography]

        On Fri, 20 Jul 2007 00:15:37 -0700, floppyzedolfin
        <floppyzedolfin @gmail.comwrote :
        >On 19 juil, 12:43, rossum <rossu...@coldm ail.comwrote:
        >On Thu, 19 Jul 2007 03:22:34 -0700, floppyzedolfin
        >>
        ><floppyzedol.. .@gmail.comwrot e:
        // 3 : decrypt the file using the rijndael keys
        Rijndael rijndaelAlg = Rijndael.Create ();
        rijndaelAlg.Mod e = CipherMode.CBC;
        /* rijndaelAlg.Pad ding = PaddingMode.PKC S7; */
        >>
        >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 :(
        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.Lengt h];
        Unmatched ]
        cstf.Write(bEnc File, 0, (int)bEncFile.L ength)
        Missing ;
        byte[] bDecFile = new byte[(int)fstef.Leng th];
        Unmatched ]
        fstef.Read(bDec File, 0, (int)bDecFile.L ength];
        Same again.
        cstef.Write(bDe cFile, 0, (int)bDecFile.L ength]
        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(File Name, FileMode.OpenOr Create);
        fst.Write(b_dat a, 0, (int)b_data.Len gth);
        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

        Comment

        Working...