Basit Şifreleme ve Çözme

static Random r = new Random();

private static string Encode(string text)
{
     var key = r.Next(10000, 34465);

     var chars = text.ToCharArray();

     var encoded = string.Empty;

     for (int i = 0; i < chars.Length; i++)
     {
          encoded += (int)chars[i] + key;
     }

     var randomIndex = r.Next(0, encoded.Length);

     encoded = encoded.Insert(randomIndex, key.ToString());

     encoded += randomIndex;

     return encoded;
}

private static string Decode(string encodedString)
{
     var randomIndexLength = encodedString.Length % 5;

     if (randomIndexLength == 0)
     {
          randomIndexLength = 5;
     }

     var randomIndex = Convert.ToInt32(encodedString.Substring(encodedString.Length - randomIndexLength));

     var key = Convert.ToInt32(encodedString.Substring(randomIndex, 5));

     var encodedBody = encodedString.Substring(0, randomIndex) + encodedString.Substring(randomIndex + 5,  encodedString.Length - randomIndexLength - randomIndex - 5);

     var decodedChars = new char[encodedBody.Length / 5];

     for (int i = 0; i < decodedChars.Length; i++)
     {
          decodedChars[i] += (char)(Convert.ToInt32(encodedBody.Substring(i * 5, 5)) - key);
     }

     return new string(decodedChars);
}

Leave a Reply

Your email address will not be published. Required fields are marked *