zoukankan      html  css  js  c++  java
  • c#常用的加密解密方法

    using System;
    using System.Security.Cryptography;
    using System.Text;

    namespace AttendService
    {
        public static class DESEncrypt
        {
            #region ========加密========

            private static string txtKey = "PatrickpanP=";
            private static string txtIV = "LiuJineagel=";

             /// <summary>
            /// 加密数据
            /// </summary>
            /// <param name="Text"></param>
            /// <param name="sKey"></param>
            /// <returns></returns>
            public static string Encrypt(string Text)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray;
                inputByteArray = Encoding.Default.GetBytes(Text);
                //des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                //des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                des.Key = Convert.FromBase64String(txtKey);
                des.IV = Convert.FromBase64String(txtIV);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }

            #endregion

            #region ========解密========


            /// <summary>
            /// 解密数据
            /// </summary>
            /// <param name="Text"></param>
            /// <param name="sKey"></param>
            /// <returns></returns>
            public static string Decrypt(string Text)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                int len;
                len = Text.Length / 2;
                byte[] inputByteArray = new byte[len];
                int x, i;
                for (x = 0; x < len; x++)
                {
                    i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }
                //des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                //des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                des.Key = Convert.FromBase64String(txtKey);
                des.IV = Convert.FromBase64String(txtIV);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Encoding.Default.GetString(ms.ToArray());
            }

            #endregion
        }
    }

  • 相关阅读:
    c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
    c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
    c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
    c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
    c语言时间库函数#include<time.h>
    c语言输入与输出库函数#include<stdio.h>
    c语言诊断_断言库函数#include<assert.h>
    c语言实用功能库函数#include<stdlib.h>
    Remove Duplicates from Sorted List
    Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/yzwdli/p/1282056.html
Copyright © 2011-2022 走看看