• mnapoli
  • NEWBIE
  • 25 Points
  • Member since 2011

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies

Ok, so I have a webservice hosted outside of Salesforce that is called from one of our pages within Salesforce. Within this webservice is a field that we need to encrypt before sending it over the wire. I am able to encrypt it but I just keep getting the "Given final block not properly padded" error message when I try to decrypt it in Salesforce. If someone can please take a look at my code and let me know whether the issue lies in my .NET code where I am encrypting it or in my Apex code where I am decrypting it, I would be eternally grateful.

 

My .NET code:

 

public static string Encrypt_AES(string text)
{
    if (text == null || text.Length <= 0)
    {
        throw new ArgumentNullException("plainText");
    }

    byte[] encrypted;

    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        aesAlg.Key = System.Text.Encoding.UTF8.GetBytes("ABC123DEF456HIJ789KLM012NOP345QR");
        aesAlg.IV = System.Text.Encoding.UTF8.GetBytes("ABC123DEF456HIJ7");
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.Padding = PaddingMode.PKCS7;

        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    sw.Write(text);
                }

                encrypted = ms.ToArray();
            }
        }
    }

    return Convert.ToBase64String(encrypted);
}

 

My Apex code:

 

blob key = blob.valueOf('ABC123DEF456HIJ789KLM012NOP345QR');
blob iv = blob.valueOf('ABC123DEF456HIJ7');
blob encryptedData = blob.valueOf(response.Text);
blob decryptedData = Crypto.decrypt('AES256', key, iv, encryptedData);				
system.debug(decryptedData.toString());

 

Ok, so I have a webservice hosted outside of Salesforce that is called from one of our pages within Salesforce. Within this webservice is a field that we need to encrypt before sending it over the wire. I am able to encrypt it but I just keep getting the "Given final block not properly padded" error message when I try to decrypt it in Salesforce. If someone can please take a look at my code and let me know whether the issue lies in my .NET code where I am encrypting it or in my Apex code where I am decrypting it, I would be eternally grateful.

 

My .NET code:

public static string Encrypt_AES(string text)
{
    if (text == null || text.Length <= 0)
    {
        throw new ArgumentNullException("plainText");
    }

    byte[] encrypted;

    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        aesAlg.Key = System.Text.Encoding.UTF8.GetBytes("ABC123DEF456HIJ789KLM012NOP345QR");
        aesAlg.IV = System.Text.Encoding.UTF8.GetBytes("ABC123DEF456HIJ7");
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.Padding = PaddingMode.PKCS7;

        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    sw.Write(text);
                }

                encrypted = ms.ToArray();
            }
        }
    }

    return Convert.ToBase64String(encrypted);
}

 My Apex code:

blob key = blob.valueOf('ABC123DEF456HIJ789KLM012NOP345QR');
blob iv = blob.valueOf('ABC123DEF456HIJ7');
blob encryptedData = blob.valueOf(response.Text);
blob decryptedData = Crypto.decrypt('AES256', key, iv, encryptedData);				
system.debug(decryptedData.toString());

 

So I have a VisualForce page that utilizes a jQuery modal to display questions to the user. The questions are populated using a repeater that references a list of an custom class that I created in my controller. The answers that they enter are tied to properties on the custom class that the repeater is referencing. Now if I take code out of the modal and just make it show all the time everything works like a charm. However as soon as I place the code back within the jQuery controlled modal div I lose the ability to see values they entered in the form in the controller. In other words, when they submit the form within the popup and it fires off the Ajax call to the controller to process the input all the values are null. If I remove the form from the popup and they submit all the values are there. The only other info that may be pertinent to my issue is that I am using multiple forms on the page. However the issue seems to occur whether it is in its own form or in the main page form.

I have a simple outputfield that is pointing to the CreatedById field on the Task object. Everything works fine as it renders the name of the user that the CreatedById belongs to and displays it as a link to that user's page. However, that link navigates the main window. I want my users to be able to click on that link but have it open in a new window. Do we have any way to control the target window of that link that is generated by the outputfield? Any help is greatly appreciated.

Ok, so I have a webservice hosted outside of Salesforce that is called from one of our pages within Salesforce. Within this webservice is a field that we need to encrypt before sending it over the wire. I am able to encrypt it but I just keep getting the "Given final block not properly padded" error message when I try to decrypt it in Salesforce. If someone can please take a look at my code and let me know whether the issue lies in my .NET code where I am encrypting it or in my Apex code where I am decrypting it, I would be eternally grateful.

 

My .NET code:

 

public static string Encrypt_AES(string text)
{
    if (text == null || text.Length <= 0)
    {
        throw new ArgumentNullException("plainText");
    }

    byte[] encrypted;

    using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
    {
        aesAlg.Key = System.Text.Encoding.UTF8.GetBytes("ABC123DEF456HIJ789KLM012NOP345QR");
        aesAlg.IV = System.Text.Encoding.UTF8.GetBytes("ABC123DEF456HIJ7");
        aesAlg.Mode = CipherMode.CBC;
        aesAlg.Padding = PaddingMode.PKCS7;

        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    sw.Write(text);
                }

                encrypted = ms.ToArray();
            }
        }
    }

    return Convert.ToBase64String(encrypted);
}

 

My Apex code:

 

blob key = blob.valueOf('ABC123DEF456HIJ789KLM012NOP345QR');
blob iv = blob.valueOf('ABC123DEF456HIJ7');
blob encryptedData = blob.valueOf(response.Text);
blob decryptedData = Crypto.decrypt('AES256', key, iv, encryptedData);				
system.debug(decryptedData.toString());

 

Is it possible to share an AES key between Salesforce and another application? I know there is the Crypto.generateAesKey() method, but I can't use that key in a PHP application to decrypt the data later. Is it possible to generate my own AES key from a string, then use that string as the AES key in both applications?