public string Encode(string PlainTextString) { char[] Head = new char[5]; char[] Tail = new char[5]; char[] TempChar8 = new char[8]; char[] TempChar = new char[5]; long Time; long CheckSum = 0; long LongReverse = 0; int PlainTxtLen = PlainTextString.Length; int GroupsOf5Chars = (int)Math.Floor((double)(PlainTxtLen / 5)); int EncTxtStrLen; int dx = (PlainTxtLen > 255) ? 8 : 0; ArrayChar ArrayOut = new ArrayChar(8 * GroupsOf5Chars + 24 + dx); String Out; Time = ((long)DateTime.Now.Ticks); Time = Time / 10000000L; Time = Time - BaseTime; Head = LongToCharArray(Time); TempChar8 = ToBase32(Head); ArrayOut.Add(TempChar8); CheckSum = CalcChecksum(CheckSum, Head); for (int Idx = 0; Idx < GroupsOf5Chars; Idx++) { TempChar = PlainTextString.ToCharArray(Idx * 5, 5); ReverseChunk(TempChar); LongReverse = CharArrayToLong(TempChar); TempChar8 = ToBase32(TempChar); ArrayOut.Add(TempChar8); CheckSum = CalcChecksum(CheckSum, TempChar); } EncTxtStrLen = PlainTextString.Length % 5; for (int Idx = 0; Idx < EncTxtStrLen; Idx++) { TempChar[Idx] = PlainTextString.ToCharArray()[((GroupsOf5Chars) * 5) + Idx]; } for (int Idx = EncTxtStrLen; Idx < 5; Idx++) { TempChar[Idx] = (char)(5 - Idx); } ReverseChunk(TempChar); TempChar8 = ToBase32(TempChar); ArrayOut.Add(TempChar8); CheckSum = CalcChecksum(CheckSum, TempChar); Tail = LongToCharArray(CheckSum); if (PlainTxtLen > 255) { Tail[4] = (char)0; } else { Tail[4] = (char)PlainTxtLen; } TempChar8 = ToBase32(Tail); ArrayOut.Add(TempChar8); if (PlainTxtLen > 255) { Tail = LongToCharArray(PlainTxtLen); TempChar8 = ToBase32(Tail); ArrayOut.Add(TempChar8); } Out = new String(ArrayOut.GetBuffer()); return(Out); }
public string Encode(string PlainTextString) { char[] Head = new char[5]; char[] Tail = new char[5]; char[] TempChar8 = new char[8]; char[] TempChar = new char[5]; long Time; long CheckSum = 0; long LongReverse = 0; int PlainTxtLen = PlainTextString.Length; int GroupsOf5Chars = (int)Math.Floor((double)(PlainTxtLen / 5)); int EncTxtStrLen; int dx = (PlainTxtLen > 255) ? 8 : 0; ArrayChar ArrayOut = new ArrayChar(8 * GroupsOf5Chars + 24 + dx); String Out; Time = ((long)DateTime.Now.Ticks); Time = Time / 10000000L; Time = Time - BaseTime; Head = LongToCharArray(Time); TempChar8 = ToBase32(Head); ArrayOut.Add(TempChar8); CheckSum = CalcChecksum(CheckSum, Head); for (int Idx = 0; Idx < GroupsOf5Chars; Idx++) { TempChar = PlainTextString.ToCharArray(Idx * 5, 5); ReverseChunk(TempChar); LongReverse = CharArrayToLong(TempChar); TempChar8 = ToBase32(TempChar); ArrayOut.Add(TempChar8); CheckSum = CalcChecksum(CheckSum, TempChar); } EncTxtStrLen = PlainTextString.Length % 5; for (int Idx = 0; Idx < EncTxtStrLen; Idx++) { TempChar[Idx] = PlainTextString.ToCharArray()[((GroupsOf5Chars) * 5) + Idx]; } for (int Idx = EncTxtStrLen; Idx < 5; Idx++) { TempChar[Idx] = (char)(5 - Idx); } ReverseChunk(TempChar); TempChar8 = ToBase32(TempChar); ArrayOut.Add(TempChar8); CheckSum = CalcChecksum(CheckSum, TempChar); Tail = LongToCharArray(CheckSum); if (PlainTxtLen > 255) { Tail[4] = (char)0; } else { Tail[4] = (char)PlainTxtLen; } TempChar8 = ToBase32(Tail); ArrayOut.Add(TempChar8); if (PlainTxtLen > 255) { Tail = LongToCharArray(PlainTxtLen); TempChar8 = ToBase32(Tail); ArrayOut.Add(TempChar8); } Out = new String(ArrayOut.GetBuffer()); return (Out); }
public string Decode(string EncodedTxtString, long Expire) { char[] buff = new char[6]; char[] chunk = new char[5]; char[] head = new char[5]; char[] tail = new char[5]; char[] temp = new char[8]; long EncodedTxtLen = EncodedTxtString.Length; long checksum = 0; StringBuilder strBldr = new StringBuilder(512); int dx = 0; buff[5] = (char)0; if (((EncodedTxtLen % 8) != 0) || (EncodedTxtLen < 16)) { throw (new Exception("Checksum Invalid!")); } if (EncodedTxtLen > 432) { dx = 8; } ArrayChar IncAsChar = new ArrayChar((int)EncodedTxtLen); IncAsChar.Add(EncodedTxtString.ToCharArray()); head = FromBase32(IncAsChar.Read(8)); long time = CharArrayToLong(head); checksum = CalcChecksum(checksum, head); EncodedTxtLen -= 8; while (EncodedTxtLen > (8 + dx)) { temp = IncAsChar.Read(8); chunk = FromBase32(temp); checksum = CalcChecksum(checksum, chunk); ReverseChunk(chunk); EncodedTxtLen -= 8; strBldr.Append(chunk); } tail = FromBase32(IncAsChar.Read(8)); EncodedTxtLen -= 8; int leng = (int)tail[4]; long CheckSum = CharArrayToLong(tail); if (leng == 0 && EncodedTxtLen > 0) { tail = FromBase32(IncAsChar.Read(8)); leng = (int)CharArrayToLong(tail); } if (CheckSum != checksum) { throw (new Exception("Checksum Invalid!")); } if (System.DateTime.Now.Ticks > (long)(time + Expire + BaseTime) * 10000000L) { throw (new Exception("Time expired!")); } return(strBldr.ToString(0, leng)); }
public string Decode(string EncodedTxtString, long Expire) { char[] buff = new char[6]; char[] chunk = new char[5]; char[] head = new char[5]; char[] tail = new char[5]; char[] temp = new char[8]; long EncodedTxtLen = EncodedTxtString.Length; long checksum = 0; StringBuilder strBldr = new StringBuilder(512); int dx = 0; buff[5] = (char)0; if (((EncodedTxtLen % 8) != 0) || (EncodedTxtLen < 16)) { throw (new Exception("Checksum Invalid!")); } if (EncodedTxtLen > 432) { dx = 8; } ArrayChar IncAsChar = new ArrayChar((int)EncodedTxtLen); IncAsChar.Add(EncodedTxtString.ToCharArray()); head = FromBase32(IncAsChar.Read(8)); long time = CharArrayToLong(head); checksum = CalcChecksum(checksum, head); EncodedTxtLen -= 8; while (EncodedTxtLen > (8 + dx)) { temp = IncAsChar.Read(8); chunk = FromBase32(temp); checksum = CalcChecksum(checksum, chunk); ReverseChunk(chunk); EncodedTxtLen -= 8; strBldr.Append(chunk); } tail = FromBase32(IncAsChar.Read(8)); EncodedTxtLen -= 8; int leng = (int)tail[4]; long CheckSum = CharArrayToLong(tail); if (leng == 0 && EncodedTxtLen > 0) { tail = FromBase32(IncAsChar.Read(8)); leng = (int)CharArrayToLong(tail); } if (CheckSum != checksum) { throw (new Exception("Checksum Invalid!")); } if (System.DateTime.Now.Ticks > (long)(time + Expire + BaseTime) * 10000000L) { throw (new Exception("Time expired!")); } return (strBldr.ToString(0, leng)); }