WEB开发网
开发学院软件开发C语言 C#里的一些加密解密标准函数示例——DES,SHA1,R... 阅读

C#里的一些加密解密标准函数示例——DES,SHA1,RSA

 2009-04-14 08:24:55 来源:WEB开发网   
核心提示:最近收到了很多朋友的来信说希望提供DES的C#代码,但是我个人认为,C#里的一些加密解密标准函数示例——DES,SHA1,RSA,.NET 提供了很多标准函数,没有必要自己写,怎么调用就更不用解释了吧,呵呵://默认密钥向量privatebyte[]Keys={0xEF,0xAB,0x56,0x78,0x90,0x34

最近收到了很多朋友的来信说希望提供DES的C#代码,但是我个人认为,.NET 提供了很多标准函数,没有必要自己写,所以我也只发布了C++的代码,如果大家一定要熟悉加密过程的话,也可以自己动手实现整个过程,这个可以参考我博客里的DES 算法介绍,和yxyDES2 Class的代码,代码注释相当的清楚。

.NET 提供了很多标准加密、解密函数,我简要介绍一下DES,SHA1,RSA的标准函数的使用。如果你想做一个网络安全模块,只需将三种算法结合起来设计一个模型,我相信可以实现很多复杂的功能。

示例本身并不复杂,我也不做过多解释,我也学Linus Torvalds一样吼一句:"Read the f**ing code”,哈哈,开个玩笑,我相信大家肯定能看懂。

注:以下示例需引用命名空间 : using System.Security.Cryptography;

一. DES 加密、解密

我相信一下注释相当清楚了,加上我博客里关于DES的文章确实不少,所以DES不做任何解释,怎么调用就更不用解释了吧,呵呵:

        //默认密钥向量
        private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 };
        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }

1 2 3  下一页

Tags:一些 加密解密 标准

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接