Code:
static string Encrypt(string toEncrypt, string passwd) {
try
{
int offset = 0;
for (int i = 0; i < passwd.Length; i++)
{
bool subtract = i % 2 == 1;
if (!subtract)
{
offset += passwd.ToCharArray()[i];
}
else
{
offset -= passwd.ToCharArray()[i];
}
}
if (offset == 0)
{
for (int i = 0; i < passwd.Length - 1; i++)
{
bool subtract = i % 2 == 1;
if (!subtract)
{
offset += passwd.ToCharArray()[i];
}
else
{
offset -= passwd.ToCharArray()[i];
}
}
}
Char[] ret = new Char[toEncrypt.Length];
for(int n = 0; n < toEncrypt.Length; n++)
{
ret[n] = Convert.ToChar(toEncrypt[n] + offset);
}
return new string(ret);
}
catch (Exception ex)
{
return Encrypt(toEncrypt, passwd);
}
}
static string Decrypt(string toDecrypt, string passwd)
{
try
{
int offset = 0;
for (int i = 0; i < passwd.Length; i++)
{
bool subtract = i % 2 == 1;
if (!subtract)
{
offset += passwd.ToCharArray()[i];
}
else
{
offset -= passwd.ToCharArray()[i];
}
}
if (offset == 0)
{
for (int i = 0; i < passwd.Length - 1; i++)
{
bool subtract = i % 2 == 1;
if (!subtract)
{
offset += passwd.ToCharArray()[i];
}
else
{
offset -= passwd.ToCharArray()[i];
}
}
}
Char[] ret = new Char[toDecrypt.Length];
for (int n = 0; n < toDecrypt.Length; n++)
{
ret[n] = Convert.ToChar(toDecrypt[n] - offset);
}
return new string(ret);
}
catch (Exception ex)
{
return Decrypt(toDecrypt, passwd);
}
} Edit: Is it because not every number has an character connected to it? Like 0-30 don't have a character connected (I think, Idk the ANCII table by heart)