字符串加密和解密的常类

字符串加密和解密常用类:转载至:

http://www.cnblogs.com/malaohu/p/3214136.html

 

public sealed class EncryptUtils

{

#region Base64加密解密

/// <summary>

/// Base64加密

/// </summary>

/// <param name="input">需要加密的字符串</param>

/// <returns></returns>

public static string Base64Encrypt(string input)

{

return Base64Encrypt(input, new UTF8Encoding());

}

/// <summary>

/// Base64加密

/// </summary>

/// <param name="input">需要加密的字符串</param>

/// <param name="encode">字符编码</param>

/// <returns></returns>

public static string Base64Encrypt(string input, Encoding encode)

{

return Convert.ToBase64String(encode.GetBytes(input));

}

/// <summary>

/// Base64解密

/// </summary>

/// <param name="input">需要解密的字符串</param>

/// <returns></returns>

public static string Base64Decrypt(string input)

{

return Base64Decrypt(input, new UTF8Encoding());

}

/// <summary>

/// Base64解密

/// </summary>

/// <param name="input">需要解密的字符串</param>

/// <param name="encode">字符的编码</param>

/// <returns></returns>

public static string Base64Decrypt(string input, Encoding encode)

{

return encode.GetString(Convert.FromBase64String(input));

}

#endregion

#region DES加密解密

/// <summary>

/// DES加密

/// </summary>

/// <param name="data">加密数据</param>

/// <param name="key">8位字符的密钥字符串</param>

/// <param name="iv">8位字符的初始化向量字符串</param>

/// <returns></returns>

public static string DESEncrypt(string data, string key, string iv)

{

byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);

byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

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

}

/// <summary>

/// DES解密

/// </summary>

/// <param name="data">解密数据</param>

/// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param>

/// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>

/// <returns></returns>

public static string DESDecrypt(string data, string key, string iv)

{

byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);

byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

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

}

#endregion

#region MD5加密

/// <summary>

/// MD5加密

/// </summary>

/// <param name="input">需要加密的字符串</param>

/// <returns></returns>

public static string MD5Encrypt(string input)

{

return MD5Encrypt(input, new UTF8Encoding());

}

/// <summary>

/// MD5加密

/// </summary>

/// <param name="input">需要加密的字符串</param>

/// <param name="encode">字符的编码</param>

/// <returns></returns>

public static string MD5Encrypt(string input, Encoding encode)

{

MD5 md5 = new MD5CryptoServiceProvider();

byte[] t = md5.ComputeHash(encode.GetBytes(input));

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

}

/// <summary>

/// MD5对文件流加密

/// </summary>

/// <param name="sr"></param>

/// <returns></returns>

public static string MD5Encrypt(Stream stream)

{

MD5 md5serv = MD5CryptoServiceProvider.Create();

byte[] buffer = md5serv.ComputeHash(stream);

StringBuilder sb = new StringBuilder();

foreach (byte var in buffer)

sb.Append(var.ToString("x2"));

return sb.ToString();

}

/// <summary>

/// MD5加密(返回16位加密串)

/// </summary>

/// <param name="input"></param>

/// <param name="encode"></param>

/// <returns></returns>

public static string MD5Encrypt16(string input, Encoding encode)

{

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

string result = BitConverter.ToString(md5.ComputeHash(encode.GetBytes(input)), 4, 8);

result = result.Replace("-", "");

return result;

}

#endregion

#region 3DES 加密解密

public static string DES3Encrypt(string data, string key)

{

TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

DES.Key = ASCIIEncoding.ASCII.GetBytes(key);

DES.Mode = CipherMode.CBC;

DES.Padding = PaddingMode.PKCS7;

ICryptoTransform DESEncrypt = DES.CreateEncryptor();

byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(data);

return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

}

public static string DES3Decrypt(string data, string key)

{

TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

DES.Key = ASCIIEncoding.ASCII.GetBytes(key);

DES.Mode = CipherMode.CBC;

DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

ICryptoTransform DESDecrypt = DES.CreateDecryptor();

string result = "";

try

{

byte[] Buffer = Convert.FromBase64String(data);

result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

}

catch (Exception e)

{

}

return result;

}

#endregion

}

时间: 2024-10-03 22:51:44

字符串加密和解密的常类的相关文章

java字符串加密与解密

原文:java字符串加密与解密 源代码下载地址:http://www.zuidaima.com/share/1557602896464896.htm 这段时间正好系统中有需要对密码进行加密,还要能进行解密,支持自定义密钥.现分享给大家,希望能帮助到牛牛们. 有些人可能会遇到com.sun.crypto.provider.SunJCE报错,是由于编译器(jdk1.7以下)对必须得库有一定的限制,因此无法构造改函数,解决方法是设置一下编译器的报错级别,具体的解决方法可以参照这篇文章: http://

asp.net字符串加密与解密

1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlContro

C#/JS AES字符串加密和解密

往往我们有一种需求:在页面端实现对即将传入到后台端的某些字符串进行加密,然后在后台端对传入进来的字符串做解密.在一些有安全要求的数据传输上会用到此种方式 下面分别列出js端和后台端的加密或解密代码. js端加密解密 aes.js: /* CryptoJS v3.1.2 code.google.com/p/crypto-js (c) 2009-2013 by Jeff Mott. All rights reserved. code.google.com/p/crypto-js/wiki/Licen

C# 加密(Encrypt) 解密(Decrypt) 操作类 java与 C# 可以相互加密解密

public sealed class EncryptUtils { #region Base64加密解密 /// <summary> /// Base64加密 /// </summary> /// <param name="input">需要加密的字符串</param> /// <returns></returns> public static string Base64Encrypt(string input)

php 字符串加密与解密

/** * @param $data 需要加密的字符串 * @param $key 加密的密码 * @return string 加密后的字符串 */function _encrypt($data, $key){ $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); $char=''; $str=''; for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; }

字符串的加密与解密(一)

    前段时间自己琢磨着写个给字符串加密和解密的小程序,在网上找了一些代码,稍做整理,记录如下:     一.不可逆加密     1.MD5     (之前一直以为经过MD5加密是不可逆的,今天在网上竟然找到个网址可以解密,试了一下,真的可以哦!) /// <summary> /// 使用MD5加密字符串 /// </summary> /// <param name="source">待加密的字符串</param> /// <re

C#一个字符串的加密与解密

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO;   namespace ConsoleApplication1 {     class Program     {         static string encryptKey = "Oyea";    //

PHP对字符串加密解密的函数

加密解密在一个系统中的应用是非常常见的需求,PHP做的网站中,也会经常用到一些加密解密的时候.下面介绍一个比较好用的加密解密函数,收藏下,以后会用得到. <?php  $id = 132; $token = encrypt($id, 'E', 'nowamagic'); echo '加密:'.encrypt($id, 'E', 'nowamagic'); echo '<br />'; echo '解密:'.encrypt($token, 'D', 'nowamagic'); /*****

字符串的加密与解密

加密过程涉及到四个对象:明文.StreamWriter.CryptoStream.MemoryStream 那么以上四个对象是如何联系起来的呢? MemoryStream是尾,明文是头 整个加密过程完成后,暗文在MemoryStream中. 逆向理解下加密过程...... 1.首先在内存中生存MemoryStream对象: MemoryStream ms = new MemoryStream(); 2.告诉CryptoStream,把加密后的字节流存储到MemoryStream中 上面的"存储&