标题:求大神帮我把这个用C#写成一个程序。感激不尽
只看楼主
qq283006567
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-10-22
结帖率:0
已结贴  问题点数:20 回复次数:3 
求大神帮我把这个用C#写成一个程序。感激不尽
本人新手,求教应该如何将如下代码写成完整的C#程序,用来加密和解密XML文件,代码如下,感谢。

报文采用标准的AES加密,加密的密钥: MYgGnQE2+DAS973vd1DFHg==
请查看下面的代码示例进行文件加密
2.2    C#版代码示例
public static class AESHelper
    {
        /// <summary>
        ///  密钥
        /// </summary>
        private static String Key = "MYgGnQE2+DAS973vd1DFHg==";

        /// <summary>
        ///  AES 解密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Decrypt(string str)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Convert.FromBase64String(Key),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        ///  加密
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <returns></returns>
        public static string Encrypt(string toEncrypt)
        {
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Convert.FromBase64String(Key),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = rm.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
    }
搜索更多相关主题的帖子: private 文件加密 public 如何 
2015-10-22 18:05
Spy001
Rank: 9Rank: 9Rank: 9
来 自:山那边
等 级:贵宾
威 望:10
帖 子:138
专家分:1360
注 册:2015-1-23
得分:20 
大胸弟,这是老师布置的作业题吧

千百年之后,谁又还记得谁........
2015-10-27 00:11
shine_arp
Rank: 2
等 级:论坛游民
帖 子:4
专家分:10
注 册:2015-11-18
得分:0 
        private void btnEncrypt_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openSelectFile = new System.Windows.Forms.OpenFileDialog();
            openSelectFile.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            openSelectFile.Filter = "xml files (*.xml)|*.xml";
            openSelectFile.FilterIndex = 1;
            openSelectFile.RestoreDirectory = true;

            if (openSelectFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string selectFile = openSelectFile.FileName;
                if ((selectFile))
                {
                    System.Xml.Linq.XElement xe = System.Xml.Linq.XElement.Load(selectFile);
                    string strXe = xe.ToString();
                    string encryXe = AESHelper.Encrypt(strXe);
                    try
                    {
                        using ( sw = new ("EncryXml" + DateTime.Now.Ticks+".aes"))
                        {
                            sw.Write(encryXe);
                            sw.Flush();
                            sw.Close();                           
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show("保存加密文件失败,异常:" + ex.Message, "Error");
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("请选择正确的文件!", "Error");
                }
            }
        }

        private void btnDecrypt_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openSelectFile = new System.Windows.Forms.OpenFileDialog();
            openSelectFile.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            openSelectFile.Filter = "AES files (*.aes)|*.aes";
            openSelectFile.FilterIndex = 1;
            openSelectFile.RestoreDirectory = true;

            if (openSelectFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string selectFile = openSelectFile.FileName;
                if ((selectFile))
                {
                     sr = new (selectFile);
                    string strFile = sr.ReadToEnd();
                    string decryStr = AESHelper.Decrypt(strFile);
                    try
                    {
                        System.Xml.Linq.XElement xe = System.Xml.Linq.XElement.Parse(decryStr);
                        string saveXmlName = "DecryptXml" + DateTime.Now.Ticks + ".xml";
                        xe.Save(saveXmlName);
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show("保存解密文件失败,异常:" + ex.Message, "Error");
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("请选择正确的文件!", "Error");
                }
            }
        }
2015-11-20 14:25
深秋2015
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:12
帖 子:402
专家分:1500
注 册:2015-10-13
得分:0 
以下是引用Spy001在2015-10-27 00:11:12的发言:

大胸弟,这是老师布置的作业题吧

大胸弟?

从此以后我都不敢抬头看
仿佛我的天空失去了颜色
2015-11-20 14:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-458440-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.366227 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved