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

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();
        HttpResponse res = h.send(req);
        deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);        
        return resp1.access_token;
    public static void getPR ()
        easyCarCallout test= new easyCarCallout ();
        String accessToken;
        accessToken = test.ReturnAccessToken ();
        if(accessToken != null){
             //--- Complete URL including the REST Resource of the Destination
             String endPoint = ''; 
            //--- 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);
            HttpResponse res1 = h1.send(req1);      
            String response = res1.getBody();
            String dec = getDecryptedData(response);
            prInfoJSONValues pf = new prInfoJSONValues();
            pf = parse (dec);

Can Someone please point why i am getting this error. Thanks in advance