一种简单的加密
Code
using System;
using System.Collections.Generic;
namespace md
{
class MainClass
{
public interface IBindesh
{
string encode(string str);
string decode(string str);
}
public class EncryptionDecryption : IBindesh
{
public string encode(string str)
{
string htext = "";
for(int i=0;i<str.Length;i++)
{
htext = htext + (char)(str[i] + 10 - 1 * 2);
}
return htext;
}
public string decode(string str)
{
string dtext = "";
for (int i = 0; i < str.Length; i++)
{
dtext = dtext + (char)(str[i] - 10 + 1 * 2);
}
return dtext;
}
public static void Main(string[] args)
{
EncryptionDecryption inter = new EncryptionDecryption();
string str=((IBindesh)inter).encode("admin");
Console.WriteLine(str);
Console.WriteLine(((IBindesh)inter).decode(str));
Console.Read();
}
}
}
}
using System;
using System.Collections.Generic;
namespace md
{
class MainClass
{
public interface IBindesh
{
string encode(string str);
string decode(string str);
}
public class EncryptionDecryption : IBindesh
{
public string encode(string str)
{
string htext = "";
for(int i=0;i<str.Length;i++)
{
htext = htext + (char)(str[i] + 10 - 1 * 2);
}
return htext;
}
public string decode(string str)
{
string dtext = "";
for (int i = 0; i < str.Length; i++)
{
dtext = dtext + (char)(str[i] - 10 + 1 * 2);
}
return dtext;
}
public static void Main(string[] args)
{
EncryptionDecryption inter = new EncryptionDecryption();
string str=((IBindesh)inter).encode("admin");
Console.WriteLine(str);
Console.WriteLine(((IBindesh)inter).decode(str));
Console.Read();
}
}
}
}
利用.net中提供的类进行数据DES加密
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public partial class 加密解密 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string encryptdata = "ABCD"; //要加密的字符串
string encryptkey = "12345678"; //密钥,必须为8位
StringBuilder sb = new StringBuilder();
sb.Append("要加密的数据是:"+encryptdata+"<br />");
sb.Append("加密后的数据是:"+Encrypt(encryptdata,encryptkey)+"<br />");
sb.Append("解密后的数据是:"+Decrypt(Encrypt(encryptdata,encryptkey),encryptkey)+"<br />");
Response.Write(sb.ToString());
}
#region
private string Encrypt(string strData, string strKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] bytedata = Encoding.UTF8.GetBytes(strData);
//foreach (byte b in bytedata)
//{
// Response.Write(b + "<br />");
//}
//Response.Write("<hr />");
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey); //ASCII码值
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
//foreach (byte c in des.Key)
//{
// Response.Write(c + "<br />");
//}
//Response.Write("<hr />");
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytedata, 0, bytedata.Length);
cs.FlushFinalBlock();
cs.Close();
}
//foreach (byte d in ms.ToArray())
//{
// Response.Write(d + "<br />");
//}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
des.Clear();
return str;
}
private string Decrypt(string strData, string strKey)
{
byte[] byteData = Convert.FromBase64String(strData);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(byteData, 0, byteData.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
#endregion
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public partial class 加密解密 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string encryptdata = "ABCD"; //要加密的字符串
string encryptkey = "12345678"; //密钥,必须为8位
StringBuilder sb = new StringBuilder();
sb.Append("要加密的数据是:"+encryptdata+"<br />");
sb.Append("加密后的数据是:"+Encrypt(encryptdata,encryptkey)+"<br />");
sb.Append("解密后的数据是:"+Decrypt(Encrypt(encryptdata,encryptkey),encryptkey)+"<br />");
Response.Write(sb.ToString());
}
#region
private string Encrypt(string strData, string strKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] bytedata = Encoding.UTF8.GetBytes(strData);
//foreach (byte b in bytedata)
//{
// Response.Write(b + "<br />");
//}
//Response.Write("<hr />");
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey); //ASCII码值
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
//foreach (byte c in des.Key)
//{
// Response.Write(c + "<br />");
//}
//Response.Write("<hr />");
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytedata, 0, bytedata.Length);
cs.FlushFinalBlock();
cs.Close();
}
//foreach (byte d in ms.ToArray())
//{
// Response.Write(d + "<br />");
//}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
des.Clear();
return str;
}
private string Decrypt(string strData, string strKey)
{
byte[] byteData = Convert.FromBase64String(strData);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(byteData, 0, byteData.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
#endregion
}