• RahulSFDCTSH
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Senior Developer
  • Cognizant


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 3
    Replies
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
We have a Callout method where we are sending JSON data after encrypting data with AES 128 to an external system but external system Cannot decode this as they are getting error like "Unrecognized Base 64 string , String Contains a Non-Base 64 Character". However we have a webservice which is getting called by external system and we are are serializing the response and sending AES 128 encrypted response and they are able to decrypt it correctly. The can see the differnce between these 2 from my Logs that one of the response contains "/" which might be the problem here. What i don't understand is i am using 1 single method to decrypt every callout or webservice request Body or response data respectively but one is giving error and other is not

Web service respose data we are sending:
"TGFu8To5cAnIplvlTMzbTZsBdBhyqDJDb/TBUt0Erfu963QwJwh05abEUN4b/Jg9F3G4z7bDn01W4uK/dEeVZOYgxRMmZ1OrzE7ZEwuX5yrPnSQuQK0jQ1rY1r5GxZkJiyXTe+cf1WMW+FUrZLsyqP0NTjEvmHcSoGQvDoTcbxAWTXkdUCVKPUwr/1t/29Q8Sl0BnRbxS6PMoupRtNYGYkYS9zugzZkRFa3MsV8uuOwp1q4EFzwjJgITvH09X0+td5odxprmpZOygZYFkKSGEvQbQ1c2E6l/XAvnKHxPsre16P+YUlNARLIOuS6gSCrp3yYK9UDQmYspzNiLOyDvpzkVrK+9QlM7VliiNgKNC2tNuDYCWYo3sCidez6gi8NcCs+JTnBPJSOli0VySsFUSw=="

Call Out Request Body we are Posting

VfxLXP6/zgyu34KFk9gmjR3suJu3nkWNrRDi+KiOFYXAe8LVcBSaS7wje0nV0nSqe/zRqfZ3oxc8TICrimmqjoZU8jdSVF+bssWgLlRV+ljMlZO1vFY2aQn178C6R1hAiarGxkZ3adD5fF8NFYGk3y+A3JhW2s5CumcYO/sLTRoc2aOsICQQfwEHbLfT5hl7otaMLdPPRhXxKvwp1Hb6wjaYjMaoy7mygPEH/ITQOuTeNTFY6XhD176YGaq6+vVbAUaz3U+uT8kOzfBjtyY3/9mzrpvkCkxNG4YcAwm4QvgT3fz09mknbHZ8rJryeCYT0+NUtpvYw6Szl0YKDxhA7ww4j4S0E9GC1YCd81Vkc545G5f9VRVAbd5usrgnYhRlmRYe+nCNs+mq6HfPzSnlI32NoreWz1LKmKVV3jLzbFL1FRibipSS81LV9H8q53XvxAE1MDR1MhQBvGwmIwc8Bp+YrUzqYiv

Method Used to create AES encryted data

public static String getEncryptedData(String inputData){
        String b64Data = EncodingUtil.base64Encode(Blob.valueOf(inputData));
        Blob key = EncodingUtil.base64Decode(KEY);
        Blob IV =  EncodingUtil.base64Decode(IV);
        Blob encryptedData = Crypto.encrypt('AES128', key, IV, Blob.valueOf(b64Data));
        //encrypted string
        String encodedprdata = EncodingUtil.base64Encode(encryptedData);
        return encodedprdata;
    }

Also, My Json String which i will encrypyt for Callout request body contain an attribute which is MD5 encrypted 

public static String getMD5EncryptedData(String inputData){
         Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(inputData));
        String sign = EncodingUtil.convertToHex(hash);
        return sign;
    }

Have anyone ever faced this kind of issue and this is really irritating me and going clueless over this. Can someone please help on this? Thanks in advance
We have a Callout method where we are sending JSON data after encrypting data with AES 128 to an external system but external system Cannot decode this as they are getting error like "Unrecognized Base 64 string , String Contains a Non-Base 64 Character". However we have a webservice which is getting called by external system and we are are serializing the response and sending AES 128 encrypted response and they are able to decrypt it correctly. The can see the differnce between these 2 from my Logs that one of the response contains "/" which might be the problem here. What i don't understand is i am using 1 single method to decrypt every callout or webservice request Body or response data respectively but one is giving error and other is not

Web service respose data we are sending:
"TGFu8To5cAnIplvlTMzbTZsBdBhyqDJDb/TBUt0Erfu963QwJwh05abEUN4b/Jg9F3G4z7bDn01W4uK/dEeVZOYgxRMmZ1OrzE7ZEwuX5yrPnSQuQK0jQ1rY1r5GxZkJiyXTe+cf1WMW+FUrZLsyqP0NTjEvmHcSoGQvDoTcbxAWTXkdUCVKPUwr/1t/29Q8Sl0BnRbxS6PMoupRtNYGYkYS9zugzZkRFa3MsV8uuOwp1q4EFzwjJgITvH09X0+td5odxprmpZOygZYFkKSGEvQbQ1c2E6l/XAvnKHxPsre16P+YUlNARLIOuS6gSCrp3yYK9UDQmYspzNiLOyDvpzkVrK+9QlM7VliiNgKNC2tNuDYCWYo3sCidez6gi8NcCs+JTnBPJSOli0VySsFUSw=="

Call Out Request Body we are Posting

VfxLXP6/zgyu34KFk9gmjR3suJu3nkWNrRDi+KiOFYXAe8LVcBSaS7wje0nV0nSqe/zRqfZ3oxc8TICrimmqjoZU8jdSVF+bssWgLlRV+ljMlZO1vFY2aQn178C6R1hAiarGxkZ3adD5fF8NFYGk3y+A3JhW2s5CumcYO/sLTRoc2aOsICQQfwEHbLfT5hl7otaMLdPPRhXxKvwp1Hb6wjaYjMaoy7mygPEH/ITQOuTeNTFY6XhD176YGaq6+vVbAUaz3U+uT8kOzfBjtyY3/9mzrpvkCkxNG4YcAwm4QvgT3fz09mknbHZ8rJryeCYT0+NUtpvYw6Szl0YKDxhA7ww4j4S0E9GC1YCd81Vkc545G5f9VRVAbd5usrgnYhRlmRYe+nCNs+mq6HfPzSnlI32NoreWz1LKmKVV3jLzbFL1FRibipSS81LV9H8q53XvxAE1MDR1MhQBvGwmIwc8Bp+YrUzqYiv

Method Used to create AES encryted data

public static String getEncryptedData(String inputData){
        String b64Data = EncodingUtil.base64Encode(Blob.valueOf(inputData));
        Blob key = EncodingUtil.base64Decode(KEY);
        Blob IV =  EncodingUtil.base64Decode(IV);
        Blob encryptedData = Crypto.encrypt('AES128', key, IV, Blob.valueOf(b64Data));
        //encrypted string
        String encodedprdata = EncodingUtil.base64Encode(encryptedData);
        return encodedprdata;
    }

Also, My Json String which i will encrypyt for Callout request body contain an attribute which is MD5 encrypted 

public static String getMD5EncryptedData(String inputData){
         Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(inputData));
        String sign = EncodingUtil.convertToHex(hash);
        return sign;
    }

Have anyone ever faced this kind of issue and this is really irritating me and going clueless over this. Can someone please help on this? Thanks in advance
Hi Everyone,

I am trying to test out web server OAuth flow between 2 different salesforce org but i am unable to get authorization code. I already have connected app set up done in my Target org and i am using Callback URL as https://[MY Domain Name].salesforce.com/services/oauth2/token and i am making a post request first to Authorization end point but when i try to check the response body in debug i get unsupported_Respose_type error. Below is code for gettting Auth code

public class getCaseFromTarget {

   String clientid = My Client Id;
   String csecret = My Client Secret;
   String auth_url = 'https://[MY Domain Name]..salesforce.com/services/oauth2/authorize';
   String response_type = 'code';
   String callback ='https://[MY Domain  Name].salesforce.com/services/oauth2/token';
   String callback_URL = EncodingUtil.urlEncode(callback, 'UTF-8');

   public class deserializeResponse {
        public String access_token;
   }
   public String ReturnAccessToken() {
       String reqbody = '?response_type=code&client_id='+cid+'&redirect_uri='+callback_URL; 
       Http h = new Http();
       HttpRequest req1 = new HttpRequest();
       req1.setBody(reqbody);
       req1.setMethod('POST');    
       req1.setEndpoint(auth_url);
       req1.setHeader('Content-Type', 'application/x-www-form-urlencoded');
       HttpResponse res = h.send(req1);
       System.debug( '**********'+res.getBody() );
       return null;/// please ignore this part. it's just temporary
   }
}

When i form the URL Manually and paste it in browser i can see the Authorization screen but getting invalid grant type error after that. I am not sure what i am doing wrong here.

I want to know if it possible to get authorization and token in same apex call implicitly. Also it would be helpful if someone can provode some sample code for this as most of the blogs or post are talking about Username-Password Flow which i don't want to use for this purpose. Thanks in Advance
I currently have a fomula field (Returns remaining days as Text ) that displays Remnaing days (Starts from 5  and keep counting down each passing day when a new Case is created). Here is the formula I used: 

IF( 
TODAY() > ( DATEVALUE(CreatedDate)  + 5), 
TEXT(-1*(TODAY()- ( DATEVALUE(CreatedDate) + 5 ))) , 
TEXT(( DATEVALUE(CreatedDate)+ 5 ) - TODAY() )
)
Currently this formula considers weekends days as well. I would like to modify the formula to exclude weekend days from TODAY and CreatedDate both.
For example: If a Case is created on Oct 11, 2017 (Wed) ,Remaining Days value on Oct 23, 2017 (Mon) should be -3 not -7.
Can someone please help?
Thanks in advance.
Hello Team,

I have followed all of the processes for pushing Change Sets and Profile accessibility but all of my fields are still showing as hidden when I push to production. I have:

1. Checked the field accessibility on the field in my sandbox and it is editable for all profiles.

User-added image

2. I created my change set with the fields in question and ALL profiles.

User-added imageUser-added image
3. I have uploaded and deployed the change set.
4. At this point, the field level security for all profiles has been switched to hidden.
User-added image

After reviewing all documentation and previously written articles this should be everything that needs to be done. We have an exorbitant amount of profiles, which is another problem in and of itself, and I don't have time to go profile by profile amending this every time I push changes up to production.

Any ideas?
Hi,

I have a requirement that if lead status is not updated for 90 days, status should be automatically updated as  Inactive and notification should be sent. . How can we achieve this
I currently have a fomula field (Returns remaining days as Text ) that displays Remnaing days (Starts from 5  and keep counting down each passing day when a new Case is created). Here is the formula I used: 

IF( 
TODAY() > ( DATEVALUE(CreatedDate)  + 5), 
TEXT(-1*(TODAY()- ( DATEVALUE(CreatedDate) + 5 ))) , 
TEXT(( DATEVALUE(CreatedDate)+ 5 ) - TODAY() )
)
Currently this formula considers weekends days as well. I would like to modify the formula to exclude weekend days from TODAY and CreatedDate both.
For example: If a Case is created on Oct 11, 2017 (Wed) ,Remaining Days value on Oct 23, 2017 (Mon) should be -3 not -7.
Can someone please help?
Thanks in advance.