3个回答
展开全部
C#有专门的类,直接调用就可以了
//类名:EcanSecurity
//作用:加密解密
//作者:刘典武
//时间:2010-12-01
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Ecan
{
public class EcanSecurity
{
/*MD5是message-digest algorithm 5(信息-摘要算法)的缩写,
* 被广泛用于加密和解密技术上,它可以说是文件的“数字指纹”。
* 任何一个文件,无论是可执行程序、图像文件、临时文件或者其他任何类型的文件,
* 也不管它体积多大,都有且只有一个独一无二的MD5信息值,并且如果这个文件被修改过,
* 它的MD5值也将随之改变。因此,我们可以通过对比同一文件的MD5值,
* 来校验这个文件是否被“篡改”过。*/
/// <summary>
/// MD5加密函数
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
///
public string md5_Encode(string str)
{
MD5 m = new MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(str);
byte[] result = m.ComputeHash(data);
string ret1 = "";
try
{
for (int j = 0; j < result.Length; j++)
{
ret1 += result[j].ToString("x").PadLeft(2, '0');
}
return ret1;
}
catch
{
return str;
}
}
/// <summary>
/// 简单加密函数
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>返回加密后的字符串</returns>
///
public string simple_Encode(string str)
{
string s = "";
try
{
for (int i = 0; i < str.Length; i++)
{
s += (char)(str[i] + 10 - 1 * 2);
}
return s;
}
catch
{
return str;
}
}
/// <summary>
/// 简单解密函数
/// </summary>
/// <param name="str">要解密的字符串</param>
/// <returns>返回解密后的字符串</returns>
///
public string simple_Decode(string str)
{
string s = "";
try
{
for (int i = 0; i < str.Length; i++)
{
s += (char)(str[i] - 10 + 1 * 2);
}
return s;
}
catch
{
return str;
}
}
//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// 对称加密法加密函数
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public string symmetry_Encode(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>
/// 对称加密法解密函数
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public string symmetry_Decode(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;
}
}
}
}
//类名:EcanSecurity
//作用:加密解密
//作者:刘典武
//时间:2010-12-01
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Ecan
{
public class EcanSecurity
{
/*MD5是message-digest algorithm 5(信息-摘要算法)的缩写,
* 被广泛用于加密和解密技术上,它可以说是文件的“数字指纹”。
* 任何一个文件,无论是可执行程序、图像文件、临时文件或者其他任何类型的文件,
* 也不管它体积多大,都有且只有一个独一无二的MD5信息值,并且如果这个文件被修改过,
* 它的MD5值也将随之改变。因此,我们可以通过对比同一文件的MD5值,
* 来校验这个文件是否被“篡改”过。*/
/// <summary>
/// MD5加密函数
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
///
public string md5_Encode(string str)
{
MD5 m = new MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(str);
byte[] result = m.ComputeHash(data);
string ret1 = "";
try
{
for (int j = 0; j < result.Length; j++)
{
ret1 += result[j].ToString("x").PadLeft(2, '0');
}
return ret1;
}
catch
{
return str;
}
}
/// <summary>
/// 简单加密函数
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>返回加密后的字符串</returns>
///
public string simple_Encode(string str)
{
string s = "";
try
{
for (int i = 0; i < str.Length; i++)
{
s += (char)(str[i] + 10 - 1 * 2);
}
return s;
}
catch
{
return str;
}
}
/// <summary>
/// 简单解密函数
/// </summary>
/// <param name="str">要解密的字符串</param>
/// <returns>返回解密后的字符串</returns>
///
public string simple_Decode(string str)
{
string s = "";
try
{
for (int i = 0; i < str.Length; i++)
{
s += (char)(str[i] - 10 + 1 * 2);
}
return s;
}
catch
{
return str;
}
}
//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// 对称加密法加密函数
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public string symmetry_Encode(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>
/// 对称加密法解密函数
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public string symmetry_Decode(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;
}
}
}
}
展开全部
//加密方法.
private string DESEncrypt(string strInput, string strKey) {
try {
byte[] bytesData = Encoding.UTF8.GetBytes(strInput); //加密明文.
byte[] bytesVector = keysVector; //加密向量.
byte[] bytesKey = Encoding.UTF8.GetBytes(strKey); //密匙.
DESCryptoServiceProvider encoder = new DESCryptoServiceProvider();
//用using可以确保流的关闭.
using(MemoryStream mStream = new MemoryStream()) {
CryptoStream cStream = new CryptoStream(mStream, encoder.CreateEncryptor(bytesKey, bytesVector), CryptoStreamMode.Write);
cStream.Write(bytesData, 0, bytesData.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
//Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits.
}
}
catch(System.Exception ex) {
return ex.Message;
}
}
//解密方法.
private string DESDecrypt(string strInput, string strKey) {
try {
byte[] bytesData = Convert.FromBase64String(strInput); //加密了的字符串.
// Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.
byte[] bytesKey = Encoding.UTF8.GetBytes(strKey);
byte[] bytesVector = keysVector; //加密向量.
DESCryptoServiceProvider decoder = new DESCryptoServiceProvider();
using(MemoryStream mStream = new MemoryStream()) {
CryptoStream cStream = new CryptoStream(mStream, decoder.CreateDecryptor(bytesKey, bytesVector), CryptoStreamMode.Write);
cStream.Write(bytesData, 0, bytesData.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
//When overridden in a derived class, decodes all the bytes in the specified byte array into a string.
}
}
catch(System.Exception ex) {
return ex.Message;
}
}
DES加密解密不难..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-10
展开全部
百度一搜一大堆的东西 你们这些人怎么就这么懒 连复制粘贴都不愿意
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询