首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DESCryptoServiceProvider加密解密有关问题

2013-12-11 
DESCryptoServiceProvider加密解密问题,在线等这是加密的方法------------------------------------------

DESCryptoServiceProvider加密解密问题,在线等
这是加密的方法
DESCryptoServiceProvider加密解密有关问题
----------------------------------------------------------
这是我找了两个解密的不好使
DESCryptoServiceProvider加密解密有关问题
DESCryptoServiceProvider加密解密有关问题

求高手指点,怎么根据第一个加密方法解密出文本来!
在线等!
解密 加密
[解决办法]
哥给你贴个例子吧,呵呵


        /// <summary>
        /// 类型:方法
        /// 方法名称:加密字符串
        /// 方法说明:根据传入的字符串和密匙返回加密后的字串
        /// </summary>
        /// <param name="strText">加密前的原文</param>
        /// <param name="strKey">密匙</param>
        /// <returns>加密后的字串</returns>
        public static string Encrypt(string strText, string strKey)
        {
            strKey += "etk579@#$am";
            Byte[] byKey = { };
            Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(strKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                //。。。。
                throw ex;
            }

        }

        /// <summary>
        /// 类型:方法
        /// 方法名称:解密字符串
        /// 方法说明:根据传入的字符串和密匙返回解密后的字串
        /// </summary>
        /// <param name="strText">加密后的密文</param>
        /// <param name="strKey">密匙</param>
        /// <returns>解密后的字串</returns>
        public static string Decrypt(string strText, string strKey)
        {
            strKey += "etk579@#$am";
            Byte[] byKey = { };
            Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };


            Byte[] inputByteArray = new byte[strText.Length];
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(strKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                //。。。。。。。
                throw ex;
            }
        }


[解决办法]
DES 是最简单常规的加密方法,一直用没有发现过问题。
////////////////////////////////////////////////////////////////////////////DES加密****************************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Component
{
    public class Security
    {
        public Security()
        { 
        
        }

        //默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /**//**//**//// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static 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 static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                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;
            }
        } 


    }
}

热点排行