function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gowtham-Reddy Tirumala-ReddyGowtham-Reddy Tirumala-Reddy 

How to encrypt in C# and decrypt in Apex class controller

Hi All,

I am trying to encrypt an object and pass it as query string from an external site(in C#) to a salesforce Apex controller and decrypt it in constructor.
This is the code I am using.

C#:
private string Encrypt256<T>(T dataObject)
        {            
            // AesCryptoServiceProvider
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider
            {
                KeySize = 256,
                Key = Convert.FromBase64String("****"),
                Mode = CipherMode.CBC,
                Padding = PaddingMode.PKCS7,
                IV = IV
            };

            // Convert string to byte array
            byte[] src = Encoding.Unicode.GetBytes(SerializeToString(dataObject));

            // encryption
            using (ICryptoTransform encrypt = aes.CreateEncryptor(aes.Key,IV))
            {
                byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);

                // Convert byte array to Base64 strings
                return Convert.ToBase64String(dest);
            }
        }
        private static string SerializeToString(object obj)
        {
            string output;
            XmlSerializer serializer = new XmlSerializer(obj.GetType());

            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, obj);

                output = writer.ToString();
            }
            return output;
        }

APEX Controller:
String queryString = ApexPages.currentPage().getParameters().get('token');
Blob key = Blob.valueOf('***');
String encodedString = EncodingUtil.urlEncode(queryString ,'UTF-8');
 Blob source = Blob.valueOf(encodedString);
Blob decrypted = Crypto.decryptWithManagedIv('AES256', key, source );
jsonString = decrypted.toString();

I keep getting the error 
"|DEBUG|ERROR:System.SecurityException: Input length must be multiple of 16 when decrypting with padded cipher"

I am new to APEX. What am I missing here. 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I notice that in C#, the key has four asterisks, while in Apex, it has three.  Is that the problem?
Gowtham-Reddy Tirumala-ReddyGowtham-Reddy Tirumala-Reddy
I am using the same key in both C# and APEX. I just masked it like that :)
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
The Apex error suggests that it expects the encrypted string to be a multiple of 16 characters.  Is it possible to pad the encrypted string on the C# side so that it is a multiple of 16 chars?  [Aside: don't you want to urlDEcode the queryString into the encodedString?]
Gowtham-Reddy Tirumala-ReddyGowtham-Reddy Tirumala-Reddy
I did urlDEcode . The strange thing is excrpt/decrypt works fine for with
 object x = new {foo = "bar"};
 var jsonString = new JavaScriptSerializer().Serialize(x);

but not with 
 object x = new {foo = "avi"};

Any other ideas? Thanks for your reply :)