求用C#写的对字符串加密解密函数

Guoyuyu 2003-02-21 04:55:02
我的Email:yu0709@sina.com
...全文
44 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
TheAres 2003-02-22
  • 打赏
  • 举报
回复
看这个帖子中我的回答:

http://expert.csdn.net/Expert/topic/1237/1237771.xml?temp=.1235468
popcorn 2003-02-22
  • 打赏
  • 举报
回复
DES加密类:
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()

chinarengzy2 2003-02-22
  • 打赏
  • 举报
回复
给你发过去了,看看吧,Base64的。
lbx1979 2003-02-21
  • 打赏
  • 举报
回复
c#里好像有加密的类吧
yhcnux 2003-02-21
  • 打赏
  • 举报
回复
楼主,我已经给你发了一个文件,叫Function.cs,里面有一个Function类,你直接加入到你的项目中,然后using MyFunction,然后直接用Function.xxx("xxx")用就行了,里面有很多静态方法,其中就有加密的。
ArEoN 2003-02-21
  • 打赏
  • 举报
回复
:)

楼上的担心的有道理呀
wang852963 2003-02-21
  • 打赏
  • 举报
回复
很简单的加密解密,但不知道有没有版权哟。我也是原文转抄别人的,贴出来给大家看看。^_^

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)));
}

*/
}



}
ht_csc 2003-02-21
  • 打赏
  • 举报
回复
关注

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧