0

How do I encrypt passwords in such a way that it not only changes the characters of the password but also add extra characters. For example a password as "ABC" would become "12345" instead of "123". Another way is that for each character, the key shift is different. Below are my codes.

class CipherMachine
    {
        static private List<char> Charset =
            new List<char>("PQOWIEURYTLAKSJDHFGMZNXBCV"
                + "olpikmujnyhbtgvrfcedxwszaq"
                + "1597362480"
                + "~!@#$%^&*()_+"
                + "PQOWIEURYTLAKSJDHFGMZNXBCV"
                + "olpikmujnyhbtgvrfcedxwszaq"
                + "1597362480"
                + "~!@#$%^&*()_+");
        static private int Key = 39;
        //static private int Length = 0;
        static public string Encrypt(string plain)
        {
            string cipher = "";
            foreach (char i in plain)
            {
                cipher += Charset.ElementAt(Charset.IndexOf(i) + Key);
                //cipher += Charset.ElementAt(Charset.IndexOf(i) + Length);
            }
            return cipher;
        }
        static public string Decrypt(string cipher)
        {
            string plain = "";
            foreach (char i in cipher)
            {
                plain += Charset.ElementAt(Charset.LastIndexOf(i) - Key);
                //plain += Charset.ElementAt(Charset.LastIndexOf(i) - Length);
            }
            return plain;
        }
    }
}

Lines that are commented out are what I thought I could do but it turned out wrong.

MbrGpt
  • 1
  • 1
  • If this is for anything other than a little programming exercise **don't encode passwords like this**. It's a very weak and easily breakable scheme. If you're storing password, always make sure to salt and hash them using a secure hash algorithm. – Rik Feb 12 '15 at 15:59
  • Yes, this is actually an assignment for school. So it's somewhat an exercise. – MbrGpt Feb 13 '15 at 06:27

1 Answers1

0

You have made the string double length so that the + Key and - Key works, but you ought to have one string of all characters and then WRAP the index (so that if the index goes beyond the length of the string, it wraps back to the beginning). You can achieve this with the % modulus operator:

static private List<char> Charset =
        new List<char>("PQOWIEURYTLAKSJDHFGMZNXBCV"
            + "olpikmujnyhbtgvrfcedxwszaq"
            + "1597362480"
            + "~!@#$%^&*()_+");
int length = Charset.Count();

// to encrypt
int key = 24;
char unencryptedChar = 'P';
int unencryptedIndex = Charset.IndexOf(unencryptedChar);
int encryptedIndex = (unencryptedIndex + key) % length;
char encryptedChar = Charset.ElementAt(encryptedIndex);

// to unencrypt
int encryptedIndex = Charset.IndexOf(encryptedChar);
int unencryptedIndex = (encryptedIndex - key + length) % length;
char unencryptedChar = Charset.ElementAt(unencryptedIndex);

When you subtract the key in the second part, the index goes negative, and modulus won't work properly on a negative, so we add the length (though this only works if the key is smaller than the length).

CapIsland
  • 129
  • 1
  • 9
  • You are using very primitive symmetric-key [cryptography](http://en.wikipedia.org/wiki/Cryptography). It's weak. If e.g you change 1 char in input stream you get 1 char changed in output stream which makes obtaining key a very easy job. Alphabet-based encryption can be easily predicted by using statistic (hacker can see word "delimiters" and can find out other characters by guessing and checking). Consider to use more advanced [techniques](http://stackoverflow.com/q/202011/1997232). – Sinatr Feb 12 '15 at 13:23
  • @Sinatr, who is "You"? Is this meant to be a comment for his question or my answer? – CapIsland Feb 12 '15 at 13:40
  • Yes, it suppose to be a comment to question, sorry =D – Sinatr Feb 12 '15 at 13:46
  • int length = Charset.Length(); gives an error which is it does not contain a definition for 'Length' and no extension method. – MbrGpt Feb 12 '15 at 15:39
  • It should be `Charset.Count()`. I have changed it. – CapIsland Feb 12 '15 at 15:52
  • int key = 24; char unencryptedChar = 'P'; int unencryptedIndex = Charset.IndexOf(unencryptedChar); int encryptedIndex = (unencryptedIndex + key) % length; char encryptedChar = Charset.ElementAt(encryptedIndex); Are these codes supposed to be added in into my current codes or ? – MbrGpt Feb 13 '15 at 06:27
  • @MbrGpt No, they are examples only. – CapIsland Feb 13 '15 at 08:40