用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

TrippleDES加密

2016-11-02 作者: 疯信子举报

[c#]代码库

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
 
namespace COMMON.DEncrypt
{
    public class TDESCSPEncrypt
    {
        //12个字符 
        private static string customIV = "jfYi3ggEEV4=";//"4vHKRj3yfzU=";
        //32个字符 
        private static string customKey = "9DQrNW0KBi1LLGok6ayQtvuZucoXtLb0";//"xhVs6DRXLfUGxw+AhtfQdpQGoa+8SA9d";
 
        /// <summary> 
        /// 加密字符串 
        /// </summary> 
        /// <param name="password"></param> 
        /// <returns></returns> 
        public static string EncryptPassword(string password, string key_Base64, string iv_Base64)
        {
            string encryptPassword = string.Empty;
 
            SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
            algorithm.Key = Convert.FromBase64String(key_Base64);
            algorithm.IV = Convert.FromBase64String(iv_Base64);
            algorithm.Mode = CipherMode.ECB;
            algorithm.Padding = PaddingMode.PKCS7;
 
            ICryptoTransform transform = algorithm.CreateEncryptor();
 
            byte[] data = Encoding.Unicode.GetBytes(password);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
 
            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();
            encryptPassword = Convert.ToBase64String(memoryStream.ToArray());
 
            memoryStream.Close();
            cryptoStream.Close();
 
            return encryptPassword;
        }
 
        /// <summary> 
        /// 解密字符串 
        /// </summary> 
        /// <param name="encryptPassword"></param> 
        /// <returns></returns> 
        public static string DecryptPassword(string encryptPassword, string key_Base64, string iv_Base64)
        {
            string decryptPassword = string.Empty;
 
            SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
            algorithm.Key = Convert.FromBase64String(key_Base64);
            algorithm.IV = Convert.FromBase64String(iv_Base64);
            algorithm.Mode = CipherMode.ECB;
            algorithm.Padding = PaddingMode.PKCS7;
 
            ICryptoTransform transform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
 
            byte[] buffer = Convert.FromBase64String(encryptPassword);
            MemoryStream memoryStream = new MemoryStream(buffer);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream, Encoding.Unicode);
            decryptPassword = reader.ReadToEnd();
 
            reader.Close();
            cryptoStream.Close();
            memoryStream.Close();
 
            return decryptPassword;
        }
 
        public static string EncryptPassword(string password, string key_Base64)
        {
            return EncryptPassword(password, key_Base64, customIV);
        }
 
        public static string DecryptPassword(string encryptPassword, string key_Base64)
        {
            return DecryptPassword(encryptPassword, key_Base64, customIV);
        }
 
        public static string EncryptPassword(string password)
        {
            return EncryptPassword(password, customKey, customIV);
        }
 
        public static string DecryptPassword(string encryptPassword)
        {
            return DecryptPassword(encryptPassword, customKey, customIV);
        }
 
        static TripleDESCryptoServiceProvider _DCSP;
        private static TripleDESCryptoServiceProvider DCSP
        {
            get
            {
                if (_DCSP == null)
                    _DCSP = new TripleDESCryptoServiceProvider();
                //
                return _DCSP;
            }
        }
 
        public static string CreateKEY()
        {
            DCSP.GenerateKey();
            return Convert.ToBase64String(DCSP.Key);
        }
    }
}


网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...