62,025
社区成员
发帖
与我相关
我的任务
分享
public static string MakePassword(int len)//生成长度为len的随机密码
{
string strPwChar = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string strRe = "";
int iRandNum;
Random rnd = new Random();
for (int i = 0; i < len; i++)
{
iRandNum = rnd.Next(strPwChar.Length);
strRe += strPwChar[iRandNum];
}
return strRe;
}
public static int GetJStr(string str)//将长度超过1的字符串中的所有字符编码为一个字节序列
{
if (str.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(str)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
public static string Chr(int asciiCode)//将字节序列重新编码为字符串
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
public static string GetEnStr(string str)
{
string temp = "";//存放密码
for (int i = 0; i < str.Length; i++)
{
temp += Chr(GetJStr(str.ToString()) - 1);//将字符串字节序列-1,然后重新编码成字符串给到temp
}
temp += MakePassword(4);//temp加上4位自动生成的密码
return new SymmetricMethod().Encrypto(temp);//调用SymmetricMethod类中的方法再加密(这个是你自己写的加密算法)
}