方法一:
    //须添加对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

转载C#加密方法的更多相关文章

  1. [转载] Java中常用的加密方法

    转载自http://www.iteye.com/topic/1122076/ 加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的 ...

  2. python文件的md5加密方法

    本文实例讲述了python文件的md5加密方法.分享给大家供大家参考,具体如下: 一.简单模式: from hashlib import md5 def md5_file(name): m = md5 ...

  3. matlab数字图像简单的加密方法

    图像加密的重要性可想而知,每个人都会有自己的小秘密,通过图像加密的方法可以保护自己的照片等的安全. 一般情况下,图像加密可以分为以下几个步骤: 1.选择图像加密算法 2.根据算法获取秘钥 3.根据保存 ...

  4. iOS里常见的几种信息编码、加密方法简单总结

    一.MD5 MD5编码是最常用的编码方法之一,是从一段字符串中通过相应特征生成一段32位的数字字母混合码. MD5主要特点是 不可逆,相同数据的MD5值肯定一样,不同数据的MD5值不一样(也不是绝对的 ...

  5. Java中常用的加密方法(JDK)

    加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...

  6. C# 加密总结 一些常见的加密方法

    C# 加密总结 一些常见的加密方法 一 散列数据 代码如下: ? private static string CalculateSHA512Hash(string input)         {   ...

  7. iOS常见的几种加密方法(base64.MD5.Token传值.系统指纹验证。。加密)

    普通加密方法是讲密码进行加密后保存到用户偏好设置中 钥匙串是以明文形式保存,但是不知道存放的具体位置 一. base64加密 base64 编码是现代密码学的基础 基本原理: 原本是 8个bit 一组 ...

  8. Node.js进阶:5分钟入门非对称加密方法

    前言 刚回答了SegmentFault上一个兄弟提的问题<非对称解密出错>.这个属于Node.js在安全上的应用,遇到同样问题的人应该不少,基于回答的问题,这里简单总结下. 非对称加密的理 ...

  9. 加密方法与HTTPS 原理详解

    一:加密方法: 1,对称加密 AES,3DES,DES等,适合做大量数据或数据文件的加解密. 2,非对称加密 如RSA,Rabin.公钥加密,私钥解密.对大数据量进行加解密时性能较低. 二:https ...

  10. 转发:C#加密方法汇总

    转自:C#加密方法汇总 方法一: //须添加对System.Web的引用 using System.Web.Security; ... /// <summary> /// SHA1加密字符 ...

随机推荐

  1. c#获取当前进程使用内存

    public static string GetMemory()        {            Process proc = Process.GetCurrentProcess();     ...

  2. ORACLE 数据库备份脚本(数据泵2-指定用户)

    #!/bin/bash export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK #定义字符集RQ=`date +20%y-%m-%d` #创建数据泵目录directory, ...

  3. 实现MybatisPlus乐观锁

    1.实体类中添加version字段及相关注解 @Version@TableField(fill = FieldFill.INSERT)//第一次添加数据时使其有个默认值1private Integer ...

  4. 几款Android 应用自动化测试工具

    本文转自:https://blog.csdn.net/hebbely/article/details/78901466 简述: 本文介绍几款流行的 Android应用自动化测试工具. Monkey测试 ...

  5. Spring框架常用依赖配置--供使用时直接复制

    Spring框架常用依赖配置--供使用时直接复制 以下仅为本人工作.学习过程中所接触到的内容,不足之处请多包涵. <properties> <org.springframework. ...

  6. HCIA-ICT实战基础11-网络地址转换NAT&NAPT

    HCIA-ICT实战基础-网络地址转换 目录 NAT概述 静态NAT 动态NAT NAPT.Easy-IP NAT Server 扩展 NAT ALG 扩展 双向NAT 扩展 重叠NAT转换方式 1 ...

  7. 「DIARY」PKUSC2021 小结

    另外有一个纯吐槽游记版本的,还没有写完(快写完了,真的) 欢迎各路神仙来吐槽一个菜鸡的考场思路 # Day1 考场小结 总体而言,T1 完全就是送分,做得也挺快的:T2 大概是本场最难的题:然后 T3 ...

  8. Java基础学习:3、数组

    1.三种声明方式: // 创建一个double数据类型的数组,长度为5 double array[] = new double[5]; // 创建int数据类型数组,长度为6 int age[] = ...

  9. STM32cubemx-HAL库串口断线问题

    STM32cubemx:version5.1 Chip: STM32F446RE IDE:Keil5 Q:小项目上写了个简单的通信包,波特率230400,数据量较大1600Byte/s,DMA的方式实 ...

  10. 内存模型和名称空间复习题(c++ prime plus )

    第一题 注意到setgolf()被重载,可以这样使用其第一个版本: golf ann; setgolf(ann, "Ann Birdfree", 24); 上述函数调用提供了存储在 ...