Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's the encryption method I create,
C#
public string Encrypt(string Text, string Key)
        {
            string newText = Text;
            string newKey = Key;
            string newResult = string.Empty;

            //EQUATE TEXT AND KEY LENGTH
            if (Text.Length > Key.Length)
            {
                int left = 0;

                for (int i = 0; i < Math.Abs(Text.Length - Key.Length); i++)
                {
                    newKey += Key.Substring(left, 1);
                    left += 1;

                    if (left == Key.Length) { left = 0; }
                }
            }
            else if (Text.Length < Key.Length)
            {
                newKey = Key.Substring(0, Text.Length);
            }
            
            //GET ASCII CODE > CONVERT TO BINARY > MERGE BINARY
            for (int i = 0; i < Text.Length; i++)
            {
                byte txtASC = Encoding.Default.GetBytes(newText)[i]; 
                byte keyASC = Encoding.Default.GetBytes(newKey)[i];

                string txtBIN = Convert.ToString(Convert.ToInt32(txtASC), 2);
                string keyBIN = Convert.ToString(Convert.ToInt32(keyASC), 2);
                string newBIN = string.Empty;

                newBIN = Convert.ToString(Convert.ToInt32(txtBIN, 2) + Convert.ToInt32(keyBIN, 2), 2); //CHANGE + TO - IN DECRYPT METHOD

                newResult += ((char)Convert.ToInt32(newBIN, 2)).ToString();

            }

            return newResult;
        }


When I use a simple 'a' key, and a single 'apple' word, both encrypt and decrypt works.
But when I put sentences and a longer key, I can't read the result of decryption anymore.
Can someone explain what's wrong in my code?
Posted

1 solution

Would you like a list?
It's hard to spot the problem you are complaining of - your code doesn't work with a string "Apple" and a key of "1234" so your testing is a little suspect...For a Vernam Cipher, I would expect to get an encrypted string the same length as the input string, and your code doesn't do that. It also doesn't implement a Vernam Cipher: the lack of an XOR in there is a bit of a clue.
http://www.cryptomuseum.com/crypto/vernam.htm[^]

I think you need to sit down, read the link, and start all over again.
BTW: You don't need to create a new key that is long enough to cover the whole message:
Just create an index starting at zero, and check it against the length of the key:
C#
int keyIndex = 0;
foreach (char c in Text)
    {
    if (keyIndex >= key.Length) keyIndex = 0;
    char keyValue = key[keyIndex];
    ...
    keyIndex++;
    }
 
Share this answer
 
v2
Comments
Midnight Ahri 1-Dec-13 22:02pm    
Thank you for answering, my problem is when I combine the character binary and key binary, convert it to ascii code, and convert back to char, it return \0.
Thats why the length of encryption result differ from the text.
OriginalGriff 2-Dec-13 5:29am    
That's because the way you are doing it is rubbish! :laugh:
Look at the XOR Operator '^' - it is exactly what you need for Vernam Cipher, which is XOR based. Adding bits does not do the same thing, not at all - and there are better ways to do the addition, as well...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900