You need to sign in to do that
Don't have an account?

How to convert the AES-256 key and Initial vector with the public RSA Key(.PEM file) shared by third party system
Problem:- How to convert the AES-256 key and Initial vector with the public RSA Key (Shared by third party system – Agreement Express)
Requirement:-
1. Randomly generate a AES-256 key.
2. Randomly generate an IV.
3. Encrypt the json with the AES key and IV, base64-encode it, and set it as the request
body.
4. Encrypt the AES key with the public RSA key and base64-encode it
5. Encrypt the IV with the public RSA key and base64-encode it
Implementation:
From the above requirement, I am able to implement the point 1, 2, and 3. Now the technical challenge which we are facing here is, how to convert the AES-256 key and initial vector with the public RSA Key shared by third party system?
Approach one:-
I tried with below code and getting following error “System.SecurityException: Invalid Crypto Key”
Requirement:-
1. Randomly generate a AES-256 key.
2. Randomly generate an IV.
3. Encrypt the json with the AES key and IV, base64-encode it, and set it as the request
body.
4. Encrypt the AES key with the public RSA key and base64-encode it
5. Encrypt the IV with the public RSA key and base64-encode it
Implementation:
From the above requirement, I am able to implement the point 1, 2, and 3. Now the technical challenge which we are facing here is, how to convert the AES-256 key and initial vector with the public RSA Key shared by third party system?
Approach one:-
I tried with below code and getting following error “System.SecurityException: Invalid Crypto Key”
Blob encryptedData = Crypto.sign(‘RSA‘, ‘AEX-256 Key’, ‘AEXThirdPartyPublicKey );
Approach two:- Tried below, however client is getting "java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block" https://gist.github.com/karmats/4270441be5a34fff7062 Please let me know if anybody would have came across above scenario. Client shared Public key in .Pem format not the private key.
https://crypto.stackexchange.com/questions/18031/the-modulus-of-rsa-public-key
Blob initialVector = Crypto.generateAesKey(128);// Random generated value
HttpRequest httpRequest = new HttpRequest();
httpRequest.setEndPoint(Label.AEX_EndPoint);
// httpRequest.setEndPoint('https://requestb.in/zno8xizn');
httpRequest.setMethod(Constants.Method_Post);
httpRequest.setHeader(Constants.Content_Type,Constants.Content_Type_Json);
httpRequest.setHeader(Constants.Authorization, authorization);
httpRequest.setHeader(Constants.Key, encryptMyKeys(aesKey));
httpRequest.setHeader(Constants.Iv, encryptMyKeys(initialVector));
System.debug('requestJSON @@'+requestJSON);
httpRequest.setBody(encryptRequest(requestJSON,aesKey,initialVector));
httpRequest.setTimeout(Constants.Timeout);
logger = new API_Utils_Logger(httpRequest, Constants.AEX_Publish_Api_Name);