加密解密问题 [英] Encryption decryption problem

查看:111
本文介绍了加密解密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,下面是两个函数,一个用于加密,另一个用于解密.当我向EncryptBufW发送英文缓冲区时,它将对其进行加密,而DecryptBufw函数可以对其进行解密.但是当我传递印地语文本时,它将被加密但未被DecryptBuff函数解密.it在out参数中返回".

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;
       }

推荐答案

也许CodeProject上的此技巧可以帮助您:
使用TripleDESCryptoServiceProvider类在.NET中进行加密/解密的功能 [ ^ ]
Maybe this tip on CodeProject can help you:
Encryption/Decryption Function in .NET using the TripleDESCryptoServiceProvider Class[^]


您应该在所有函数中使用UTF8Encoding而不是Unicode !!
You should use UTF8Encoding instead of Unicode in all functions !!


问题已解决..
代替OutEnc_buffer = Encoding.Unicode.GetString(fout.ToArray());
写..
OutEnc_buffer = Convert.ToBase64String(fout.ToArray());


然后再次解密
byte[] data = Convert.FromBase64String(InEnc_buffer);
代替 byte[] data = Encoding.Unicode.GetBytes(InEnc_buffer);
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);


这篇关于加密解密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆