zoukankan      html  css  js  c++  java
  • C# DES 加密解密

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    
    namespace MTR.Library.CommonHelper
    {
        public class DESEncryptHelper
        {
            /// <summary>
            /// Encrypt string
            /// </summary>
            /// <param name="value">The string which to be encrypted.</param>
            /// <returns>The value after being encrypted.</returns>
            public static string Encrypt(string value)
            {
                return Encrypt(value, System.Configuration.ConfigurationManager.AppSettings["pwd"]);
            }
    
            /// <summary>
            /// Encrypt string
            /// </summary>
            /// <param name="value">The string which to be encrypted.</param>
            /// <param name="password">The password.</param>
            /// <returns>The value after being encrypted.</returns>
            public static string Encrypt(string value, string password)
            {
                try
                {
                    byte[] rgbKey = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                    byte[] rgbIV = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(value);
                    using (DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider())
                    {
                        using (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 (Exception ex)
                {
                    throw new Exception("Error when encrypting data", ex);
                }
            }
    
            /// <summary>
            /// Decrypt string
            /// </summary>
            /// <param name="value">The string which to be decrypted.</param>
            /// <returns>The value after being decrypted.</returns>
            public static string Decrypt(string value)
            {
                return Decrypt(value, System.Configuration.ConfigurationManager.AppSettings["pwd"]);
            }
    
            /// <summary>
            /// Decrypt string
            /// </summary>
            /// <param name="value">The string which to be decrypted.</param>
            /// <param name="password">The password.</param>
            /// <returns>The value after being decrypted.</returns>
            public static string Decrypt(string value, string password)
            {
                try
                {
                    byte[] rgbKey = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                    byte[] rgbIV = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                    byte[] inputByteArray = Convert.FromBase64String(value);
                    using (DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider())
                    {
                        using (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 (Exception ex)
                {
                    throw new Exception("Error when decrypting data", ex);
                }
            }
        }
    }
    View Code
  • 相关阅读:
    url 编码 js url传参中文乱码解决方案
    form提交时,如果target=_blank,则会打开一个新页面,但是大小和位置无法控制,请问如何进行控制
    java日期转字符串 字符串转日期 日期转日历 日历转日期
    策略模式【设计模式学习02】
    设计模式六大原则【设计模式学习开篇】
    应用程序栏【WP7学习札记之九】
    主题样式与数据绑定【WP7学习札记之八】
    启动器与选择器常用Task【WP7学习札记之四】
    屏幕方向与常用控件【WP7学习札记之六】
    设备的开发【WP7学习札记之五】
  • 原文地址:https://www.cnblogs.com/xachary/p/3924815.html
Copyright © 2011-2022 走看看