zoukankan      html  css  js  c++  java
  • .Net Core DES加密解密

    一、DES说明

    1.加密的密钥必须是16位,因为是通过AES处理的Create,AES内置的位数为16位。

    2.加密结果返回Base64字符格式

    二、加密方法整理

    //默认密钥向量 
    private static byte[] Keys = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
    /// <summary> 
    /// DES加密字符串 
    /// </summary> 
    /// <param name="encryptString">待加密的字符串</param> 
    /// <param name="encryptKey">加密密钥,要求为16位</param> 
    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> 
    public static string DESEncrypt(string encryptString, string encryptKey = "Key123Ace#321Key")
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 16));
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            var DCSP = Aes.Create();
            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)
        {
            return ex.Message + encryptString;
        }
    
    }
    /// <summary> 
    /// DES解密字符串 
    /// </summary> 
    /// <param name="decryptString">待解密的字符串</param> 
    /// <param name="decryptKey">解密密钥,要求为16位,和加密密钥相同</param> 
    /// <returns>解密成功返回解密后的字符串,失败返源串</returns> 
    public static string DESDecrypt(string decryptString, string decryptKey = "Key123Ace#321Key")
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 16));
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            var DCSP = Aes.Create();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            Byte[] inputByteArrays = new byte[inputByteArray.Length];
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch (Exception ex)
        {
            return ex.Message + decryptString;
        }
    
    }
    View Code

    测试代码:

    //DES 测试
    string result1 = SecurityHelper.DESEncrypt("张三丰");
    Console.WriteLine(result1); //Wmp7NDhI5S/U/H0qf0YWBw==
    
    string result2 = SecurityHelper.DESDecrypt(result1);
    Console.WriteLine(result2); //张三丰

    更多:

    .Net Core AES加密解密

    .Net Core Base64加密解密

    .Net Core Md5加密整理

  • 相关阅读:
    Proteus仿真与实际的差别
    用 Proteus学习51单片机之1602液晶
    用Proteus学习51单片机之中断
    用Proteus学习51单片机之键盘
    用Proteus学习51单片机之数码管
    PHPnow 安装服务 [ Apache_pn ] 失败的解决方法 for[windows7/vista]
    chrome 不能打印背景图的解决方法
    JS 无块级作用域
    IE6下zindex犯癫不起作用bug的初步研究
    ie6下的js调试工具companion.js
  • 原文地址:https://www.cnblogs.com/tianma3798/p/8807906.html
Copyright © 2011-2022 走看看