I have the following code to encode a plain text:
int AddNumber;
int AsciiNumber;
string OneChar;
string String1 = "DAVE SMITH";
string String2 = "";
for (int i = 0; i < String1.Length; i++)
{
AddNumber = i + 95;
AsciiNumber = (int)Char.Parse(String1.Substring(i,1));
byte[] NewAscii = new byte[] { Convert.ToByte( AsciiNumber + AddNumber ) };
// Get string of the NewAscii
OneChar = Encoding.GetEncoding(1252).GetString(NewAscii);
String2 = String2 + OneChar;
}
The problem I have is how to decode the string back to plain text. Here is my attempt code:
String1 = "";
for (int i = 0; i < String2.Length; i++)
{
AddNumber = i + 95;
AsciiNumber = (int)Char.Parse(String2.Substring(i,1));
byte[] NewAscii = new byte[] { Convert.ToByte( AsciiNumber - AddNumber ) };
// Get string of the NewAscii
OneChar = Encoding.GetEncoding(1252).GetString(NewAscii);
String1 = String1 + OneChar;
}
The problem is that above, on processing the encoded empty space (between DAVE and SMITH), the value AsciiNumber = (int)Char.Parse(String2.Substring(i,1)) is 402 where it should be 131.
Do you see what I am misunderstanding?
For the decoding part
String1 = "";
for (int i = 0; i < String2.Length; i++)
{
var charByte = System.Text.Encoding.GetEncoding(1252).GetBytes(String2.Substring(i, 1));
AddNumber = i + 95;
AsciiNumber = Convert.ToInt32(charByte[0]) - AddNumber;
String1 += Convert.ToChar(AsciiNumber);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments