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
lucena69lucena69 

APEX Crypto.sign() - verify/decrypt in C# .NET

Hi Everyone,

 

I'm signing a token using the APEX Crypto.sign() method.

I'm then trying to verify/decrypt the signature in C# .NET but I'm not having much luck.

 

Has anyone had any experience with this? What's the correct approach methodology? (Even if not in .NET but in something else?)

 

I've generated the PKCS8 private key using openssl.exe, and salesforce seems to sign it without error.

I've then tried to use the public key in C# .NET but I'm not seeing the right result.

 

Any thoughts/tips would be appreciated.

 

Thanks,

-lucena

 

AloneAlone

You got the solution , I am facing same issue

AbhiSFDCAbhiSFDC

Hi

 

Did you got solution for this.

 

Because i am also trying to do same. It will be great if you let me know how did you fix it.

 

Apex Code -

Apex Code : - To generate Signature

 

Note: I have stored PKCS#8 format key in Authentication object.

 

Code:

public Pagereference getSignatureWithSign() {

        Authentication__c auth = [Select Token__c from Authentication__c limit 1];

        String privateKey = auth.Token__c ;

       

        Blob signature = Crypto.Sign('RSA',

                            Blob.valueOf('SFDC'),

                            EncodingUtil.base64Decode(privateKey ));

        displaySIGNRSASignature = EncodingUtil.base64Encode(signature);

        return null ;

}



 

C# Code -

public static bool VerifySignature(string signedMessage)

        {

 

            bool isVerified = false;

            try

            {

                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

                string certSubjectName = "SalesForce";

                rsa = (RSACryptoServiceProvider)RetrivePublicKeyFromCertificate(certSubjectName);

                byte[] SignDataBytes = Convert.FromBase64String(signedMessage);

                byte[] signatureToVerify = Encoding.Unicode.GetBytes("SFDC");

                isVerified = rsa.VerifyData(signatureToVerify, CryptoConfig.MapNameToOID("SHA1"), SignDataBytes);

 

            }

            catch (Exception ex)

            {

                throw ex;

            }

            return isVerified;

        }