• 常用的对称加密算法代码


    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Security.Cryptography;

    using System.IO;



    namespace Regent.TCommons

    {

    /// <summary>

    /// '********************************************************

    /// 模块名称: 安全类

    /// 模块功能: 常用的对称加密算法代码。

    /// </summary>

    public class TSecurity

    {

    #if debug

    public

    #endif

    static byte[] K_KEY = new byte[] { 111, 122, 112, 103, 109, 111, 110, 100 };



    #if debug

    public

    #endif

    static byte[] K_VI = new byte[] { 121, 108, 10, 110, 119, 97, 110, 103 };

    /// <summary>

    /// 编码字符串

    /// </summary>

    /// <param name="data"></param>

    /// <returns></returns>

    public static string Encode(string data)

    {

    DES des = DES.Create();



    des.Key = K_KEY;

    des.IV = K_VI;



    MemoryStream mStream = new MemoryStream();



    CryptoStream cStream = new CryptoStream(mStream,

    des.CreateEncryptor(),

    CryptoStreamMode.Write);



    byte[] toEncrypt = ASCIIEncoding.UTF8.GetBytes(data);

    cStream.Write(toEncrypt, 0, toEncrypt.Length);

    cStream.FlushFinalBlock();

    byte[] bs = mStream.ToArray();

    cStream.Close();

    mStream.Close();



    return Convert.ToBase64String(bs);

    }

    /// <summary>

    /// 解码字符串

    /// </summary>

    /// <param name="data"></param>

    /// <returns></returns>



    public static string Decode(string data)

    {

    byte[] bytes = Convert.FromBase64String(data);



    MemoryStream msDecrypt = new MemoryStream(bytes);

    DES des = DES.Create();

    des.Key = K_KEY;

    des.IV = K_VI;



    CryptoStream csDecrypt = new CryptoStream(msDecrypt,

    des.CreateDecryptor(),

    CryptoStreamMode.Read);



    byte[] fromEncrypt = new byte[data.Length];



    csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

    return ASCIIEncoding.UTF8.GetString(fromEncrypt).Trim('\0');

    }

    }

    }
  • 相关阅读:
    HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)
    HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)
    HDOJ/HDU 2163 Palindromes(判断回文串~)
    js之函数
    js琐碎知识点
    position
    css属性background
    图片展示失效容错处理
    文字打点
    浮动的一些知识点
  • 原文地址:https://www.cnblogs.com/ToddLai/p/2287288.html
Copyright © 2020-2023  润新知