1、方法一 (不可逆加密) srxljl

public string EncryptPassword(string PasswordString,string PasswordFormat ) 
   { 
     string   encryptPassword = null;
     if (PasswordFormat="SHA1"){ 
           encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1"); 
         } 
         elseif (PasswordFormat="MD5") 
     {

      encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5"); 
         }
    return encryptPassword ;
}

2、方法二 (可逆加密)srxljl

public interface IBindesh
{
    string encode(string str);
    string decode(string str);
}

public class EncryptionDecryption : IBindesh
    {
        public string encode(string str)
        {
            string htext = "";

for ( int i = 0; i < str.Length; i++)
            {
                      htext = htext + (char) (str[i] + 10 - 1 * 2);
                  }
            return htext;
              }

public string decode(string str)
        {
            string dtext = "";

for ( int i=0; i < str.Length; i++)
            {
                      dtext = dtext + (char) (str[i] - 10 + 1*2);
                  }
            return dtext;
              }

3、方法三 (可逆加密)srxljl

const string KEY_64 = "VavicApp";//注意了,是8个字符,64位

const string IV_64 = "VavicApp"; 
public string Encode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
                  MemoryStream ms = new MemoryStream();
                  CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

StreamWriter sw = new StreamWriter(cst);
                  sw.Write(data);
                  sw.Flush();
                  cst.FlushFinalBlock();
                  sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

}

public string Decode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

byte[] byEnc;
            try
            {
                      byEnc = Convert.FromBase64String(data);
                  }
            catch
            {
                return null;
                  }

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                  MemoryStream ms = new MemoryStream(byEnc);
                  CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                  StreamReader sr = new StreamReader(cst);
            return sr.ReadToEnd();
              }

4,md5(32位加密) srxljl

public string GetMD5(string s, string _input_charset)
    {

/// <summary>
        /// 与ASP兼容的MD5加密算法
        /// </summary>

MD5 md5 = new MD5CryptoServiceProvider();
        byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
             StringBuilder sb = new StringBuilder(32);
        for (int i = 0; i < t.Length; i++)
        {
                 sb.Append(t[i].ToString("x").PadLeft(2, '0'));
             }
        return sb.ToString();
         }

(16位加密)srxljl

public static string GetMd5Str(string ConvertString)
    {
             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        string t2 =BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
             t2 = t2.Replace("-", "");
        return t2;
         }

5、加解文本文件srxljl

//加密文件
    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();
         }

c#加密 可逆与不可逆MD5 加密的更多相关文章

  1. 16位的MD5加密和32位MD5加密的区别

    16位的MD5加密和32位MD5加密的区别 MD5加密后所得到的通常是32位的编码,而在不少地方会用到16位的编码它们有什么区别呢?16位加密就是从32位MD5散列中把中间16位提取出来!其实破解16 ...

  2. IOS中把字符串加密/IOS中怎么样MD5加密/IOS中NSString分类的实现

    看完过后,你会学到: 1学习IOS开发中的分类实现, 2以及类方法的书写, 3以及字符串的MD5加密/解密. ---------------------------wolfhous---------- ...

  3. java实现DES加密与解密,md5加密

    很多时候要对秘要进行持久化加密,此时的加密采用md5.采用对称加密的时候就采用DES方法了 import java.io.IOException; import java.security.Messa ...

  4. NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密

    本文列举了    数据加密算法(Data Encryption Algorithm,DEA) 密码学中的高级加密标准(Advanced EncryptionStandard,AES)RSA公钥加密算法 ...

  5. md5 32位 加密原理 Java实现md5加密

    md5 32位 加密原理 简单概括起来,MD5 算法的过程分为四步:处理原文,设置初始值,循环加工,拼接结果. 第一步:处理原文 首先,我们计算出原文长度(bit)对 512 求余的结果,如果不等于 ...

  6. IOS常见的加密方法,常用的MD5和Base64

    iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...

  7. Android数据加密之MD5加密

    前言: 项目中无论是密码的存储或者说判断文件是否是同一文件,都会用到MD5算法,今天来总结一下MD5加密算法. 什么是MD5加密? MD5英文全称“Message-Digest Algorithm 5 ...

  8. python-os模块及md5加密

    常用内置方法 __doc__打印注释 __package__打印所在包 __cached__打印字节码 __name__当前为主模块是__name__ == __main__ __file__打印文件 ...

  9. MD5加密处理

    无论传送过程和存储方式,都是以明文的方式,很不安全!一旦泄漏,将会造成很大的损失! 插件名称jQuery.MD5.js: /** * jQuery MD5 hash algorithm functio ...

随机推荐

  1. Android Scrollview 内部组件android:layout_height="fill_parent"无效的解决办法

    Found the solution myself in the end. The problem was not with the LinearLayout,  but with the Scrol ...

  2. 《C++ primer》--第三章

    习题3.2 什么是默认构造函数? 解答: 默认构造函数就是在没有显示提供初始化式时调用的构造函数.它由不带参数的构造函数,或者为所有形参提供默认实参的构造函数定义.如果定义某个类的变量时没有提供初始化 ...

  3. User Agent

    Android: Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; MI-ONE Plus Build/GINGERBREAD) AppleWebKit/533 ...

  4. UITextView 不左上角显示

    在Autolayout中 UITextView显示不左上角显示,修改如下 在viewDidLoad里面添加如下代码 if([[[UIDevice currentDevice] systemVersio ...

  5. SQL对字符串进行排序

    假设字符串中只由'A'.'B'.'C'.'D'组成,且长度为7.并设函数REPLICATE(<字符串>,<n>)可以创建一个<字符串>的n个副本的字符串,另外还有R ...

  6. JavaEE5 Tutorial_Servlet

    Web资源:web组件,静态web文件如图片 Web程序:可发布的Web资源集合   Web程序根目录下有个web-inf文件夹,如果只有jsp和静态资源,里面可以没有web.xml 根目录下可以直接 ...

  7. B树在数据库索引中的应用剖析

    引言 关于数据库索引,google一个oracle index,mysql index总 有大量的结果,其中很多的使用方法推荐,**索引之n条经典建议云云.笔者认为,较之借鉴,在搞清楚了自己的需求的基 ...

  8. Uva 208 - Firetruck

    [题目链接]http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&p ...

  9. JDBC学习笔记(2)——Statement和ResultSet

    Statement执行更新操作 Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句.Statement ...

  10. Define custom @Required-style annotation in Spring

    The @Required annotation is used to make sure a particular property has been set. If you are migrate ...