标题:求大神帮我把这个用C#写成一个程序。感激不尽
取消只看楼主
qq283006567
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-10-22
结帖率:0
已结贴  问题点数:20 回复次数:0 
求大神帮我把这个用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



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




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

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