Getting Error Message as "the input string is not a complete block" while Decryting using AES

64 Views Asked by At

*The encrypted key is passed from java code while i want to decrypt in c#.

Below is decryption logic in Java

   private static final String AES_ALGO = "AES";
    private static final String KEY_ALGO = "PBKDF2WithHmacSHA256";
    private static final String AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding";
    private static final String secretKey = "vdKnilESoIqCGc";
    private static final String salt= "vdKnilESoIqCGc";
    private static final String strToDecrypt= "bDs_GiEdNL_2U-JVvSFKi0EBN7xdBlpR2V27lFUniHdY_O23gs57169BUbk8rA3vBo7USX1kzAE510ezkHeuTALMDlKXxa_eywYIXg-Ih8Stk4RIVP6HdKgkAXOoRRx3LZXruyBNo0kFPmVNlDREscqvwmjXLRsjLCbUfZepXI9oiREDfRKjZVQ5sACr8SvZuVatdHFBXUd84FCtbxUBNgstAT2yDCw1E_9BdECaTWiww6Fem4ISCEOOzg8h5nvG4GIWG_67ogWIgXiqHroNdV0gTZRppNlEE2M1W15rfWE=";

    public static String decrypt(String strToDecrypt, String salt) {
        byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        try {
            SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGO);
            KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
            SecretKey tmp = factory.generateSecret(spec);
            SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), AES_ALGO);
            Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5);
            cipher.init(DECRYPT_MODE, secretKey, ivspec);
            return new String(cipher.doFinal(getUrlDecoder().decode(strToDecrypt)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

In C#, I am trying to decrypt using below code but it is throwing error message

 byte[] iv1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                int Keysize = 256;
                 string salt = "220120221623321";
                 String passphrase = "vdKnilESoIqCGc";
                     Rfc2898DeriveBytes rfcdb = new System.Security.Cryptography.Rfc2898DeriveBytes(passphrase, Encoding.UTF8.GetBytes(salt), 65536, HashAlgorithmName.SHA256);
             
                    var key = rfcdb.GetBytes(32);

                 byte[] buffer =Encoding.UTF8.GetBytes(ecres);
                using (Aes aes =new AesCryptoServiceProvider())
                {
                    aes.KeySize = 256;
                    aes.BlockSize = 128;
                    aes.Mode = CipherMode.CBC;
                      aes.Padding = PaddingMode.PKCS7;
                    //aes.Padding = PaddingMode.Zeros;
                    aes.IV = iv1;
                    aes.Key = key;
                    //  ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
                    //   ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
                    //  var encryptor = aes.CreateEncryptor(key, aes.IV);
                    using (ICryptoTransform decrypt = aes.CreateDecryptor(aes.Key, aes.IV))
                    {
                        byte[] decryptedText = decrypt.TransformFinalBlock(buffer, 0, buffer.Length);
                        var dec= decryptedText;
                    }
                }

Is there any other way around to solve it? *

1

There are 1 best solutions below

0
Shanky_boi On

Thanks For the suggestions.In Java , base64 string uses '-','_' character which are invalid in c# base64 string,so, I had to replace it.I was able to solve it using below code:

 var data2 = ecres.TrimEnd(padding).Replace('-', '+').Replace('_', '/').PadRight(4 * ((ecres.Length + 3) / 4), '=');
                byte[] plainBytes = Convert.FromBase64String(data2);//Encoding.UTF8.GetBytes(ecres);
                byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                string salt = "220120221623321";
                String passphrase = "vdKnilESoIqCGc";
                var saltBytes = new byte[16];
                var ivBytes = new byte[16];
                Rfc2898DeriveBytes rfcdb = new System.Security.Cryptography.Rfc2898DeriveBytes(passphrase, Encoding.UTF8.GetBytes(salt), 65536, HashAlgorithmName.SHA256);
                saltBytes = rfcdb.GetBytes(32);
                var tempBytes = iv;
                Array.Copy(tempBytes, ivBytes, Math.Min(ivBytes.Length, tempBytes.Length));
                var rij = new RijndaelManaged(); //SymmetricAlgorithm.Create();
                rij.Mode = CipherMode.CBC;
                rij.Padding = PaddingMode.PKCS7;
                // rij.FeedbackSize = 128;
                // rij.KeySize = 256;

                // rij.BlockSize = 256;
                rij.Key = saltBytes;
                rij.IV = iv;
                SymmetricAlgorithm sa = rij;
                var bytesDec = sa.CreateDecryptor().TransformFinalBlock(plainBytes, 0, plainBytes.Length);
                // var decryptedText = Encoding.Unicode.GetString(bytesDec);
                var decryptedText = Encoding.UTF8.GetString(bytesDec);