zoukankan      html  css  js  c++  java
  • 使用3DES加密算法对数据进行加密C#类

    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

    作者:一修先生
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    ES6中的let关键字,有什么用呢?
    ES6系列-什么是ES6?新手应该怎么理解
    css的变量教程,更强大的css
    尤雨溪在直播中讲到的Vue3.0 Beta的那些特性,快记笔记了
    前端基础进阶(七)-前端工程师最容易出错的问题-this关键字
    前端基础进阶(六)-大厂面试题问题:循环闭包与setTimeout
    JavaScript 中 this的指向
    What happens to our code? JavaScript 代码是怎样执行的
    Truthy and Falsy Values and Equality Operators
    iDisposable
  • 原文地址:https://www.cnblogs.com/1971ruru/p/2820589.html
Copyright © 2011-2022 走看看