DebateFort - Where Warriors Come To Debate
RAGECRY - Funny, Amusing, Interesting, Trending & Viral Videos and Images
GameOrc - Free Flash Games Online
Results 1 to 5 of 5
  1. #1
    Still a Cookie at heart
    Donator
    Nathan's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    In a magical place
    Posts
    6,113
    Reputation
    394
    Thanks
    353

    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)
    Last edited by Nathan; 11-10-2011 at 06:37 AM.

  2. #2
    Gimme Dem Boobies...
    MPGH Member
    ლ(ಠ_ಠლ)'s Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    trolololololo
    Posts
    6,853
    Reputation
    188
    Thanks
    1,348
    My Mood
    Angry
    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.
    EasyAdmin v1.0
    • Manage your server directly from ACP
    • See statistics such as CPU Usage, Players Info, Etc
    • Fully automated Login/Register System
    • RCON Tool Functionality
    • In-Game admin shortcuts
    • Database Storage


    Coming Soon!



  3. #3
    Threadstarter
    Still a Cookie at heart
    Donator
    Nathan's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    In a magical place
    Posts
    6,113
    Reputation
    394
    Thanks
    353
    Quote Originally Posted by Elio 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

  4. #4
    Gimme Dem Boobies...
    MPGH Member
    ლ(ಠ_ಠლ)'s Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    trolololololo
    Posts
    6,853
    Reputation
    188
    Thanks
    1,348
    My Mood
    Angry
    Quote Originally Posted by Cookie. 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.
    EasyAdmin v1.0
    • Manage your server directly from ACP
    • See statistics such as CPU Usage, Players Info, Etc
    • Fully automated Login/Register System
    • RCON Tool Functionality
    • In-Game admin shortcuts
    • Database Storage


    Coming Soon!



  5. #5
    JetaKing
    Super User
    Adolfmay's Avatar
    Join Date
    Jan 2007
    Gender
    male
    Location
    J:\E\T\A\M\A\Y.exe
    Posts
    8,601
    Reputation
    302
    Thanks
    1,667
    My Mood
    Sad
    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.
    Last edited by Adolfmay; 11-10-2011 at 11:46 PM.
    Remote\Local PE Loader

    There are two types of tragedies in life. One is not getting what you want, the other is getting it.

    If you wake up at a different time in a different place, could you wake up as a different person?

    But soon we shall die and all memory of those five will have left earth, and we ourselves shall be loved for a while and forgotten. But the love will have been enough; all those impulses of love return to the love that made them. Even memory is not necessary for love. There is a land of the living and a land of the dead and the bridge is love, the only survival, the only meaning."

  6. The Following User Says Thank You to Adolfmay For This Useful Post:

    PokemonCraft (11-22-2011)

Similar Threads

  1. [Tutorial] Encrypting/decrypting strings
    By sythe179 in forum Visual Basic Programming
    Replies: 5
    Last Post: 08-08-2011, 03:08 PM
  2. Caesar/ROT String Encryption
    By Departure in forum Combat Arms Coding Help & Discussion
    Replies: 1
    Last Post: 02-28-2011, 12:41 PM
  3. Basic Encryption
    By Calard in forum General Game Hacking
    Replies: 0
    Last Post: 02-23-2007, 06:32 AM
  4. String Theory
    By arunforce in forum General
    Replies: 8
    Last Post: 01-14-2007, 09:49 AM
  5. Encryption on Files
    By HolyFate in forum Gunz General
    Replies: 15
    Last Post: 02-20-2006, 01:50 PM