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
Jagadeesh AngadiJagadeesh Angadi 

How to decrypt a message using asymmetric RSA algorithm?

I have a private key with me, I have to decrypt a message using my private key with asymmetric RSA algorithm, Please help with the documentation or sample code on how to decrypt a message using asymmetric RSA.
ShivankurShivankur (Salesforce Developers) 
Hi Jagadeesh,

You should be able to make use of Crypto Class available in Apex which provides methods for creating digests, message authentication codes, and signatures, as well as encrypting and decrypting information.

Reference link for documentation: 
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_crypto.htm

Reference link for example code:
https://rajvakati.com/2018/03/11/apex-crypto-example/

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Jagadeesh AngadiJagadeesh Angadi
@Shivankur, Reference link you shared for example code says - 'only symmetric private key encryption using the AES algorithm is supported'. But, I need to decrypt using asymmetric RSA algorithm. Do you have any references for decryption using asymmetric RSA algorithm.
ShivankurShivankur (Salesforce Developers) 
Hi Jagadeesh,

Please take a note of following:
The only similarity both RSA and AES has is that they are encryption algorithms.
RSA :
  • It is an asymmetric key algorithm. Meaning, it uses 2 different keys (Public key and Private key) for encryption and decryption. Public key is available to open world, where as private key is possessed by owner.
  • Public Key encryption is used for exchanging data.
  • Private Key encryption is used for authentication of owner.(digital signatures)
  • It is stream cipher algorithm. Meaning, entire data is encrypted at once, which takes more computational power. Hence it is slow. Mainly used for exchanging little information such as symmetric keys.
  • RSA's strength and weaknesses lies in the factoring large integers.
AES :
  • It is a symmetric key algorithm. Meaning, same key is used for both encryption and decryption.
  • It is a 128-block cipher algorithm. Meaning, the data is divided into chunks of fixed length data(128 bits). The chunks are processed in AES where each round is dependent on output of its predecessor. Large data can be encrypted using AES.
  • AES's strength is in the possible key permutations using Rijndael finite field method internally.
Hope it helps. Cheers.