-1

Attempts to decrypt an encrypted identifier gives an error.

Please help me figure out how to solve it.

My decryption code is as follows:

public static string Decryptor(string cipherText)
{
    string EncryptionKey = "MAKV2SPBNI99212";

    cipherText = cipherText.Replace(" ", "+");

    byte[] cipherBytes = Convert.FromBase64String(cipherText);

    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });

        encryptor.Key = pdb.GetBytes(32);

        encryptor.IV = pdb.GetBytes(16);

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(cipherBytes, 0, cipherBytes.Length);

                cs.Close();

            }

            cipherText = Encoding.Unicode.GetString(ms.ToArray());

        }

    }

    return cipherText;


}

Is my issue in my code?

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
Sandip Gend
  • 45
  • 1
  • 8
  • possible duplicate: http://stackoverflow.com/questions/8061581/padding-is-invalid-and-cannot-be-removed?rq=1 – swe Dec 05 '16 at 10:06
  • What sort of `cipherText` do you pass in this method? Is it any kind of text, or is it Base-64 encoded binary data? – Codo Dec 05 '16 at 10:07
  • 1
    There are about 100,000 other similar questions on SO because people do not understand encryption basics (input and output of encryption algorithms is binary data, not text; key is binary data; encryption works on blocks of data and needs padding; using ECB is insecure in most contexts etc.). How can we better handle this than answer all those individual questions? Is there a good FAQ somewhere? – Codo Dec 05 '16 at 10:12
  • i pass some numeric value like 1006,1063 – Sandip Gend Dec 05 '16 at 10:45
  • dear user, please have a look at all these other questions concerning the same topic. at the right side of this page you see linked and related questions. Open the linked question and then you will see hundrets of related questions to yours. if you do not find an answer there come back here again. Is that ok for you? – swe Dec 05 '16 at 11:16

1 Answers1

0

The basic problem is how you convert text to binary data (required for encyrption) and how you convert it back into text. Please understand that AES requires binary data as input and produces binary data as output.

So you need to main changes. First, you encode the cipher text in UTF-8 to get binary data:

byte[] cipherBytes = Encoding.UTF8.GetBytes(cipherText);

After the encryption, you have binary data again (ms.ToArray()). The best thing would be to transmit this data in binary form. Then you don't need to go through more conversion and encoding.

If you need a string instead, encode it in Base-64:

cipherText = Convert.ToBase64String(ms.ToArray());

In your current code, you try to decode the binary data as Unicode. This doesn't work. Random binary data hardly ever is valid Unicode encoded data. So it can't be decoded into a String using the Unicode encoding.

For the decryption, you need to perform the reverse operations in the reverse order.

These are just the main problems. There are additional ones:

  • The salt is fixed and the initialization vector is derived from the key; one of them should be random and transmitted with the encrypted data.
  • You use the default cipher mode, default key size and padding. You better understand what they are and if they are appropriate. You'll need to use exactly the same for decryption.
  • I assume it's just for the sake of this question that the password is fixed and part of the source code. In the final application, this is of course a no go.
Codo
  • 75,595
  • 17
  • 168
  • 206