String Encryption

Posts 15 of 5 · Page 1 of 1
String Encryption
I have a problem
When I encrypt a string with my encrypter (see below), it usually returns a good string.

Like:


But sometimes it return quite a few question marks, which is pretty weird, as it's not possible to get 2 characters with the same encoded character (in my code). So can someone tell me what's wrong?

Here's a picture of what I mean:


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)
Why don't you just use a easier way to encrypt it?
Code:
public virtual string CryptString(string toEcrypt)
{
    byte[] bt = ASCIIEncoding.ASCII.GetBytes(toEcrypt);
    for(int i = 0; i < bt.Length; i++)
         bt[i] += 0x5;
     return ASCIIEncoding.ASCII.GetString(bt);
}
and now to decrypt it just convert the string to its bytes represent and subtract each byte with 0x5.
Quote Originally Posted by user590177 View Post
Why don't you just use a easier way to encrypt it?
Code:
public virtual string CryptString(string toEcrypt)
{
    byte[] bt = ASCIIEncoding.ASCII.GetBytes(toEcrypt);
    for(int i = 0; i < bt.Length; i++)
         bt[i] += 0x5;
     return ASCIIEncoding.ASCII.GetString(bt);
}
and now to decrypt it just convert the string to its bytes represent and subtract each byte with 0x5.
That's waaay simpler to crack. And as encryption is meant for security, it would be kinda fucked.
But I figured out the problem. I forgot to take into account that that not every number represents a character.

ps: what you posted is known as encoding
Quote Originally Posted by Nathan View Post

ps: what you posted is known as encoding
Yea I learned that about 3 hours ago. And based on that you can create a more advanced function to encrypt the string.
Alternatlty you can use the System.Security.Cryptography namespace in the .Net framework - or if you're working with an unmanged language, MS's Cryptography API library (Which should ship with all versions of VC++ now) at least since 2K10.

For simple encryption like this, it is practical to write your own encryption routine; however when you get to more complex encryption such as those that use asymmetric keys, or are more secure implementations it can become a whole project on its own.

It isn't realistic to give the user an ascii representation of the encrypted string, you should just convert it to an array of bytes and feed it to the user.
Posts 15 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?