首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

急求C#里面怎么加密解密

2012-01-06 
急求C#里面如何加密解密!如题,ASP里面有MD5的,但是不能解密,看了看,加密解密还挺麻烦,什么向量之类的,那个

急求C#里面如何加密解密!
如题,ASP里面有MD5的,但是不能解密,
看了看,加密解密还挺麻烦,什么向量之类的,那个高手能讲解一些阿!

100分   在线等!

[解决办法]
向量就相当于密钥吧


public class Sec
{
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
private static string KeyCode = "xxxxxx ";

public static string EnCode(string encryptString)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(KeyCode);
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;
}
}

public static string DeCode(string decryptString)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(KeyCode);
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;
}


}
}
[解决办法]
DES加密解密代码:
C#

-----------------------------------------------

//名称空间

using System;

using System.Security.Cryptography;

using System.IO;

using System.Text;


//方法

//加密方法

public string Encrypt(string pToEncrypt, string sKey)

{

DESCryptoServiceProvider des = new DESCryptoServiceProvider();

//把字符串放到byte数组中

//原来使用的UTF8编码,我改成Unicode编码了,不行

byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);


//建立加密对象的密钥和偏移量

//原文使用ASCIIEncoding.ASCII方法的GetBytes方法

//使得输入密码必须输入英文文本

des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);

des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);

//Write the byte array into the crypto stream

//(It will end up in the memory stream)

cs.Write(inputByteArray, 0, inputByteArray.Length);


cs.FlushFinalBlock();

//Get the data back from the memory stream, and into a string

StringBuilder ret = new StringBuilder();

foreach(byte b in ms.ToArray())

{

//Format as hex

ret.AppendFormat( "{0:X2} ", b);

}

ret.ToString();

return ret.ToString();

}


//解密方法

public string Decrypt(string pToDecrypt, string sKey)

{

DESCryptoServiceProvider des = new DESCryptoServiceProvider();


//Put the input string into the byte array

byte[] inputByteArray = new byte[pToDecrypt.Length / 2];

for(int x = 0; x < pToDecrypt.Length / 2; x++)

{

int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));

inputByteArray[x] = (byte)i;

}


//建立加密对象的密钥和偏移量,此值重要,不能修改

des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);

des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);

//Flush the data through the crypto stream into the memory stream

cs.Write(inputByteArray, 0, inputByteArray.Length);

cs.FlushFinalBlock();


//Get the decrypted data back from the memory stream

//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象

StringBuilder ret = new StringBuilder();



return System.Text.Encoding.Default.GetString(ms.ToArray());

}
[解决办法]
一个加解密的类。
/// <summary>
/// RC2加解密类
/// </summary>
public class RC2_
{
private RC2 rc;
public string Key;
public string IV;
/// <summary>
/// 对称加密类的构造函数
/// </summary>
public RC2_(string key)
{
rc = new RC2CryptoServiceProvider();
Key = key;
IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(* ";
}
/// <summary>
/// 对称加密类的构造函数
/// </summary>
public RC2_(string key, string iv)
{
rc = new RC2CryptoServiceProvider();
Key = key;
IV = iv;
}
/// <summary>
/// 获得密钥
/// </summary>
/// <returns> 密钥 </returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
rc.GenerateKey();
byte[] bytTemp = rc.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}


/// <summary>
/// 获得初始向量IV
/// </summary>
/// <returns> 初试向量IV </returns>
private byte[] GetLegalIV()
{
string sTemp = IV;
rc.GenerateIV();
byte[] bytTemp = rc.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name= "Source "> 待加密的串 </param>
/// <returns> 经过加密的串 </returns>
public string Encrypt(string Source)
{
try
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
ICryptoTransform encrypto = rc.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception( "在文件加密的时候出现错误!错误提示: \n " + ex.Message);
}
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name= "Source "> 待解密的串 </param>
/// <returns> 经过解密的串 </returns>
public string Decrypt(string Source)
{
try
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
ICryptoTransform encrypto = rc.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception( "在文件解密的时候出现错误!错误提示: \n " + ex.Message);
}
}
}
[解决办法]
using System;
using System.Text;
using System.Security.Cryptography;


/// <summary>
/// 根据输入的算法名进行运算,返回带字母的结果
/// </summary>
/// <param name= "value "> 进行加密的字符串 </param>
/// <param name= "EncryptName "> 算法名
/// 举例:MD5,SHA1,SHA256,SHA384,SHA512,RIPEMD160
/// </param>
/// <returns> 加密结果 </returns>
public static string EncryptValue(string value,string EncryptName)
{
byte[] hashvalue = (CryptoConfig.CreateFromName(EncryptName) as HashAlgorithm).ComputeHash(bytes(value));

StringBuilder text = new StringBuilder();
for (int i = 0, j = hashvalue.Length; i < j; i++)
text.Append(hashvalue[i].ToString( "x ").PadLeft(2, '0 '));


return text.ToString();
}

/// <summary>
/// 根据指定的值获取bytes
/// </summary>
/// <param name= "value "> 指定的值 </param>
/// <returns> bytes </returns>
public static byte[] bytes(string value)
{
return Encoding.ASCII.GetBytes(value);
}

DES,DSA,RSA速度太慢了

热点排行