zoukankan      html  css  js  c++  java
  • 常用的对称加密算法代码

    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');

    }

    }

    }
  • 相关阅读:
    ecshop的详细安装步骤
    php+mysql 除了设置主键防止表单提交内容重复外的另一种方法
    strcmp
    map set区别
    ++i vs i++
    stl vector erase
    user initialization list vs constructor assignment
    default constructor,copy constructor,copy assignment
    memset
    strcpy vs memcpy
  • 原文地址:https://www.cnblogs.com/ToddLai/p/2287288.html
Copyright © 2011-2022 走看看