using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EncryptionSample
{
class Program
{
static void Main(string[] args)
{
string asd = PassWord.JiaMi("kevin");
string asd2 = PassWord.JieMi(asd);
Console.WriteLine("加密之后: "+asd);
Console.WriteLine("***************");
Console.WriteLine("解密之后: "+asd2);
}
}
public class PassWord
{
private PassWord()
{ }
///
/// 解密程序
///
/// 加密的密码
///
public static string JieMi(string dd)
{
string vkey = "zhangzengbao";
int vkeylen = vkey.Length;
int vKeyPos = 0;
int vSrcPos = 0;
int vSrcAsc = 0;
int vTmpSrcAsc = 0;
string vDest = "";
string ff = dd.Substring(0, 2);
int ddddd = Convert.ToInt32(ff, 16);
string fff = Convert.ToString(ddddd, 10);
int vOffset = Convert.ToInt32(fff);
vSrcPos = 2;
while (vSrcPos < dd.Length)
{
int eee = Convert.ToInt32(dd.Substring(vSrcPos, 2), 16);
vSrcAsc = Convert.ToInt32(Convert.ToString(eee, 10));
if (vKeyPos < vkeylen)
{
vKeyPos = vKeyPos + 1;
}
else
{
vKeyPos = 1;
}
char ddddff = (vkey[vKeyPos - 1]);
//vKeyPos = vKeyPos - 1;
vTmpSrcAsc = vSrcAsc ^ (int)(vkey[vKeyPos - 1]);
if (vTmpSrcAsc <= vOffset)
{
vTmpSrcAsc = 255 + vTmpSrcAsc - vOffset;
}
else
{
vTmpSrcAsc = vTmpSrcAsc - vOffset;
}
vDest = vDest + (char)(vTmpSrcAsc);
vOffset = vSrcAsc;
vSrcPos = vSrcPos + 2;
}
return vDest;
}
///
/// 加密程序
///
/// 要加密的字符串
///
public static string JiaMi(string dd)
{
string vkey = "zhangzengbao";
int vkeylen = vkey.Length;
int vKeyPos = 0;
int vSrcAsc = 0;
int vRange = 256;
int voffset = 0;
string vDest = "";
Random ro = new Random();
voffset = ro.Next(vRange);
//voffset = ro.Next(17, 256);
vDest = Convert.ToString(voffset, 16);
if (vDest.Length < 2)
{
vDest = "0" + vDest;
}
for (int i = 0; i < dd.Length; i++)
{
//byte[] B = Encoding.ASCII.GetBytes(a);
// byte[] b = Encoding.ASCII.GetBytes(dd[i].ToString());
// int bb =(int) b[0];
vSrcAsc = ((int)dd[i] + voffset) % 255;//求模
// vSrcAsc = (bb + voffset) % 255;
if (vKeyPos
vKeyPos = vKeyPos + 1;
}
else
{
vKeyPos = 1;
}
vRange = (int)(vkey[vKeyPos-1]);
//byte[] c = Encoding.ASCII.GetBytes(vkey[vKeyPos - 1].ToString());
// int cc = (int)c[0];
//vRange = cc;
vSrcAsc=vSrcAsc ^ vRange;
string v1=Convert.ToString(vSrcAsc, 16);
if (v1.Length < 2)
{
v1 = "0" + v1;
}
vDest = vDest +v1;
voffset=vSrcAsc;
}
vDest = vDest.ToUpper();
return vDest;
}
}
}

