Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use 2 method for encrypt and decrypt strig but I have an error in line :"
C#
cryptStream.FlushFinalBlock();

" when I want to decrypt string.
how remove this error?
I use RijndaelManaged algorithm.
my key is 32 byte:256 bit
my IV is 16 byte:128 bit
C#
public string RamziKonToString(string inputText)
        {
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of string
            byte[] inputBytes = UTF8Encoder.GetBytes(inputText);
            ////////////////////////////////////////////////////////////////////////
            //get the correct transform
            ICryptoTransform transform = SA.CreateEncryptor();
            SA.Mode = CipherMode.CBC;
            SA.Padding = PaddingMode.PKCS7;
            SA.Key = kelid;
            SA.IV = iV;

            //memory stream for output
            MemoryStream memStream = new MemoryStream();

            //setup the cryption - output written to memstream
            CryptoStream cryptStream = new CryptoStream(memStream, transform, CryptoStreamMode.Write);

            //write data to cryption engine
            cryptStream.Write(inputBytes, 0, inputBytes.Length);

            //we are finished
            cryptStream.FlushFinalBlock();

            //get result
            byte[] output = memStream.ToArray();

            //finished with engine, so close the stream
            cryptStream.Close();
            ///////////////////////////////////////////////////////////////////////////////
            //convert back to a string
            return Convert.ToBase64String(output);
        }


C#
public string KashfeRamz(string inputText)
        {
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of string
            byte[] inputBytes = Convert.FromBase64String(inputText);
            ///////////////////////////////////////////////////////////
            //get the correct transform
            ICryptoTransform transform = SA.CreateDecryptor();
            SA.Padding = PaddingMode.PKCS7;
            SA.Key = kelid;
            SA.IV = iV;
            SA.Mode = CipherMode.CBC;
            //memory stream for output
            MemoryStream memStream = new MemoryStream();

            //setup the cryption - output written to memstream
            CryptoStream cryptStream = new CryptoStream(memStream, transform, CryptoStreamMode.Write);

            //write data to cryption engine

            using (var sw = new StreamWriter(cryptStream))
            {
                sw.Write(inputBytes);
                cryptStream.FlushFinalBlock();
            }
            //cryptStream.Write(inputBytes, 0, inputBytes.Length);
            //we are finished
            

            //get result
            byte[] output = memStream.ToArray();

            //finished with engine, so close the stream
            cryptStream.Close();

            /////////////////////////////////////////////////////////////////
            //convert back to a string
            return UTF8Encoder.GetString(output);
        }
Posted
Updated 18-Sep-14 19:52pm
v2
Comments
George Jonsson 19-Sep-14 1:48am    
You have a working example here
MSDN: CryptoStream Class[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900