方法一:
    //须添加对System.Web的引用
    using System.Web.Security;
     
    ...
     
    /// <summary>
    /// SHA1加密字符串
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <returns>加密后的字符串</returns>
    public string SHA1(string source)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "SHA1");
    }
 
 
    /// <summary>
    /// MD5加密字符串
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <returns>加密后的字符串</returns>
    public string MD5(string source)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");;
    }

方法五:
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
     
    ...
     
    //默认密钥向量
    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="encryptString">待加密的字符串</param>
    /// <param name="encryptKey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
    public static string EncryptDES(string encryptString, string encryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            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
        {
            return encryptString;
        }
    }
     
    /// <summary>
    /// DES解密字符串
    /// </summary>
    /// <param name="decryptString">待解密的字符串</param>
    /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
    /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
    public static string DecryptDES(string decryptString, string decryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch
        {
            return decryptString;
        }
    }

方法六(文件加密):
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
     
    ...
     
    //加密文件
    private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
    {
        //Create the file streams to handle the input and output files.
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        fout.SetLength(0);
     
        //Create variables to help with read and write.
        byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
        long rdlen = 0;              //This is the total number of bytes written.
        long totlen = fin.Length;    //This is the total length of the input file.
        int len;                     //This is the number of bytes to be written at a time.
     
        DES des = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
     
        //Read from the input file, then encrypt and write to the output file.
        while (rdlen < totlen)
        {
            len = fin.Read(bin, 0, 100);
            encStream.Write(bin, 0, len);
            rdlen = rdlen + len;
        }
     
        encStream.Close();
        fout.Close();
        fin.Close();
    }
     
    //解密文件
    private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
    {
        //Create the file streams to handle the input and output files.
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        fout.SetLength(0);
     
        //Create variables to help with read and write.
        byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
        long rdlen = 0;              //This is the total number of bytes written.
        long totlen = fin.Length;    //This is the total length of the input file.
        int len;                     //This is the number of bytes to be written at a time.
     
        DES des = new DESCryptoServiceProvider();
        CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
     
        //Read from the input file, then encrypt and write to the output file.
        while (rdlen < totlen)
        {
            len = fin.Read(bin, 0, 100);
            encStream.Write(bin, 0, len);
            rdlen = rdlen + len;
        }
     
        encStream.Close();
        fout.Close();
        fin.Close();
 
}

原文地址:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/30/1659687.html

最新文章

  1. 分享50款 Android 移动应用程序图标【上篇】
  2. 向tiny6410移植tslib(tslib-1.4)
  3. 精通javascript(看书笔记)
  4. 【原】Oracle11gR2图文安装
  5. SVN中trunk、branches、tags用法详解
  6. wampserver
  7. 关于mysql数据库在输入密码后,滴的一声直接退出界面的解决办法
  8. STL中vector容器实现反转(reverse)
  9. 几种placeholder替换项目参数的方法比较
  10. hdu 3530 Subsequence 单调队列
  11. eclipse的SVN插件的配置
  12. hdu4489(递推dp)
  13. Oracle 基于 RMAN 的不完全恢复(incomplete recovery by RMAN)
  14. 打开Openstack dashboard出现Internal Server Error
  15. 二 APPIUM Android自动化 环境搭建
  16. 【luogu P4007 清华集训2017】小Y和恐怖奴隶主
  17. 解决 RtlCreateActivationContext() failed 0xc000000d
  18. Python算法与数据结构--求所有子数组的和的最大值
  19. 2019微信浏览器跳转外部浏览器下载app打开任意站实现方法
  20. FPGA笔试必会知识点1--数字电路基本知识

热门文章

  1. CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
  2. 苹果电脑快捷键使用 Mac快捷键大全详细介绍
  3. ansible-file
  4. 项目启用eslint 检测工具
  5. 27 python 发送短信
  6. 一个分布式websocket的实现
  7. bzoj 3532
  8. Linux ~ jenkins 直接安装
  9. 一个线程池的c++实现
  10. SAP ABAP 验证与替代