自己写的一个可逆的C#加密算法
using System;
using System.Collections.Generic;
using System.Text;
namespace CLFWeb.Community
{
/********************************************************************************************************
* 公共类(加解密)
* Copyright (C) 2008-2009 By 石家庄云天信息技术有限公司
* songjh20080616(创建标识)
* 修改标识
* 修改描述
* 修改标识
* 修改描述
*********************************************************************************************************/
public class PassWord
{
private PassWord()
{ }
/// <summary>
/// 解密程序
/// </summary>
/// <param name="dd">加密的密码</param>
/// <returns>解密后的密码</returns>
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;
}
/// <summary>
/// 加密程序
/// </summary>
/// <param name="dd">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
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<vkeylen)
{
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;
}
}
}