0

I have problem with working with digital signature.

In controller encryptWithManagedIV() function is encrypted and decrypted properly. But i don't know how to work with signWithCertificate, how to get the original text back from encrypted data. Any one can help me.

"SelfSignedCert_01Aug2017_114100" my org certificate name , we can get in Certificate and Key Management inside setup .

Visual force page :

<apex:page controller="DigiSig" >
     <apex:form >
        <apex:pageBlock id="section" title="My Content" mode="edit">            
            <apex:pageBlockSection title="My Content Section"  columns="5">
                <apex:outputText > originalText : {! originalText } </apex:outputText>
                <apex:outputText > encryptedText : {! encryptedText } </apex:outputText>
                <apex:outputText > decryptedText : {! decryptedText } </apex:outputText>                        
            </apex:pageBlockSection>

            <apex:pageBlockButtons location="top">
            <apex:commandButton action="{!signWithCertificate}" reRender="section" value="signWithCertificate"/>
                <apex:commandButton action="{!encryptWithManagedIV}" reRender="section" value="encryptWithManagedIV"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>       
    </apex:form>    
</apex:page>

Controller : DigSig

public class DigiSig {
    public String originalText{get;set;}
    public String encryptedText{get;set;}
    Public String decryptedText{get;set;}

    public DigiSig(){}  

    public void signWithCertificate(){
        decryptedText='';
        originalText= 'Test data to encrypted';
        Blob data = Blob.valueOf(originalText);
        Blob ans= System.Crypto.signWithCertificate('RSA-SHA256', data, 'SelfSignedCert_01Aug2017_114100'); // "SelfSignedCert_01Aug2017_114100" my org certificate name , we can get in Certificate and Key Management inside setup 
        encryptedText= EncodingUtil.base64Encode(ans);

        ans= System.Crypto.signWithCertificate('RSA-SHA256', ans, 'SelfSignedCert_01Aug2017_114100');

        // how to get decrypted text here from "ans"
        // decryptedText=  ???????

        System.debug('************  ans' + ans);
        System.debug('************  working');  
    }

    public void encryptWithManagedIV(){
        originalText= 'Test data to encrypted';
        Blob data = Blob.valueOf(originalText);
        Blob  key = Crypto.generateAesKey(256);
        Blob ans =Crypto.encryptWithManagedIV('AES256', key, data);
        encryptedText= EncodingUtil.base64Encode(ans);

        decryptedText= Crypto.decryptWithManagedIV('AES256', key, ans).toString();

        System.debug('************  ans' + ans);
        System.debug('************  working'); 
    }

}

1 Answer 1

2

You cannot recover the original data from a signature. A signature is proof that a specific input was signed with a specific certificate. You use a signature to verify the authenticity of data you have received. A mismatch of the signature means that the data has been altered. If you want to encrypt and decrypt data, use the encrypt/encryptWithManagedIv and decrypt/decryptWithMangedIv methods.

10
  • In order to verify the signature in other peer , user must have this certificate right. That means each peer i have to share my certificate with them ?
    – NITHESH K
    Commented Aug 29, 2017 at 16:58
  • @NITHESHK If you're doing peer authentication, each will have their own private certificate and public certificate. The private certificate is used to create the signature, and the public certificate is used to verify the signature. So, you would give your public certificate to the peer, and they could verify the signature.
    – sfdcfox
    Commented Aug 29, 2017 at 17:03
  • @sfdfox i have few more doubt on Certificate generated by the org , this is "SelfSignedCert_01Aug2017_114100" org generated certificated i have to use in signWithCertificate(), And i don't have two certificate for private and public . How can achieve this.
    – NITHESH K
    Commented Aug 30, 2017 at 4:56
  • "SelfSignedCert_01Aug2017_114100" certificate i have used to encrypt the data, same "SelfSignedCert_01Aug2017_114100" certificate only i have to share with peer ?
    – NITHESH K
    Commented Aug 30, 2017 at 4:57
  • if your saying , "The private certificate is used to create the signature, and the public certificate is used to verify the signature." then how can i generate private certificate & public certificate and how to used it in above code . Please any help will appreciated.
    – NITHESH K
    Commented Aug 30, 2017 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .