62,268
社区成员
发帖
与我相关
我的任务
分享
//Sourcein是要进行加密的字符串
public static string MD5(string Sourcein)
{
MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider();
byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);
byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);
return Convert.ToBase64String(MD5Out);
}
//AES加密函数
public static string Encrypt(string toEncrypt) {
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
//AES解密函数
public static string Decrypt(string toDecrypt) {
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace Test
{
/// <summary>
/// NewDES 的摘要描述。
/// </summary>
public class NewDES
{
public NewDES()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
//加密鑰匙
private static byte[] DESKey=new byte[]{11,23,93,102,72,41,18,12};
//解密鑰匙
private static byte[] DESIV=new byte[]{75,158,46,97,78,57,17,36};
public string Encode(string Encode_String)
{
DESCryptoServiceProvider objDES=new DESCryptoServiceProvider();
MemoryStream objMemoryStream=new MemoryStream();
CryptoStream objCryptoStream=new CryptoStream(objMemoryStream,objDES.CreateEncryptor(DESKey,DESIV),CryptoStreamMode.Write);
StreamWriter objStreamWriter=new StreamWriter(objCryptoStream);
objStreamWriter.Write(Encode_String);
objStreamWriter.Flush();
objCryptoStream.FlushFinalBlock();
objMemoryStream.Flush();
return Convert.ToBase64String(objMemoryStream.GetBuffer(),0,(int)objMemoryStream.Length);
}
public string Decode(string Encode_String)
{
DESCryptoServiceProvider objDES=new DESCryptoServiceProvider();
byte[] Input=Convert.FromBase64String(Encode_String);
MemoryStream objMemoryStream=new MemoryStream(Input);
CryptoStream objCryptoStream=new CryptoStream (objMemoryStream,objDES.CreateDecryptor(DESKey,DESIV),CryptoStreamMode.Read);
StreamReader objStreamReader=new StreamReader(objCryptoStream);
return objStreamReader.ReadToEnd();
}
}
}