SantyEssac
SantyEssac

Reputation: 809

Error: "Padding is invalid and cannot be removed" using asymmetric algorithm

I want to encrypt and decrypt the string using asymmetric cryptographic algorithm that is I want to pass different key in encrypt and decrypt function.

My code is as follows:

public ActionResult Encrypt(string clearText)
{
    string EncryptionKey = "ABKV2SPBNI99212";
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
    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.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            clearText = Convert.ToBase64String(ms.ToArray());
        }
    }

    Decrypt(clearText);

    return View(clearText); 
}

public string Decrypt(string cipherText)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    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 ;
}

Here is the encrypt function I am sending the value using link as follows

 <a href="@Url.Action("Encrypt", "Home", new {@clearText="5"})">Bid Buddy</a>

Here I want to send different key values as shown.

Upvotes: 0

Views: 178

Answers (2)

Artjom B.
Artjom B.

Reputation: 61942

AES is a symmetric block cipher. Decryption only works if the same key is presented as during encryption. There is no way around that.

What you additionally have is a cryptographic hash function. All hash functions have collisions, but the exploitation of those is negligible for a cryptographic hash function such as yours. So, it would be too costly to find two passprases that map to the same key (which would make it technically asymmetric).

You need to generate an public-private key pair. An option to do this is for example RSA. You would then encrypt the data with a random AES key and encrypt this AES key with the RSA public key. This is called hybrid encryption.

Upvotes: 2

Earth
Earth

Reputation: 3571

Your encrpt / decrpyt keys are different and that cause you getting this error.

Padding is invalid and cannot be removed

Make sure to keep the encrpt / decrpt keys are same.

Upvotes: 2

Related Questions