Source language: Translate to:

JScript function: XOR encryption of string with a password

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

JScript function: XOR encryption of string with a password

Postby dpayer » Fri Mar 09, 2012 1:54 pm

The JScript function posted below is adapted from this script:

http://www.javascriptsource.com/passwor ... 82201.html

It is a lightweight encryption process for an ascii string that lets you set a password for the encryption. You can use the encrypted string to transport valuable information over insecure paths and then decrypt it with the password when desired.
Image
This is for strings less than 500 bytes. More than enough for passwords but not quite robust enough for full documents. Feel free to remove the popup box at the bottom indicating it is either encrypting or decrypting. There are a couple other popups when password length or no content is entered. You can remove them if you want no alert displays.

The function both encrypts and decrypts. When you call it, you will be requested to give 4 variables:
[%1] name of variable containing the string to be encrypted/decrypted
[%2] name of variable containing the password used to encrypt
[%3] variable with numeric value. encrypt=2 , decrypt=1
[%4] variable in your app to receive the returned value.

Sample pub calls function by name: encrypt_decrypt
encrypt_decrypt function to place in your functions folder

encrypt_decrypt function:

Code: Select all
{NeoBook Function}
Version=5.70
Language=JScript
Param=[%1]|Variable|String to be encrypted / decrypted
Param=[%2]|Variable|Password to encrypt / decrypt
Param=[%3]|Variable|Variable with numeric value.  encrypt = 2 or decrypt = 1
Param=[%4]|Variable|Variable to receive returned value
{End}
function encrypt() {
var MsgBox=new ActiveXObject("wscript.shell")
  if(pwd == null || pwd.length <= 0) {
    MsgBox.Popup("No encryption password provided.");
    return null;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.ceil(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  if(mult < 2) {
    MsgBox.Popup("Algorithm cannot find a suitable hash. Please provide a more complex or longer password. ");
    return null;
  }
  var salt = Math.round(Math.random() * 1000000000) % 100000000;
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i++) {
    enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
    if(enc_chr < 16) {
      enc_str += "0" + enc_chr.toString(16);
    } else enc_str += enc_chr.toString(16);
    prand = (mult * prand + incr) % modu;
  }
  salt = salt.toString(16);
  while(salt.length < 8)salt = "0" + salt;
  enc_str += salt;
  return enc_str;

}

function decrypt() {
var MsgBox=new ActiveXObject("wscript.shell")
  if(str == null || str.length < 8)
  {
    MsgBox.Popup("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
    return;
  }
  if(pwd == null || pwd.length <= 0)
  {
    MsgBox.Popup("No decryption password provided.");
    return;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++)
  {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.round(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  var salt = parseInt(str.substring(str.length - 8, str.length), 16);
  str = str.substring(0, str.length - 8);
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr2 = "";
  var enc_str2 = "";
  for(var i=0; i<str.length; i+=2)
  {
    enc_chr2 = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
    enc_str2 += String.fromCharCode(enc_chr2);
    prand = (mult * prand + incr) % modu;
  }
  return enc_str2;

}
var MsgBox=new ActiveXObject("wscript.shell")
var str=nbGetVar('[%1]');
var pwd=nbGetVar('[%2]');
var encrmode=nbGetVar('[%3]');

if (encrmode<2)
   {
   MsgBox.Popup("Decrypting");
    var enc_str2=decrypt(str, pwd);
    nbSetVar ('[%4]', enc_str2);
    }
else if (encrmode>1)
   {
   MsgBox.Popup("Encrypting");
    var enc_str=encrypt(str, pwd);
     nbSetVar ('[%4]', enc_str);
    }
   
else
{
var MsgBox=new ActiveXObject("wscript.shell")
MsgBox.Popup("Mode not selected.")
}
delete MsgBox;
Last edited by dpayer on Tue Jul 10, 2012 12:48 pm, edited 1 time in total.
User avatar
dpayer
 
Posts: 1382
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby chromebandit » Fri Mar 09, 2012 6:44 pm

Excellent

Thanks for sharing this function - this is top and it will be 1 of my favorized functions i used a plugin before - now thanks 2u my pub is 300kb smaller same function :mrgreen:

Best Regards
User avatar
chromebandit
 
Posts: 283
Joined: Wed Nov 24, 2010 5:00 pm


Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest