zoukankan      html  css  js  c++  java
  • C#中使用Des加密算法

             pkey加密使用的秘钥

    bIV加密使用的偏移向量
     
             using System.Security.Cryptography;
    

      

          

            static string pkey = "WELCOMEGUY";  
            static byte[] bIV = { 0x00, 0x00, 0x00, 0x00, (byte)0x00, (byte)0xXX, (byte)0xOO, (byte)0xXO };
            /// <summary>
            /// DES加密
            /// </summary>
            /// <param name="str">需要加密的</param>
            /// <returns></returns>
            private static string Encrypt(string str)
            {            
                byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream();
                CryptoStream cs=new CryptoStream(
                                                    ms,
                                                    des.CreateEncryptor(Encoding.UTF8.GetBytes(pkey), bIV),
                                                    CryptoStreamMode.Write
                                                 );
    
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
                
            }
    
            /// <summary>
            /// DES解密
            /// </summary>
            /// <param name="pToDecrypt">需要解密的</param>
            /// <returns></returns>
            private static string Decrypt(string pToDecrypt)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);            
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(
                                                    ms,
                                                    des.CreateDecryptor(Encoding.UTF8.GetBytes(pkey), bIV),
                                                    CryptoStreamMode.Write
                                                    );
                cs.Write(inputByteArray, 0, inputByteArray.Length);            
                cs.FlushFinalBlock();
                return Encoding.UTF8.GetString(ms.ToArray());
            }
  • 相关阅读:
    awk例子
    vsftp搭建
    makefile里PHONY的相关介绍
    youget帮助使用手册
    正则表达式全集
    常用的正则表达式
    基本用法
    心情
    asp.net和java
    java and asp.net
  • 原文地址:https://www.cnblogs.com/5igis/p/5igis_12303.html
Copyright © 2011-2022 走看看