请教大家一个对称加密解密的问题,根据给出的解密方法,写出相对应的加密的方法

fhgok 2013-08-09 11:50:47
请教大家一个对称加密解密的问题,请根据给出的解密方法DecryptorMethod,写出相对应的加密的方法,解密方法如下:


/// <summary>
/// 解密文件内容
/// </summary>
/// <param name="infile">传入的待解密的文件路径</param>
/// <param name="decryptKey">string decryptKey = "12345768";</param>
/// <param name="Keys">byte[] Keys = new byte[] { 83, 40, 69, 40, 56, 39, 50, 74 };</param>
/// <returns></returns>
public string DecryptorMethod(string infile, string decryptKey, byte[] Keys)
{
string str = "";
if (infile != string.Empty)
{
try
{
using (FileStream stream = new FileStream(infile, FileMode.Open, FileAccess.Read))
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
ICryptoTransform transform = provider.CreateDecryptor(bytes, rgbIV);
using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read))
{
byte[] buffer = new byte[stream.Length];
stream2.Read(buffer, 0, buffer.Length);
stream2.Flush();
for (int i = buffer.Length - 1; i > 0; i--)
{
if (buffer[i] != 0)
{
break;
}
}
return Encoding.UTF8.GetString(buffer);
}
}
}
catch
{
str = "读取文件失败";
}
}
return str;
}
...全文
148 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rtdb 2013-08-10
  • 打赏
  • 举报
回复
很标准的DES API,很简单的,随便查一下API就可以搞定的
倒退一天 2013-08-10
  • 打赏
  • 举报
回复
解密里面那个循环没一点用,我给你屏蔽了
 /// <summary>
        /// 加密
        /// </summary>
        /// <param name="EnctryStr">待加密字符串</param>
        /// <param name="decryptKey">string decryptKey = "12345768";</param>
        /// <param name="Keys">byte[] Keys = new byte[] { 83, 40, 69, 40, 56, 39, 50, 74 };</param>
        private void EnctryMethod(string EnctryStr, string decryptKey, byte[] Keys)
        {
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                byte[] bEncrypt = Encoding.UTF8.GetBytes(EnctryStr);
                MemoryStream ms = new System.IO.MemoryStream();
                byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                ICryptoTransform transform = provider.CreateEncryptor(bytes, rgbIV);
                using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                {
                    cs.Write(bEncrypt, 0, bEncrypt.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }

                byte[] Buffer = ms.ToArray();
                ms.Close();
                string path = @"D:\a.txt";
                using (FileStream streamWrite = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    streamWrite.Write(Buffer, 0, Buffer.Length);
                }  
            }                   
        }
        /// <summary>
        /// 解密文件内容
        /// </summary>
        /// <param name="infile">传入的待解密的文件路径</param>
        /// <param name="decryptKey">string decryptKey = "12345768";</param>
        /// <param name="Keys">byte[] Keys = new byte[] { 83, 40, 69, 40, 56, 39, 50, 74 };</param>
        /// <returns></returns>
        public string DecryptorMethod(string infile, string decryptKey, byte[] Keys)
        {
            string str = "";
            if (infile != string.Empty)
            {
                try
                {
                    using (FileStream stream = new FileStream(infile, FileMode.Open, FileAccess.Read))
                    {
                        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                        byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
                        byte[] rgbIV = Keys;
                        ICryptoTransform transform = provider.CreateDecryptor(bytes, rgbIV);
                        using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                        {
                            byte[] buffer = new byte[stream.Length];
                            stream2.Read(buffer, 0, buffer.Length);
                            stream2.Flush();
                            //for (int i = buffer.Length - 1; i > 0; i--)
                            //{
                            //    if (buffer[i] != 0)
                            //    {
                            //        break;
                            //    }
                            //}
                            return Encoding.UTF8.GetString(buffer);
                        }
                    }
                }
                catch
                {
                    str = "读取文件失败";
                }
            }
            return str;
        }
fhgok 2013-08-10
  • 打赏
  • 举报
回复
chuheiqian给出的方法可用,非常感谢,也同时感谢大家的回帖。
  • 打赏
  • 举报
回复
 decryptKey 是什么 密钥么? 我记得好像直接有现成的方法的啊。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧