Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi following are Two functions one is for encryption and another for decryption.When i send English buffer to EncryptBufW then it encrypt it and function DecryptBufw can decrypt it.But when i pass Hindi text then it encrypted but not decrypted by DecryptBuff function.it returns "" in out parameter.
public int EncryptBuffW(string InDec_buffer, out string OutEnc_buffer, out int OutEnc_bSize)
       {
           OutEnc_buffer = "";
           OutEnc_bSize = 0;
           string OutData = "";
           CryptoStream encStream = null;
           MemoryStream fin = null;
           MemoryStream fout = new MemoryStream();

           try
           {
               try
               {
                   byte[] data = Encoding.Unicode.GetBytes(InDec_buffer);
                   //Create the file streams to handle the input and output files.
                   fin = new MemoryStream(data);
                   //Create variables to help with read and write.

                   byte[] bin = new byte[bufLen]; //This is intermediate storage for the encryption.

                   long rdlen = 0; //This is the total number of bytes written.

                   long totlen = fin.Length; //This is the total length of the input file.

                   int len; //This is the number of bytes to be written at a time.

                   RijndaelManaged rijn = new RijndaelManaged();

                   rijn.Mode = CipherMode.CBC;
                   rijn.KeySize = 256;
                   rijn.BlockSize = 128;
                   rijn.Padding = PaddingMode.PKCS7;

                   encStream = new CryptoStream(fout, rijn.CreateEncryptor(cryptoKey, cryptoIV), CryptoStreamMode.Write);

                   //Read from the input file, then encrypt and write to the output file.

                   while (true)
                   {

                       len = fin.Read(bin, 0, bufLen);

                       if (len == 0)

                           break;

                       encStream.Write(bin, 0, len);

                       rdlen += len;


                   }
                   encStream.FlushFinalBlock();
                   OutEnc_buffer = Encoding.Unicode.GetString(fout.ToArray());
                   /* StreamWriter wr = new StreamWriter("c:\\fff.txt", true, Encoding.Unicode);
                    wr.Write(OutEnc_buffer);
                    wr.Close();*/

               }
               catch (Exception e)
               {
                   if (logPermission)
                   {
                       SetErrLogFileName("EncryptBuffW:" + " Exception Details: " + e.Message);
                   }
                   return 1;
               }

               finally
               {

                   if (encStream != null)

                       encStream.Close();

                   if (fout != null)

                       fout.Close();

                   if (fin != null)

                       fin.Close();

               }
           }
           catch (Exception ee)
           {
               if (logPermission)
               {
                   SetErrLogFileName("EncryptBuffW:" + " Exception Details: " + ee.Message);
               }
               return 1;
           }

           OutEnc_bSize = OutEnc_buffer.Length;
           return 0;
       }


public int DecryptBuffW(string InEnc_buffer, int InEnc_bSize, out string OutDec_buffer)
       {
           OutDec_buffer = "";
           InEnc_bSize = 0;
           string OutData = "";
           CryptoStream encStream = null;
           MemoryStream fin = null;
           MemoryStream fout = new MemoryStream();
           try
           {
               try
               {
                   byte[] data = Encoding.Unicode.GetBytes(InEnc_buffer);
                   //Create the file streams to handle the input and output files.
                   fin = new MemoryStream(data);
                   //Create variables to help with read and write.

                   byte[] bin = new byte[bufLen]; //This is intermediate storage for the encryption.

                   long rdlen = 0; //This is the total number of bytes written.

                   long totlen = fin.Length; //This is the total length of the input file.

                   int len; //This is the number of bytes to be written at a time.

                   RijndaelManaged rijn = new RijndaelManaged();

                   rijn.Mode = CipherMode.CBC;
                   rijn.KeySize = 256;
                   rijn.BlockSize = 128;
                   rijn.Padding = PaddingMode.PKCS7;

                   encStream = new CryptoStream(fout, rijn.CreateDecryptor(cryptoKey, cryptoIV), CryptoStreamMode.Write);

                   //Read from the input file, then encrypt and write to the output file.

                   while (true)
                   {

                       len = fin.Read(bin, 0, bufLen);

                       if (len == 0)

                           break;

                       encStream.Write(bin, 0, len);

                       rdlen += len;


                   }
                   encStream.FlushFinalBlock();
                   OutDec_buffer = Encoding.Unicode.GetString(fout.ToArray());
                   /*StreamWriter wr = new StreamWriter("c:\\fff.txt",true,Encoding.Default);
                   wr.Write(OutEnc_buffer);
                   wr.Close();*/

               }
               catch (Exception e)
               {
                   if (logPermission)
                   {
                       SetErrLogFileName("DecryptBuffW:" + " Exception Details: " + e.Message);
                   }
                   return 1;
               }

               finally
               {

                   if (encStream != null)

                       encStream.Close();

                   if (fout != null)

                       fout.Close();

                   if (fin != null)

                       fin.Close();

               }
           }
           catch (Exception ee)
           {
               if (logPermission)
               {
                   SetErrLogFileName("DecryptBuffW:" + " Exception Details: " + ee.Message);
               }
               return 1;
           }

           InEnc_bSize = OutDec_buffer.Length;

           return 0;
       }
Posted
Updated 7-Sep-12 0:30am
v3
Comments
CodeHawkz 7-Sep-12 6:27am    
How do you feed the hindi characters to the system? Did you try outputting the raw input and see whether the input and the received input is identical? Perhaps, your methods are working fine :) I'll check up on the methods anyways
sunil pol 7-Sep-12 6:33am    
The out parameter of encypt function i directly send for decryption as a in Parametr.
Hindi Data can be send "जनवरी हजार अप्रैल" as a input parameter

 
Share this answer
 
v2
You should use UTF8Encoding instead of Unicode in all functions !!
 
Share this answer
 
Problem is solved..
istead of OutEnc_buffer = Encoding.Unicode.GetString(fout.ToArray());
write..
OutEnc_buffer = Convert.ToBase64String(fout.ToArray());


and while decrypting again
write byte[] data = Convert.FromBase64String(InEnc_buffer);
instead of byte[] data = Encoding.Unicode.GetBytes(InEnc_buffer);
 
Share this answer
 
v2
try using the methods in this post:
Self Driven Cryptographic Operations[^]
 
Share this answer
 

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