当前位置: 代码迷 >> ASP.NET >> 急求C#里面怎么加密解密
  详细解决方案

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

热度:1766   发布时间:2013-02-25 00:00:00.0
急求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);
  相关解决方案