C#DES加密和解密
努力的藍精靈發表於2013-11-26
-
using System;
-
using System.IO;
-
using System.Text;
-
using System.Globalization;
-
using System.Collections.Generic;
-
using System.Security.Cryptography;
-
-
namespace EncryptionTest
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Program pr = new Program();
-
string key = pr.GenerateKey();
-
-
string s1 = "測試字串";
-
string s2 = pr.EncryptString(s1,key);
-
string s3 = pr.DecryptString(s2,key);
-
Console.WriteLine("原字串:[{0}]",s1);
-
Console.WriteLine("加密後:[{0}]", s2);
-
Console.WriteLine("解密後:[{0}]", s3);
-
-
}
-
-
-
-
-
-
public string GenerateKey()
-
{
-
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
-
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
-
}
-
-
-
-
-
-
-
public string EncryptString(string sinputString, string Skey)
-
{
-
byte[] data = Encoding.UTF8.GetBytes(sinputString);
-
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
-
DES.Key = ASCIIEncoding.ASCII.GetBytes(Skey);
-
DES.IV = ASCIIEncoding.ASCII.GetBytes(Skey);
-
ICryptoTransform desEncrypt = DES.CreateEncryptor();
-
byte[] result = desEncrypt.TransformFinalBlock(data, 0, data.Length);
-
return BitConverter.ToString(result);
-
}
-
-
-
-
-
-
-
public string DecryptString(string sinputString, string sKey)
-
{
-
string[] sinput = sinputString.Split("-".ToCharArray());
-
byte[] data = new byte[sinput.Length];
-
for (int i = 0; i < sinput.Length; i++)
-
{
-
data[i] = byte.Parse(sinput[i], NumberStyles.HexNumber);
-
}
-
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
-
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
-
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
-
ICryptoTransform desencrypt = DES.CreateDecryptor();
-
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
-
return Encoding.UTF8.GetString(result);
-
}
-
-
-
-
-
-
-
-
public string MD5Encrypt(string pToEncrypt, string sKey)
-
{
-
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
-
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
-
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);
-
cs.Write(inputByteArray, 0, inputByteArray.Length);
-
cs.FlushFinalBlock();
-
StringBuilder ret = new StringBuilder();
-
foreach (byte b in ms.ToArray())
-
{
-
ret.AppendFormat("{0:X2}", b);
-
}
-
ret.ToString();
-
return ret.ToString();
-
}
-
-
-
-
-
-
-
public string MD5Decrypt(string pToDecrypt, string sKey)
-
{
-
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
-
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);
-
cs.Write(inputByteArray, 0, inputByteArray.Length);
-
cs.FlushFinalBlock();
-
StringBuilder ret = new StringBuilder();
-
return System.Text.Encoding.Default.GetString(ms.ToArray());
-
}
-
}
-
-
-
}