http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3588|回复: 0

[C#语言基础] 记录个小小的加密算法

[复制链接]
发表于 2012-7-26 09:55:30 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;
/// <summary>
/// MD5PWD 的摘要说明
/// </summary>
public class MD5PWD
{
    /// <summary>
    /// 开 发 人:苏飞
    /// 开发时间:2009年08月
    /// 主要功能:加密功能
    /// </summary>
    public MD5PWD()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="str">加密串</param>
    /// <param name="key">Key</param>
    /// <returns></returns>
    private  static string Encode(string str, string key)
    {
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
        provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
        byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
        MemoryStream stream = new MemoryStream();
        CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
        stream2.Write(bytes, 0, bytes.Length);
        stream2.FlushFinalBlock();
        StringBuilder builder = new StringBuilder();
        foreach (byte num in stream.ToArray())
        {
            builder.AppendFormat("{0:X2}", num);
        }
        stream.Close();
        return builder.ToString();
    }

    /// <summary>
    /// Des 解密 GB2312 
    /// </summary>
    /// <param name="str">Desc string</param>
    /// <param name="key">Key ,必须为8位 </param>
    /// <returns></returns>
    private  static string Decode(string str, string key)
    {
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
        provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
        byte[] buffer = new byte[str.Length / 2];
        for (int i = 0; i < (str.Length / 2); i++)
        {
            int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
            buffer = (byte)num2;
        }
        MemoryStream stream = new MemoryStream();
        CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
        stream2.Write(buffer, 0, buffer.Length);
        stream2.FlushFinalBlock();
        stream.Close();
        return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
    }

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="str">要加密的字符串</param>
    /// <returns></returns>
    public static string Encode(string str)
    {
        return MD5PWD.Encode(str, "http://www.cnblogs.com/sufei");
    }
    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="str">要解密的字符串</param>
    /// <returns></returns>
    public static string Decode(string str)
    {
        return MD5PWD.Decode(str, "http://www.cnblogs.com/sufei");
    }


}


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-5 09:52

© 2014-2021

快速回复 返回顶部 返回列表