>>>>>>>string cTest1 = "ABC123"; >>>>>>>string cTest1Encr; >>>>>>>string cTest1Decr; >>>>>>>cTest1Encr = Encrypt( cTest1, "123abc"); >>>>>>>cTest1Decr = Encrypt( cTest1Encr, "123abc"); >>>>>>>>>>>>>>
using System; >>using System.Collections.Generic; >>using System.Linq; >>using System.Text; >> >>namespace Cipher >>{ >> public class Cipher >> { >> private const int PwMinNum = 1000; >> >> public string Encrypt(string tcStr, string tcPassword) >> { >> byte[] tcStrBytes = Encoding. GetEncoding(1252).GetBytes(tcStr); >> int tcStrLength = tcStrBytes.Length; >> >> string lcPassword = tcPassword + ((char) 0).ToString(); >> byte[] lcPasswordBytes = Encoding.GetEncoding(1252).GetBytes(lcPassword); >> >> int lnPassLen = lcPasswordBytes.Length; >> int lnPassPos = 0; >> int lnPassNum = ((CipherGetPnum(lcPasswordBytes)/997 - 1)%254) + 1; >> >> byte[] bytes = new byte[tcStr.Length]; >> >> for (int i = 0; i < tcStrLength; i++) >> { >> int lnNum01 = ((lnPassNum + (i - tcStrLength)) - 1); >> lnPassNum = (Math.Abs(lnNum01)%254)*Math.Sign(lnNum01) + 1; >> var lnByte = tcStrBytes[i] ^ (lnPassNum ^ lcPasswordBytes[lnPassPos]); >> bytes[i] = lnByte==0 ? tcStrBytes[i] : (byte) (lnByte & 0xFF); >> lnPassPos = lnPassPos > lnPassLen ? 0 : lnPassPos + 1; >> } >> return Encoding.GetEncoding(1252).GetString(bytes); >> } >> >> private int CipherGetPnum(IEnumerable<byte> ascii) >> { >> int liRet = 1 + ascii.Select((t, i) => t + i).Sum(); >> while (liRet < PwMinNum) >> { >> liRet = liRet << 1; >> } >> return liRet; >> } >> } >>}>
int lnPassLen = lcPasswordBytes.Length - 1; //and lnPassPos = lnPassPos == lnPassLen ? 0 : lnPassPos + 1;