4 Replies - 2881 Views - Last Post: 04 May 2012 - 04:51 AM Rate Topic: -----

#1 articuno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-May 12

AES decrypting problem(C#)

Posted 04 May 2012 - 01:46 AM

Hi guys!

I create project for a encrypt and decrypt files with AES method and i have a problem with exception "Padding is invalid and cannot be removed". If you have any idea for solution of my problem, please tell me.

        /// <summary>
        /// Decrypts specified ciphertext using Rijndael symmetric key algorithm.
        /// </summary>
        /// <param name="kriptiranoBesedilo">Ciphertext</param>
        /// <param name="aGeslo">Hash algorithm(SHA1)</param>
        /// <param name="stInteracij">Number of iterations</param>
        /// <param name="iVektor">Initialization vector</param>
        /// <param name="velikostKljuca">Size of encryption key in bits(128,192,256)</param>
        /// <param name="kljuc">Key</param>
        /// <returns></returns>
        public string dekriptiraj(string kriptiranoBesedilo, string aGeslo, int stInteracij, string iVektor, int velikostKljuca,string kljuc)
        {
            byte[] inciVektorBayt = Encoding.ASCII.GetBytes(iVektor);
            byte[] kriptiranoBesediloBayt = Convert.FromBase64String(kriptiranoBesedilo);

            //convert string into bytes
            byte[] kljucBayt = new byte[kljuc.Length / 8];
            kljucBayt = Encoding.ASCII.GetBytes(kljuc);

            //create uninitialized rijndael encryption object
            RijndaelManaged simetricniKljuc = new RijndaelManaged();

            //it is reasonable to set encryption mode to cipher block chaining
            simetricniKljuc.Mode = CipherMode.CBC;

            ICryptoTransform deKriptiraj = simetricniKljuc.CreateDecryptor(kljucBayt, inciVektorBayt);

            //define memory and crypto stream
            MemoryStream podatkovniTokPomnilnika1 = new MemoryStream(kriptiranoBesediloBayt);
            CryptoStream podatkovniTokKriptiranja1 = new CryptoStream(podatkovniTokPomnilnika1, deKriptiraj, CryptoStreamMode.Read);

            //plain text in bytes
            byte[] besedilo = new byte[kriptiranoBesediloBayt.Length];

            //start decrypting
            int stevecDektiptiranja = podatkovniTokKriptiranja1.Read(besedilo,0,besedilo.Length);

            //close strams
            podatkovniTokPomnilnika1.Close();
            podatkovniTokKriptiranja1.Close();

            //convert decrypted data into a string
            string dekriptiranoBesedilo = ASCIIEncoding.ASCII.GetString(besedilo,0,stevecDektiptiranja);

            //return decrypted data   
            return dekriptiranoBesedilo;
	}


The problem arises in row:
int stevecDektiptiranja = podatkovniTokKriptiranja1.Read(besedilo,0,besedilo.Length);


Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: AES decrypting problem(C#)

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: AES decrypting problem(C#)

Posted 04 May 2012 - 02:12 AM

I've never heard of setting an int's value from a memory stream before... I don't think <memorystream>.Read() returns an int.
Was This Post Helpful? 0
  • +
  • -

#3 articuno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-May 12

Re: AES decrypting problem(C#)

Posted 04 May 2012 - 02:51 AM

This line in code
//start decrypting
            int stevecDektiptiranja = podatkovniTokKriptiranja1.Read(besedilo,0,besedilo.Length);

return size of decrypting txt. If we encrypt string "hello, world!", this stream return 13.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: AES decrypting problem(C#)

Posted 04 May 2012 - 04:19 AM

The size of a block for AES is fixed, but you are telling it to read less than a block size (Line 36) so it's telling you that it needs the rest of the block (padding isn't valid). Why are you telling it the size at all? Try following the example on the documentation page, it's shorter than your code and it actually works.
Was This Post Helpful? 0
  • +
  • -

#5 articuno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-May 12

Re: AES decrypting problem(C#)

Posted 04 May 2012 - 04:51 AM

I see this example on one website and I tested this code(because I had problems with implementation AES algorithm) and its work. Then I have programmed my version where I didn use the same parameters(in decrypt function), because I wanted to have only 2 parameters(text and key).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1