zoukankan      html  css  js  c++  java
  • C# 常用加密方式

    ——————————————————————————MD5加密方式————————————————————————————-— 

    //tostring中的参数标示用何种格式来格式化字符串,其中X标示16进制,2表示两位
            //  string str1=byte.ToString("x");//str1="0";
            //  string str2=byte.ToString("x2");//str2="00";


            public static string GetMD5Encording(string str)
            {

                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] _byte = Encoding.Default.GetBytes(str);
                byte[] _byteRst = md5.ComputeHash(_byte);
                StringBuilder SBuilder = new StringBuilder();
                for (int i = 0; i < _byteRst.Length; i++)
                {
                    SBuilder.Append(_byteRst[i].ToString("x2"));
                }
                return SBuilder.ToString();
            }
        }

    ————————————————————————————对称加密—————————————————————————————

    using System.Text;

        using System.IO;

        using System;

        using System.Text.RegularExpressions;

        using System.Security.Cryptography;

        namespace LHCHIP.Common

        {

            /**//// <summary>

            /// 对称加密算法类

            /// </summary>

            public class DecryptEncrypt

            {

                /**//// <summary>

                /// 返回自身的一个类

                /// </summary>

                public static DecryptEncrypt MyDecryptEncrypt

               {

                    get

                   {

                        return new DecryptEncrypt();

                    }

                }

            private SymmetricAlgorithm mobjCryptoService;

            private string Key;

            /**//// <summary>

            /// 对称加密类的构造函数

            /// </summary>

            internal DecryptEncrypt()

            {

                mobjCryptoService = new RijndaelManaged();

                Key = "rrp(%&h70x89H$jgsfgfsI0456Ftma81&fvHrr&&76*h%(12lJ$lhj!y6&(*jkPer44a";

            }

            /**//// <summary>

            /// 获得密钥

            /// </summary>

            /// <returns>密钥</returns>

            private byte[] GetLegalKey()

            {

                string _TempKey = Key;

                mobjCryptoService.GenerateKey();

                byte[] bytTemp = mobjCryptoService.Key;

                int KeyLength = bytTemp.Length;

                if (_TempKey.Length > KeyLength)

             _TempKey = _TempKey.Substring(0, KeyLength);

                else if (_TempKey.Length < KeyLength)

                    _TempKey = _TempKey.PadRight(KeyLength, ' ');

                return ASCIIEncoding.ASCII.GetBytes(_TempKey);

            }

            /**//// <summary>

            /// 获得初始向量IV

            /// </summary>

            /// <returns>初试向量IV</returns>

            private byte[] GetLegalIV()

            {

                string _TempIV = "@afetj*Ghg7!rNIfsgr95GUqd9gsrb#GG7HBh(urjj6HJ($jhWk7&!hjjri%$hjk";

                mobjCryptoService.GenerateIV();

                byte[] bytTemp = mobjCryptoService.IV;

                int IVLength = bytTemp.Length;

                if (_TempIV.Length > IVLength)

                    _TempIV = _TempIV.Substring(0, IVLength);

                else if (_TempIV.Length < IVLength)

                    _TempIV = _TempIV.PadRight(IVLength, ' ');

                return ASCIIEncoding.ASCII.GetBytes(_TempIV);

            }

            /**//// <summary>

            /// 加密方法

            /// </summary>

            /// <param name="Source">待加密的串</param>

            /// <returns>经过加密的串</returns>

            public string Encrypto(string Source)

            {

                byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);

                MemoryStream ms = new MemoryStream();

                mobjCryptoService.Key = GetLegalKey();

                mobjCryptoService.IV = GetLegalIV();

                //创建对称加密器对象

            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

                //定义将数据流链接到加密转换的流

                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

                cs.Write(bytIn, 0, bytIn.Length);

                cs.FlushFinalBlock();

                ms.Close();

                byte[] bytOut = ms.ToArray();

                return Convert.ToBase64String(bytOut);

            }

            /**//// <summary>

            /// 解密方法

            /// </summary>

            /// <param name="Source">待解密的串</param>

            /// <returns>经过解密的串</returns>

            public string Decrypto(string Source)

            {

                byte[] bytIn = Convert.FromBase64String(Source);

                MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);

                mobjCryptoService.Key = GetLegalKey();

                mobjCryptoService.IV = GetLegalIV();

                //创建对称解密器对象

                ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();

                //定义将数据流链接到加密转换的流

                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

                StreamReader sr = new StreamReader(cs);

                return sr.ReadToEnd();

                }

            }

        }

    ————————————————————————DES加密————————————————————————————————
    using System;
    using System.Security.Cryptography;
    using System.IO;
    using System.Text;
    public class EncryptStringDES
    {
        public static void Main(String[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: des_demo <string-to-encrypt>", args[0]);
                return;
            }
            // 使用UTF8函数加密输入参数
            UTF8Encoding utf8Encoding = new UTF8Encoding();
            byte[] inputByteArray = utf8Encoding.GetBytes(args[0].ToCharArray());
            // 方式一:调用默认的DES实现方法DES_CSP.
            DES des = DES.Create();
            // 方式二:直接使用DES_CSP()实现DES的实体
            //DES_CSP DES = new DES_CSP();
            // 初始化DES加密的密钥和一个随机的、8比特的初始化向量(IV)
            Byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
            Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
            des.Key = key;
            des.IV = IV;
            // 建立加密流
            SymmetricStreamEncryptor sse = des.CreateEncryptor();
            // 使用CryptoMemoryStream方法获取加密过程的输出
            CryptoMemoryStream cms = new CryptoMemoryStream();
            // 将SymmetricStreamEncryptor流中的加密数据输出到CryptoMemoryStream中
            sse.SetSink(cms);
            // 加密完毕,将结果输出到控制台
            sse.Write(inputByteArray);
            sse.CloseStream();
            // 获取加密数据
            byte[] encryptedData = cms.Data;
            // 输出加密后结果
            Console.WriteLine("加密结果:");
            for (int i = 0; i < encryptedData.Length; i++)
            {
                Console.Write("{0:X2} ", encryptedData[i]);
            }
            Console.WriteLine();
            //上面演示了如何进行加密,下面演示如何进行解密
            SymmetricStreamDecryptor ssd = des.CreateDecryptor();
            cms = new CryptoMemoryStream();
            ssd.SetSink(cms);
            ssd.Write(encryptedData);
            ssd.CloseStream();
            byte[] decryptedData = cms.Data;
            char[] decryptedCharArray = utf8Encoding.GetChars(decryptedData);
            Console.WriteLine("解密后数据:");
            Console.Write(decryptedCharArray);
            Console.WriteLine();
        }
    }
  • 相关阅读:
    Azure Queues and Service Bus Queues
    同步消息队列模型
    ADO.NET Asynchronous Programming
    js中cookie
    字符串格式化命令 sprintf
    JS的Touch事件们,触屏时的js事件
    imac上php环境php+apache+mysql
    日历js插件
    html_entity_decode 函数 编辑器输出之显示含html为转义 thinkphp
    thinkphp自定义权限管理之名称判断
  • 原文地址:https://www.cnblogs.com/Traner/p/2819974.html
Copyright © 2011-2022 走看看