You need to sign in to do that
Don't have an account?
Gowtham-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.
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.
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 :)