资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在c#中使用加密类-创新互联

这篇文章将为大家详细讲解有关如何在c#中使用加密类,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为安吉企业提供专业的成都做网站、网站建设,安吉网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
namespace Encryption.App_Code
{
    /// 
    /// 加密码类
    /// 
    public class Encryption
    {
        /// 
        /// 加密
        /// 
        /// 
        /// 
        public static string DesEncrypt(string inputString)
        {
            return DesEncrypt(inputString, Key);
        }
        /// 
        /// 解密
        /// 
        /// 
        /// 
        public static string DesDecrypt(string inputString)
        {
            return DesDecrypt(inputString, Key);
        }
        /// 
        /// 密匙
        /// 
        private static string Key
        {
            get
            {
                return "hongye10";
            }
        }
        /// 
        /// 加密字符串
        /// 注意:密钥必须为8位
        /// 
        /// 字符串
        /// 密钥
        /// 返回加密后的字符串
        public static string DesEncrypt(string inputString, string encryptKey)
        {
            byte[] byKey = null;
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString);
                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 (System.Exception error)
            {
                //return error.Message;
                return null;
            }
        }
        /// 
        /// 解密字符串
        /// 
        /// 加了密的字符串
        /// 密钥
        /// 返回解密后的字符串
        public static string DesDecrypt(string inputString, string decryptKey)
        {
            byte[] byKey = null;
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            byte[] inputByteArray = new Byte[inputString.Length];
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(inputString);
                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 = new System.Text.UTF8Encoding();
                return encoding.GetString(ms.ToArray());
            }
            catch (System.Exception error)
            {
                //return error.Message;
                return null;
            }
        }
    }
}

关于如何在c#中使用加密类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


本文名称:如何在c#中使用加密类-创新互联
当前地址:http://cdkjz.cn/article/dcdicc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220