Decrypting a value to set as password not working

ahaz86
Mega Guru

Hello,

As part of a requirement for creating certain users we have a form which asks for a password that upon submission is encrypted using GlideEncrypter().

The problem is when I try to decrypt the value and set it as the password it is not working correctly as the decrypted value does not work as the password.

Below is the code I use to decrypt and set the password.

var Encrypter = new GlideEncrypter();

var decrypted = Encrypter.decrypt(current.variables.password);

var gu = new GlideRecord('sys_user');

gu.initialize();

gu.user_name = current.variables.headless_id;

gu.first_name = current.variables.headless_id;

gu.last_name = current.variables.headless_id;

gu.email = current.variables.email_address;

gu.user_password.setDisplayValue(decrypted.toString());

//gu.user_password.setDisplayValue(decrypted);

//gu.user_password.setValue(decrypted);

gu.user_password.setDisplayValue(decrypted.toString());

gu.insert();

If I do not encrypt the password this script works fine though.

Any thoughts on this?

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Alexander,



The problem is that line 2 above sets the decrypted variable as what I am assuming is a Java string.   Calling toString on that object, likewise, returns another string.   If you do a typeof decrypted it will return "object" and not 'string'.   The setDisplayValue function expects a string and apparently has no idea how to handle the object it is given.   The solution is to add + '' which forces a type conversion to a javascript string that setDisplayValue can use.   Thus, the following function should work for you:



var Encrypter = new GlideEncrypter();


var decrypted = Encrypter.decrypt(current.variables.password);



var gu = new GlideRecord('sys_user');


gu.initialize();


gu.user_name = current.variables.headless_id;


gu.first_name = current.variables.headless_id;


gu.last_name = current.variables.headless_id;


gu.email = current.variables.email_address;


gu.user_password.setDisplayValue(decrypted + '');


gu.insert();



I hope this helps.



Kind regards,



Travis


View solution in original post

7 REPLIES 7

Here is my script for the business rule on the incident table



(function executeRule(current, previous /*null when async*/) {


  // Add your code here


  var Encrypter = new GlideEncrypter();


  var decrypted = Encrypter.decrypt(current.u_encrypted_password);



  var gu = new GlideRecord('incident');


  gu.initialize();


  gu.u_encrypted_password.setDisplayValue(decrypted + '');


  gu.insert();



})(current, previous);


naresh1019
Mega Expert

In my demo Instance, when I am trying to print the password field (from sys_user table), it is showing a decrypted value.



1) I would like to know


            which script/BR is responsible for converting password ...into decrypted value.


2) As a admin user if I want to print all the password values, --> how should i approach this...



Note: The above questions are only to enhance my knowledge


David Pichard
Mega Guru

Is it possible to use GlideEncrypter() in workflow run script activities? So far no luck. Wondering if I need to reference a full package name of some sort?