You need to sign in to do that
Don't have an account?
System.SecurityException: last block incomplete in decryption AES 128 Decryption Error
Hi Everyone,
I have a scenario where i am integrating with another Salesforce Org using Apex Callout and the response sent by wenbservice need to be Decrypted Using AES 128. I have taken care of using the same Key and Initialization vector which was used for Encryption but I am getting error
System.SecurityException: last block incomplete in decryption
when i try to decrypt the response body. Below is my code
public class easyCarCallout {
private final String clientId = customsetting.getorgdefaults.clientid;
private final String clientSecret = customsetting.getorgdefaults.clientSecret ;
private final String username = customsetting.getorgdefaults.username ;
private final String password = customsetting.getorgdefaults.password ;
public class deserializeResponse
{
public String id;
public String access_token;
}
public class prInfoJSONValues {
public String prnum;
public String Name;
public datetime dtstart;
public datetime dtend;
public String city;
public String organizer;
public String externalid;
public String status;
public String error;
}
public static prInfoJSONValues parse(String json) {
return (prInfoJSONValues) System.JSON.deserialize(json, prInfoJSONValues.class);
}
public static String getDecryptedData(String inputData){
Blob key = EncodingUtil.base64Decode(Customsetting.getorgdefaults.KEY);
Blob IV = EncodingUtil.base64Decode(Customsetting.getorgdefaults.IV );
Blob decryptedData = Crypto.decrypt('AES128', key, IV, Blob.valueOf(inputData));
String decryptedClearText = decryptedData.toString();
return decryptedClearText;
}
public String ReturnAccessToken ()
{
//--- Client Id = Consumer Key, Client Secret = Consumer Secret, Username/Password = User's Username and Password, Password should be concatenated with the Security token
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://test.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void getPR ()
{
easyCarCallout test= new easyCarCallout ();
String accessToken;
accessToken = test.ReturnAccessToken ();
system.debug('accessToken'+accessToken);
if(accessToken != null){
//--- Complete URL including the REST Resource of the Destination
String endPoint = 'https://cs72.salesforce.com/services/apexrest/pr_c/4172991';
//--- Create the JSON String containing the fields to be sent to the destination for update
Http h1 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
//req1.setHeader('Content-Type','application/json');
//req1.setHeader('accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse res1 = h1.send(req1);
system.debug('res1'+res1.getBody());
String response = res1.getBody();
String dec = getDecryptedData(response);
system.debug('dec'+dec);
prInfoJSONValues pf = new prInfoJSONValues();
pf = parse (dec);
system.debug('pf'+pf);
}
}
}
Can Someone please point why i am getting this error. Thanks in advance
I have a scenario where i am integrating with another Salesforce Org using Apex Callout and the response sent by wenbservice need to be Decrypted Using AES 128. I have taken care of using the same Key and Initialization vector which was used for Encryption but I am getting error
System.SecurityException: last block incomplete in decryption
when i try to decrypt the response body. Below is my code
public class easyCarCallout {
private final String clientId = customsetting.getorgdefaults.clientid;
private final String clientSecret = customsetting.getorgdefaults.clientSecret ;
private final String username = customsetting.getorgdefaults.username ;
private final String password = customsetting.getorgdefaults.password ;
public class deserializeResponse
{
public String id;
public String access_token;
}
public class prInfoJSONValues {
public String prnum;
public String Name;
public datetime dtstart;
public datetime dtend;
public String city;
public String organizer;
public String externalid;
public String status;
public String error;
}
public static prInfoJSONValues parse(String json) {
return (prInfoJSONValues) System.JSON.deserialize(json, prInfoJSONValues.class);
}
public static String getDecryptedData(String inputData){
Blob key = EncodingUtil.base64Decode(Customsetting.getorgdefaults.KEY);
Blob IV = EncodingUtil.base64Decode(Customsetting.getorgdefaults.IV );
Blob decryptedData = Crypto.decrypt('AES128', key, IV, Blob.valueOf(inputData));
String decryptedClearText = decryptedData.toString();
return decryptedClearText;
}
public String ReturnAccessToken ()
{
//--- Client Id = Consumer Key, Client Secret = Consumer Secret, Username/Password = User's Username and Password, Password should be concatenated with the Security token
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://test.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void getPR ()
{
easyCarCallout test= new easyCarCallout ();
String accessToken;
accessToken = test.ReturnAccessToken ();
system.debug('accessToken'+accessToken);
if(accessToken != null){
//--- Complete URL including the REST Resource of the Destination
String endPoint = 'https://cs72.salesforce.com/services/apexrest/pr_c/4172991';
//--- Create the JSON String containing the fields to be sent to the destination for update
Http h1 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
//req1.setHeader('Content-Type','application/json');
//req1.setHeader('accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse res1 = h1.send(req1);
system.debug('res1'+res1.getBody());
String response = res1.getBody();
String dec = getDecryptedData(response);
system.debug('dec'+dec);
prInfoJSONValues pf = new prInfoJSONValues();
pf = parse (dec);
system.debug('pf'+pf);
}
}
}
Can Someone please point why i am getting this error. Thanks in advance