使用3DES密碼編譯演算法對資料進行加密C#類

來源:互聯網
上載者:User

1

2

3

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Security.Cryptography;namespace Rare.Card.Libary.Security{    /// <summary>    /// 構造一個對稱演算法,使用3Des加密    ///如果當前的 Key 屬性為 NULL,可調用 GenerateKey 方法以建立新的隨機 Key。     ///如果當前的 IV 屬性為 NULL,可調用 GenerateIV 方法以建立新的隨機 IV    /// </summary>    public class CryptoTripleDes    {        //加密向量        private static byte[] IV = { 0xB0, 0xA2, 0xB8, 0xA3, 0xDA, 0xCC, 0xDA, 0xCC };        /// <summary>        /// 使用指定的128位元組的金鑰組8位元組數組進行3Des加密        /// </summary>        /// <param name="keys">密鑰,16位元組,128位</param>        /// <param name="values">要加密的數組</param>        /// <returns>已加密的數組</returns>        public static byte[] CreateEncryptByte(byte[] keys, byte[] values)        {            TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();            //指定密匙長度,預設為192位            tdsc.KeySize = 128;            //使用指定的key和IV(加密向量)            tdsc.Key = keys;            tdsc.IV = IV;            //加密模式,位移            tdsc.Mode = CipherMode.ECB;            tdsc.Padding = PaddingMode.None;            //進行加密轉換運算            ICryptoTransform ct = tdsc.CreateEncryptor();            //8很關鍵,加密結果是8位元組數組            byte[] results = ct.TransformFinalBlock(values, 0, 8);            return results;        }        /// <summary>        /// 使用指定的128位元組的金鑰組字串(8位)進行3Des加密        /// </summary>        /// <param name="strKey"></param>        /// <param name="strValue"></param>        /// <returns></returns>        public static byte[] CreateEncryptString(string strKey, string strValue)        {            TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();            byte[] results = new byte[strValue.Length];            tdsc.KeySize = 128;            if (!string.IsNullOrEmpty(strKey))            {                tdsc.Key = Encoding.UTF8.GetBytes(strKey);            }            tdsc.IV = IV;            using (ICryptoTransform ct = tdsc.CreateDecryptor())            {                byte[] byt = Encoding.UTF8.GetBytes(strValue);                results = ct.TransformFinalBlock(byt, 0, 8);            }            return results;        }        /// <summary>        /// 對加密字串進行解密        /// </summary>        /// <param name="keys">密匙</param>        /// <param name="values">已加密字串</param>        /// <returns>解密結果</returns>        public static byte[] CreateDescryptByte(byte[] keys, byte[] values)        {            TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();            //指定密匙長度,預設為192位            tdsc.KeySize = 128;            //使用指定的key和IV(加密向量)            tdsc.Key = keys;            tdsc.IV = IV;            //加密模式,位移            tdsc.Mode = CipherMode.ECB;            tdsc.Padding = PaddingMode.None;            //進行加密轉換運算            ICryptoTransform ct = tdsc.CreateDecryptor();            //8很關鍵,加密結果是8位元組數組            byte[] results = ct.TransformFinalBlock(values, 0, 8);            return results;        }    }}

 

4測試資料:

16位元組密鑰:0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x30
對:0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88 八位元組明文作3DES加密
得出如下8位元組密文:0x3c,0x46,0xea,0x28,0x2f,0xdb,0x64,0x00

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.