很简单的加密解密,但不知道有没有版权哟。我也是原文转抄别人的,贴出来给大家看看。^_^using System;namespace Watchdog
{
/// <summary>
/// EnCryptAndDeCrypt 的摘要说明。
/// </summary>
public class EnCryptAndDeCrypt
{
private String g_Key; //加密的钥匙
private String strContent; //要加密的内容
private static int iKeyLength=256; //密钥的长度 public EnCryptAndDeCrypt()
{ } public String Key
{
set
{
g_Key=value;
} get
{
return g_Key;
}
} /**
* 加密
* @param strEncrypted:需要加密的字符串
* @return:加密过后的字符串
*/
public string EnCrypt(String strCryptThis)
{ String strEncrypted="";
char strChar,iCryptChar, iKeyChar, iStringChar;
for(int i=0;i<strCryptThis.Length;i++)
{
iKeyChar=g_Key[i];
iStringChar=strCryptThis[i];
iCryptChar=(char)(iKeyChar^iStringChar);
strEncrypted = strEncrypted + iCryptChar;
} return strEncrypted;
} /**
* 解密
* @param strEncrypted:需要解密的字符串
* @return:解密过后的字符串
*/
public String DeCrypt(String strEncrypted)
{
char strChar, iKeyChar, iStringChar,iDeCryptChar;
String strDecrypted="";
for(int i=0;i<strEncrypted.Length;i++)
{
iKeyChar=g_Key[i];
iStringChar=strEncrypted[i];
iDeCryptChar=(char)(iKeyChar^iStringChar);
strDecrypted=strDecrypted + iDeCryptChar;
} return strDecrypted;
} /**
* 生成本次加密或解密的钥匙
* @return
*/ public static String KeyGeN()
{
int k;
int iCount;
String strMyKey="";
int lowerbound = 65;
int upperbound = 96;
Random rd=new Random();
for(int i=1;i<iKeyLength;i++)
{
//rd=new Random(i);
//k =(int)(((upperbound - lowerbound) + 1)* rd.Next() + lowerbound; k=(int)rd.Next(lowerbound,upperbound);
strMyKey=strMyKey + (char)k; }
return strMyKey;
} /**
* 测试
* @param args
public static void main(String[] args)
{
String key=EnCryptAndDeCrypt.KeyGeN();
String str="I am stone.xxxx sdfsdfsd.,dffsdfsdfsdf sdfwe3e";
EnCryptAndDeCrypt test=new EnCryptAndDeCrypt(); test.setKey(key);
System.out.println(test.EnCrypt(str));
System.out.println(test.DeCrypt(test.EnCrypt(str)));
}
*/
}
}
{
/// <summary>
/// EnCryptAndDeCrypt 的摘要说明。
/// </summary>
public class EnCryptAndDeCrypt
{
private String g_Key; //加密的钥匙
private String strContent; //要加密的内容
private static int iKeyLength=256; //密钥的长度 public EnCryptAndDeCrypt()
{ } public String Key
{
set
{
g_Key=value;
} get
{
return g_Key;
}
} /**
* 加密
* @param strEncrypted:需要加密的字符串
* @return:加密过后的字符串
*/
public string EnCrypt(String strCryptThis)
{ String strEncrypted="";
char strChar,iCryptChar, iKeyChar, iStringChar;
for(int i=0;i<strCryptThis.Length;i++)
{
iKeyChar=g_Key[i];
iStringChar=strCryptThis[i];
iCryptChar=(char)(iKeyChar^iStringChar);
strEncrypted = strEncrypted + iCryptChar;
} return strEncrypted;
} /**
* 解密
* @param strEncrypted:需要解密的字符串
* @return:解密过后的字符串
*/
public String DeCrypt(String strEncrypted)
{
char strChar, iKeyChar, iStringChar,iDeCryptChar;
String strDecrypted="";
for(int i=0;i<strEncrypted.Length;i++)
{
iKeyChar=g_Key[i];
iStringChar=strEncrypted[i];
iDeCryptChar=(char)(iKeyChar^iStringChar);
strDecrypted=strDecrypted + iDeCryptChar;
} return strDecrypted;
} /**
* 生成本次加密或解密的钥匙
* @return
*/ public static String KeyGeN()
{
int k;
int iCount;
String strMyKey="";
int lowerbound = 65;
int upperbound = 96;
Random rd=new Random();
for(int i=1;i<iKeyLength;i++)
{
//rd=new Random(i);
//k =(int)(((upperbound - lowerbound) + 1)* rd.Next() + lowerbound; k=(int)rd.Next(lowerbound,upperbound);
strMyKey=strMyKey + (char)k; }
return strMyKey;
} /**
* 测试
* @param args
public static void main(String[] args)
{
String key=EnCryptAndDeCrypt.KeyGeN();
String str="I am stone.xxxx sdfsdfsd.,dffsdfsdfsdf sdfwe3e";
EnCryptAndDeCrypt test=new EnCryptAndDeCrypt(); test.setKey(key);
System.out.println(test.EnCrypt(str));
System.out.println(test.DeCrypt(test.EnCrypt(str)));
}
*/
}
}
解决方案 »
- C# webBrowser操作小问题!
- 如何在执行询环查询操作中,更新窗体中的表格数据和状态条的状态?
- ASP.net中做Excle报表
- msde 2000 的osql怎么运行后窗口一闪就没了?
- 高分求:关于和C++类型对应的问题
- 命名空间问题
- 为什么在装了点NET2003的环境下可用,在只装框架的系统下报错,大虾救救我!
- Visual Studio环境下Windows应用程序和智能设备有何异同?
- 谁能帮助我这样技术弱势群体?这个贴再没人解答,我就死给你们看!!!!!!!!
- 大家看看我这个问题咋解决啊..这个问题解决不了我的程序没法子进行下去啦
- 用C#编程实现DTS包动态创建和保存???!!!!
- 怪问题呀~~~~~~~~~
FileStream fs = new FileStream("EncryptedFile.txt",FileMode.Create,FileAccess.Write);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform desencrypt = des.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fs,desencrypt,CryptoStreamMode.Write);
cryptostream.Write(bytearrayinput,0,bytearrayinput.Length);
cryptostream.Close();
Dim fs As New FileStream("EncryptedFile.txt", FileMode.Create, FileAccess.Write)
Dim des As New DESCryptoServiceProvider()
Dim desencrypt As ICryptoTransform = des.CreateEncryptor()
Dim cryptostream As New CryptoStream(fs, desencrypt, CryptoStreamMode.Write)
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()