Password 2-way field set to single character

Robert Beeman
Kilo Sage

I'm trying to set a Password 2-way type field value programatically. I've noticed that if I set this with a single character, like below, then the field is empty when I decrypt it. If I set it to two or more characters then it decrypts to the proper value.

What makes this more confusing is that if I set the value to a single character manually from the form, and update the record, then it decrypts to my value.

var gr = new GlideRecord('u_my_table');

gr.addQuery('sys_id','9c5127614fb832009363b5e18110c7f1');

gr.query();

if (gr.next()) {

        gr.u_secret = '3'; //Password 2-way

        gr.update();

}

var gr = new GlideRecord('u_my_table');

gr.addQuery('sys_id','9c5127614fb832009363b5e18110c7f1');

gr.query();

if (gr.next()) {

        var Encrypter = new GlideEncrypter();

        var decrypted = Encrypter.decrypt(gr.u_secret);

        gs.info(decrypted); // Empty if set to single character, correct value if more than one character

}

Am I doing something wrong or is this expected behavior? Thank in advance!

1 ACCEPTED SOLUTION

Sumit Maniktala
Kilo Expert

I see the issue now



You need to encrypt the text before you set it so that it is padded to multiple of 8 characters & that is what system expects. So do this



var gr = new GlideRecord('u_test');


gr.addQuery('sys_id', '69bb303e4f30320038f527118110c727');


gr.query();


if(gr.next())


{


//Here is what you need to do, encrypt the content before setting in field


var Encrypter = new GlideEncrypter();


var encrypted = Encrypter.encrypt('3');



gr.u_test_pwd = encrypted;


gr.update();


}



and now when you retrieve it via script it will return the expected result



var gr1 = new GlideRecord('u_test');


gr1.addQuery('sys_id', '69bb303e4f30320038f527118110c727');


gr1.query();


if(gr1.next())


{


var Encrypter = new GlideEncrypter();    


var decrypted = Encrypter.decrypt(gr1.u_test_pwd);    


gs.log(decrypted);


}


View solution in original post

2 REPLIES 2

Sumit Maniktala
Kilo Expert

I see the issue now



You need to encrypt the text before you set it so that it is padded to multiple of 8 characters & that is what system expects. So do this



var gr = new GlideRecord('u_test');


gr.addQuery('sys_id', '69bb303e4f30320038f527118110c727');


gr.query();


if(gr.next())


{


//Here is what you need to do, encrypt the content before setting in field


var Encrypter = new GlideEncrypter();


var encrypted = Encrypter.encrypt('3');



gr.u_test_pwd = encrypted;


gr.update();


}



and now when you retrieve it via script it will return the expected result



var gr1 = new GlideRecord('u_test');


gr1.addQuery('sys_id', '69bb303e4f30320038f527118110c727');


gr1.query();


if(gr1.next())


{


var Encrypter = new GlideEncrypter();    


var decrypted = Encrypter.decrypt(gr1.u_test_pwd);    


gs.log(decrypted);


}


Awesome! I knew I must have been missing something. Thank you so much. +points to you