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
tulasi ram 1tulasi ram 1 

How to decrypt some value in Apex which is coming as encrypted value from Javascript using btoa function

Hi we are using javascript remoting in visualforce pages. There is a method in javascript that is passing a password in encrypted form using btoa function. Then this data is recieving by one Apex method which is annotated by @remoteAction. But how i can decrypt that password in Apex. Please help in this.
MUHAMMED SEMIN P NMUHAMMED SEMIN P N
Hi tulasi,
To encrypt some value we have to use some key value that can be hard coded or we can generate key also by using this
Blob cryptoKey = Crypto.generateAesKey(256);
We have to use same key to decrypt that value.
Here I am going to share some code.Hope it will help you. 
write the below code in your apex method.
Blob data = EncodingUtil.base64Decode(password);
Blob decryptedData = Crypto.decryptWithManagedIV('AES128', cryptoKey , data);
String dryptData = decryptedData.toString();
System.debug('Printing decryptData '+decryptData);
if it help you, please mark it as a best answer !!!
thanks


 
PINKY REGHUPINKY REGHU
Hi,
       To decrypt that password in Apex:
Blob decodeString = EncodingUtil.base64Decode(encryptpasswd);
		encryptpassword=decodeString.toString();
The complete code is given below:
vf page
<apex:page controller="submiformcontroller1">
    <script type="text/javascript">
    function remoteJS() 
    {
        var unameJS = document.getElementById('uname').value;   
         var passJS = document.getElementById('password').value; 
        var encryptpass= btoa(passJS);
		alert(encryptpass); 
        submiformcontroller1.getdecryptpassword(encryptpass,
        function(result, event)
        {
          alert('event.status==>'+event.status);
           if (event.status) 
            {
                	 alert('the password is passed');
                }
        });
    }
    </script>
   username:<input id="uname" type="text" />
    password:<input id="password" type="text" />
    <button onclick="remoteJS()">pass password</button>
    <div id="errors-js"> </div>
</apex:page>

controller:
global class submiformcontroller1
{
   public String usename { get; set; }
    public String passwrd { get; set; }
    public static String encryptpassword{get;set;}
    public submiformcontroller1() {
    }
    @RemoteAction
    global static String getdecryptpassword(String encryptpasswd) 
    {
        System.debug(encryptpasswd);
       Blob decodeString = EncodingUtil.base64Decode(encryptpasswd);
		encryptpassword=decodeString.toString();
        System.debug(encryptpassword);
        return encryptpassword;
    }  
}

Please let me know if that helps you.
If it helps don't forget to mark this as a best answer!!!