Viewing source code files from the server
which support rendering for: CryptoDemo
index.html :
entry point, with only SSI (Server Side Include) statements
<!--#include virtual="/inc/site_meta.ssi" --> <!--#include virtual="CryptoDemo.html" --> <!--#include virtual="/inc/bottom.ssi" -->
CryptoDemo.html :
Main HTML/JS source
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- Ron Miller, April 2013 --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>EnCrypt using JS</title> <!--script type="text/javascript" src="jquery.js"></script--> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script type="text/javascript" src="md5.js"></script> <script type="text/javascript" src="utf_XL8.js"></script> <script type="text/javascript" src="Crypto_RM.js"></script> </head> <body bgcolor="#E8FCFF"> <font size="4"><b><center>This page demonstrates the use of JavaScript for encryption.</center></b></font> <font size="2">Using my own, highly optimized, algorithms: <a href="Crypto_RM.js">Crypto_RM.js</a> Regarding the use of JS please see: <a href="http://www.fourmilab.ch/javascrypt/">Why Browser-Based Encryption</a> <a href="viewSrc.php"> view server code </a> <br/> <a href="/CyFile/">More advanced web page using 'Crypto_RM.js'</a> </font><br/><br/> <select style="width: 160px;" name="algo" id="algo" value="4" onchange="javascript:algoChg();" > <option value="">- Select an Encryption -</option> <option value="0">E-Cipher </option> <option value="3">Enc32b</option> <option value="4">Enc425</option> <option value="5">BiCrypt</option> <option value="9">a Psuedo Rand # Xor</option> <option value="10">#2 PRN Xor algorithm</option> <option value="11">MD5-Xor</option> </select> Keycode string: <input type="text" name="kcodeStr" id="kcodeStr" value="One's Own-Secret code"> <input type="button" value="Load a BiCrypted file" onClick="javascript: loadCrypttext();"> <input type='text' id="codefile" value='Sample.enc'/> <br/><span id="warning" style="color:orange"></span> <span id="info" style="color:blue"></span><br/> <font size="1"> </font><br/> <textarea rows='8' style='width: 1000px;' name="textIN" id="textIN" > - Enter text Here - </textarea><br/> <input type="button" value="enCrypt" onClick="enCrypt()"> <font color='gray'>Note that you could make good/fun use of these by cutting & pasting to & from these text areas</font><br/> <textarea rows='8' style='width: 1000px;' name="Crypttext" id="Crypttext" ></textarea><br/> <input type="button" value="deCrypt" onClick="deCrypt()"><br/> <textarea rows='20' style='width: 1000px;' name="textOUT" id="textOUT" ></textarea><br/> <div id="show"></div> <hr/> <!---------------------------------------------------------------------------------------------> <script language="javascript" type="text/javascript"> var encrypted; var debug = false; $("#algo").val(4); // for testing init to algo 4 function algoChg(){ var algo = $("#algo").val(); switch (algo) { case '0': $("#info").html("For more information on this algorithm see: <a href='http://raspi.ddns01.com/E-Cipher/'> E-Cipher, a hand performed cipher algorithm with emphasis on Efficiency. </a>"); break; case '3': $("#info").html("Encrypts into 4 binary bytes at a time."); break; case '4': $("#info").html("For each 4 bytes it produces 5 printable characters"); break; case '5': $("#info").html("Algorithm combining 'Enc32b' and 'Enc425'"); break; case '9': case '10': $("#info").html("Algorithm based on XORing with Pseudo Random Numbers"); break; case '11': $("#info").html("Utilizes a rolling MD5; indirectly based on the keycode string."); break; default: $("#info").html(""); } } function enCrypt(){ var clearText = $('#textIN').val(); //var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase") //$('#Crypttext').html(encrypted.ciphertext.toString()); getKeyCodes(); var algo = $("#algo").val(); //alert ("algo: " + algo); switch (algo) { case '0': case '1': case '2': alert ("Not yet ported to this JS applet.\nWill use Enc32."); $("#algo").val(3); case '3': encrypted = enc32b(clearText); break; case '4': encrypted = enc425(utf16To8(clearText)); break; case '5': encrypted = BiCrypt(clearText); // aka the BiCycle Algorithm break; case '9': encrypted = aHashXor(clearText); break; case '10': encrypted = Hash2Xor(clearText); break; case '11': encrypted = MD5_Xor(clearText); break; default: alert ("Sorry: Invalid algo number."); } //alert(encrypted); $('#Crypttext').text(encrypted); $('#Crypttext').val(encrypted); // do both otherwise it won't change after manual editing //alert(encrypted.ciphertext.toString()); } function deCrypt(){ var cyText = $('#Crypttext').val(); //alert(decrypted.plaintext.toString()); //$('#textOUT').html(decrypted.toString()); getKeyCodes(); var algo = $("#algo").val(); //alert ("algo: " + algo); switch (algo) { case '0': case '1': case '2': alert ("Not yet ported to this JS applet.\nWill use Enc32."); $("#algo").val(3); case '3': //var decrypted = dec32b(cyText); // give sporatic errors, probablly due to textarea not fully handling binary var decrypted = dec32b(encrypted); break; case '4': var decrypted = utf8To16(dec425(cyText)); break; case '5': var decrypted = deBiCrypt(cyText); break; case '9': //var decrypted = aHashXor(cyText); // gave sporatic errors var decrypted = aHashXor(encrypted); break; case '10': var decrypted = Hash2Xor(encrypted); break; case '11': var decrypted = deMD5_Xor(cyText); break; default: alert ("Sorry: Invalid algo number."); } $('#textOUT').text(decrypted); $('#textOUT').val(decrypted); // do both otherwise it won't change after manual editing } // utility to setup keyCodes for the Crypto_RM.js package //------------------------------------------------------- function getKeyCodes() { kcodeStr = $("#kcodeStr").val(); if (kcodeStr.length<20) { $("#warning").html("Warning: The Keycode string should be at least 20 characters long."); } else { $("#warning").html(""); } if (kcodeStr.indexOf(":") == -1) { kcodeStr = ':' + kcodeStr + ':';} setKeyCodeStr(kcodeStr); hexmd5 = hex_md5(kcodeStr); //alert("hexmd5=" + hexmd5 ); setMasterKey(hexmd5); } function loadCrypttext() { $('#Crypttext').load($('#codefile').val(), function() { //alert("text=" + $('#Crypttext').text()); alert("val=" + $('#Crypttext').val()); $('#Crypttext').val($('#Crypttext').text()); // needed in case textarea was edited }); $('#algo').val(5); algoChg(); alert('Encrypted text will be loaded. Click \'deCrypt\' to see plain text.'); } </script> </body> </html>
Crypto_RM.js :
My own Crypto library
/* * A JavaScript (hence 53bit) implementation of my * Random Number Generators / Hashes, * Radix string handling functions; * and Encryption ciphers which utilizes them * * Version 1.0 Copyright (c) Ron Miller 2013 * Distributed under the BSD License * * A good encryption algorithim should take non random input and render it as random data! rrm * To test the randomosity of my algorithms I rigorously tested them, * using an expanded version of John Walker's ENT program. (http://www.fourmilab.ch/random/) * */ // some globals chr89 = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM~!@#$%^`*()_+-=,./;'[]{}:?|"; // note: '&' gave issue var chr89x2 = chr89 + chr89; var debug = false; var masterKey = ''; var crm_kcodeStr = ''; var hexmd5 = ''; // KeyCodes held globally var kcode1 = 123456; // To later be based on a MD5 string var kcode2 = 314159; // "" var kcode3 = 612561; // var kcode4 = 933768; // // diag vars //var va = new Array(); //var m = new Array(); //var r = new Array(); //var t = new Array(); //var k4 = new Array(); //------------------------ E N C 4 2 5 ----------------------- // output is all printable chars of a set of 89 characters // for each 4 bytes IN there is 5 chars OUT (hence: 425) function enc425(clearText){ // input must be UTF-8 string or array bytes !! does not work with UTF-16 strings // can be called like this: enc425(utf16To8(utf16_clearText)) ... later ... utf8To16(dec425(clearText)) // scramble the char89 string with kcode1,2 //alert ("chr89=" + chr89); //alert("kcode1=" + kcode1 + "\nkcode2=" + kcode2 + "\nkcode3=" + kcode3 + "\nkcode4=" + kcode4); //alert("MD5 of 'chr89'=" + hex_md5(chr89)); var nulls = "\0\0\0\0"; if (clearText.length % 4) clearText += nulls.substring(4-(clearText.length % 4)); Sstr = NShuffle(kcode1, chr89); Sstr = NShuffle(kcode2, Sstr); chr89x2 = Sstr + Sstr; //alert ("new chr89=" + Sstr); //alert("MD5 of the new 'chr89'=" + hex_md5(Sstr)); var cyText = ''; var bnum=0; //alert("MD5 of 'clearText': " + hex_md5(clearText)); while (clearText) { bnum++; //val32 = 256 * (256 * (256 * clearText.charCodeAt(0) + clearText.charCodeAt(1)) + clearText.charCodeAt(2)) + clearText.charCodeAt(3); var val32 = 0; for (var i=0;i<Math.min(4,clearText.length);i++) { val32 = 256 * val32 + clearText.charCodeAt(i); } xMask = randN(kcode3,0x100000000); int32r = xor32(val32, xMask); // ~ val32 ^ xMask rinx = randN(kcode4,89); // use rinx in NSradix ... var Sradix = chr89x2.substr(rinx); cyText = cyText + NSradix425(int32r, Sradix); kcode3 = kcode3 + val32; // to effect all later encodes, based on preceeding clear text // now produce next psuedo random number (seed for next cycle) // ... // after multiple batch testing the best for case 431 are: // #1a: 0x4000004000000 // #1b: 0x2000000000000 // #1c: 0x4080201007000 // #3 0xffffffffffff kcode4 = randN(kcode4,0x2000000000000); if (clearText.length>4) clearText = clearText.substr(4); else clearText = ""; } return cyText; } //----------------------- D E C 4 2 5 ------------------------ function dec425(cyText){ // scramble the char89 string with kcode1,2 Sstr = NShuffle(kcode1, chr89); Sstr = NShuffle(kcode2, Sstr); chr89x2 = Sstr + Sstr; var clearText = ''; //var cyText = $('#Crypttext').val(); //var testText = $('#textIN').val(); var chr=0; var bnum=0; while (cyText) { bnum++; str89 = cyText.substr(0,5) rinx = randN(kcode4,89); // use rinx in NSradix ... var Sradix = chr89x2.substr(rinx); int32r = deNSradix425(str89, Sradix); xMask = randN(kcode3,0x100000000); val32 = xor32(int32r, xMask); // ~ int32r ^ xMask kcode3 = kcode3 + val32; // to effect all later encodes, based on preceeding clear text // now produce next psuedo random number (seed for next cycle) kcode4 = randN(kcode4,0x2000000000000); chr0 = (val32 % 256); val32 = Int(val32/256); chr1 = (val32 % 256); val32 = Int(val32/256); chr2 = (val32 % 256); val32 = Int(val32/256); chr3 = (val32 % 256); decodechar = String.fromCharCode(chr3,chr2,chr1,chr0); /// cyText = cyText.substr(5); if (cyText.length>5) { clearText = clearText + String.fromCharCode(chr3,chr2,chr1,chr0); cyText = cyText.substr(5); } else { cyText = ""; if (chr3!=0) clearText += String.fromCharCode(chr3); if (chr2!=0) clearText += String.fromCharCode(chr2); if (chr1!=0) clearText += String.fromCharCode(chr1); if (chr0!=0) clearText += String.fromCharCode(chr0); } chr+=4; } return clearText; } //::::::::::::::::::: E N C 3 2 b :::::::::::::::::::::::: // produces 1:1 bytes of 8bit values; hence output is not all printable chars // encryption is done in 4 byte chunks so there may be upto 3 nulls on the end when decryted function enc32b(utf16Text) { var mdu16 = hex_md5(utf16Text); var kc1 = {val: kcode1}; var kc2 = {val: kcode2}; var val32; var cyText = ''; var bnum=0; var clearText = utf16To8(utf16Text); // var clearText = encodeURIComponent(utf16Text); // var clearText = new TextEncoder("utf-8").encode(utf16Text); //var mdu8 = hex_md5(clearText ); //alert("MD utf16: " + mdu16 + " utf8: " + mdu8 ); if ((clearText.length % 4)>0) { //alert("adding null chars: " + 4-(clearText.length % 4)); // for (var i=(clearText.length % 4); i<4; i++) { // clearText += "\0"; // } var nulls = "\0\0\0\0"; clearText += nulls.substring(clearText.length % 4); /// next line seems right but does not work right /// clearText += nulls.substring(4-(clearText.length%4)); } //alert("Final length=" + clearText.length); while (clearText) { val32 = 0; bnum++; for (var i=0;i<Math.min(4,clearText.length);i++) { val32 = 256 * val32 + clearText.charCodeAt(i); } // this line does the encryption int32r = xor32(xor32(xor32(val32, rand32b_byRef(kc1, kc2)),randN(kcode3,0x100000000)),kcode4); kcode3 = kcode3 + val32; // to effect all later encodes, based on preceeding clear text // add to cyText the 4 byte chars of int32r; str4 = ''; for (var i=0; i<4; i++) { str4 = String.fromCharCode(int32r & 0xFF) + str4; int32r = int32r >>> 8; } cyText = cyText + str4; clearText = clearText.substr(4); } return cyText; } function dec32b(cyText) { var kc1 = {val: kcode1}; var kc2 = {val: kcode2}; var clearText = ""; var bnum=0; while (cyText) { bnum++; var int32r = 0; for (var i=0;i<Math.min(4,cyText.length);i++) { int32r = 256 * int32r + cyText.charCodeAt(i); } ran1 = rand32b_byRef(kc1, kc2); ran2 = randN(kcode3,0x100000000); // this line does the De-encryption val32 = xor32(xor32(xor32(int32r, ran1),ran2),kcode4); kcode3 = kcode3 + val32; // to effect all later encodes, based on preceeding clear text // add to cyText the 4 byte chars of int32r; str4 = ""; for (var i=0; i<4; i++) { str4 = String.fromCharCode(val32 & 0xFF) + str4; val32 = val32 >>> 8; } clearText = clearText + str4; cyText = cyText.substr(4); } // return clearText; var utf16Text = utf8To16(clearText); // var utf16Text = decodeURIComponent(clearText); // var utf16Text = new TextDecoder("utf-8").decode((clearText)); // new Uint16Array(clearText)); return utf16Text; } //==================== Bi C r y p t ===== aka 'BiCycle' algo ================================ // several steps herein were taken to make this algo aggravate & extend brute force attacks // and steps were taken to make this algo non conducive to analysis (eg. that of distribution) function BiCrypt(clearText) { hexmd5 = hex_md5(clearText); raw = hex2rstr(hexmd5); //hexchk = rstr2hex(raw); alert(" hex:"+hexmd5+"\n raw:"+raw+"\n hexchk:"+hexchk); var prefix = String.fromCharCode(kcode1%256) + String.fromCharCode(kcode2%256); // create a set of 4 dummy chars var postfix = String.fromCharCode(kcode3%256) + String.fromCharCode(kcode4%256); // 2 in front & 2 in back // note both enc32 char length & cyText1 length are divisible by 4, needed so we don't get null chars tacked onto the end by enc425 pass1key = hex_md5(crm_kcodeStr + hexmd5 + crm_kcodeStr); setKeyCodes(pass1key); cyText1 = prefix + enc32b(clearText) + postfix + raw; // first pass of encryption by enc32b() setKeyCodes(masterKey); // add 2 random chars to front, so that the encoding blocks have a different alignment than in pass 1 return enc425(cyText1); // second pass of encryption } function deBiCrypt(cyText, textLength) { // textLength is optional // alert("cyText is: " + cyText); setKeyCodes(masterKey); clearText1 = dec425(cyText); // alert("clearText1 is: " + clearText1); rawmd5 = clearText1.substr(clearText1.length-16); hexmd5 = rstr2hex(rawmd5); //alert("rawmd5=" + rawmd5 + "\nRecovered hexmd5=" + hexmd5 ); pass1key = hex_md5(crm_kcodeStr + hexmd5 + crm_kcodeStr); setKeyCodes(pass1key); // remove first 2 junk chars + 2 more & last 16 hexmd5 keycode clearText1 = clearText1.substr(2,clearText1.length-20); //return dec32b(clearText1); clearText = dec32b(clearText1); if (typeof(textLength)!=='undefined' && textLength>0) { clearText = clearText.substr(0, textLength); md5_result = hex_md5(clearText); if (hexmd5 != md5_result) { //alert("Expecteded hexmd5=" + hexmd5 + "\nResulted md5_result=" + md5_result ); var msg = "The resulting file's MD5 figerprint is not = to the original.\nThe results is a scrambble mess.\n\nApparently the pass codes are wrong !\nPlease re-enter the 'Realm/Cue' and 'Passcode'."; alert(msg); clearText = msg; } } return clearText; } // utility functions for handling the keyCodes for the encryption algos //---------------------------------------------------------------------- // page using a crypto algo should have code like this: //function getKeyCodes() { // kcodeStr = $("#kcodeStr").val(); // setKeyCodeStr(kcodeStr); // hexmd5 = hex_md5(kcodeStr); // //alert("hexmd5=" + hexmd5 ); // setMasterKey(hexmd5); //} function setKeyCodeStr(kcodeStr) { crm_kcodeStr = kcodeStr; // put into global var } // Master key for an algorithum // param: 'hexmd5' should be the hex MD5 of more than 20 characters (eg: username + password + site URL) // the password portion should be at least 12 characters function setMasterKey(hexmd5) { masterKey = hexmd5; setKeyCodes(hexmd5); //alert("the Mstr KCode is: " + hexmd5 ); } function setKeyCodes(hexmd5) { // break into four (32bits) integers each from 8hex digits kcode1 = parseInt(hexmd5.substr(0,8),16) kcode2 = parseInt(hexmd5.substr(8,8),16) kcode3 = parseInt(hexmd5.substr(16,8),16) kcode4 = parseInt(hexmd5.substr(24,8),16) //alert("hexmd5=" + hexmd5 + "\nkcode1=" + kcode1 + "\nkcode2=" + kcode2 + "\nkcode3=" + kcode3 + "\nkcode4=" + kcode4); } //=================== some basic (8 bit at a time) Xor-Hash algos ======================= // for testing randomness of local Hash funtions (Note these two hashes do not use the Keycode on the page, when encrypting) // enc32, enc425 & BiCrypt can't be test against any RandomTests.cpp random algo cases due to their enc complexities // but these hashes they used can to checked against // (tested against case 31 of RandomTests.cpp for = output) // & both are used as test ground for 'randN' family of functions function aHashXor(cyText) { verify_rand_funcs(); var clearText = ''; var seed = 235402; // the same initial val used by RandomTests.cpp for (var i=0; i<cyText.length; i++) { chrval = cyText.charCodeAt(i); xorval = randN(seed, 256); // seed = randN(seed, 0x20000000000000); // restrict to 52 bits (2^53-1) 53rd bit is a sign // seed = randN(seed, 0x80000000); // restrict to 31 bits , Nah seed = randN(seed, 0x4000004000000); clearText = clearText + String.fromCharCode(chrval ^ xorval); } return clearText; } // (tested against case 32 of RandomTests.cpp for = output) // & both are used as test ground for 'rand32b' family of functions function Hash2Xor(cyText) { var clearText = ''; // the same init vals used by RandomTests.cpp pn1 = 65521; // Prime Number pn2 = 65551; // seedVar1=314159; seedVar2=7654321; //k = 112; // note that pn1 * pn2 +k, covers the range of 32 bits evenly (with negleable exception) for (var i=0; i<cyText.length; i++) { k = seedVar1 % 223; chrval = cyText.charCodeAt(i); // rt = pn2 * randN(seedVar1,pn1) + randN(seedVar2,pn2) + k; val1 = randN(seedVar1,pn1); val2 = randN(seedVar2,pn2); seedVar1 = randN(seedVar1,0x2000002000000); seedVar2 = randN(seedVar2,0x2000002000000); rt = pn2 * val1 + val2 + k; xorval = rt % 256; clearText = clearText + String.fromCharCode(chrval ^ xorval); } return clearText; } //=================== an algos based on Xoring a MD5 hash ======================= function MD5_Xor(utf16Text) { // use each byte worth of bit, until the whole MD5 is used then get another var cyText = ''; var strinx=0; var clearText = utf16To8(utf16Text); //alert("hexmd5 + kcodeStr " + hexmd5 + " " + kcodeStr); for (var i=0; i<clearText.length; i++) { if (strinx == 0) { tststr = hexmd5 + kcodeStr + hexmd5; hexmd5 = hex_md5(tststr); strinx = hexmd5.length; //alert("tststr : " + tststr); //alert("MD5 hexmd5 : " + hexmd5); } strinx -= 2; MDhexval = hexmd5.substring(strinx, strinx+2); xorval = parseInt( "0x" + MDhexval); chrval = clearText.charCodeAt(i); //cyText = cyText + String.fromCharCode(chrval ^ xorval); intval = (chrval ^ xorval); //intval = xor32(chrval, xorval); //alert("chrval, xorval, intval: " + chrval + " " + xorval + " " + intval); if (intval<16) cyText += '0'; // else a single digit would be added cyText = cyText + intval.toString(16); //alert("xorval = " + MDhexval + " in dec: " + xorval); } return cyText; // as hex chars } function deMD5_Xor(cyText) { // convert each set of 2 hex chars to original text char var clearText = ''; var strinx=0; //alert("hexmd5 + kcodeStr " + hexmd5 + " " + kcodeStr); for (var i=0; i<cyText.length; i+=2) { if (strinx == 0) { tststr = hexmd5 + kcodeStr + hexmd5; hexmd5 = hex_md5(tststr); strinx = hexmd5.length; //alert("tststr : " + tststr); //alert("MD5 hexmd5 : " + hexmd5); } strinx -= 2; MDhexval = hexmd5.substring(strinx, strinx+2); xorval = parseInt( "0x" + MDhexval); Cyhexval = cyText.substring(i, i+2); Cyintval = parseInt( "0x" + Cyhexval); intval = (Cyintval ^ xorval); //intval = xor32(Cyintval, xorval); //alert("Cyhexval, Cyintval, xorval, intval: " + Cyhexval + " " + Cyintval + " " + xorval + " " + intval); clearText = clearText + String.fromCharCode(intval); //alert("xorval = " + MDhexval + " in dec: " + xorval); } /// return clearText; var utf16Text = utf8To16(clearText); return utf16Text; } //---------------------------------------------------------------------------- // My sudo standard random Number algo, tweeked for JS operation // seedVar ( <53 bit unsigned var perferred) // returns a number 0 - (limit-1) //////////////////////////////////////////////////////////////// function randN(seedVar, limit) { var term2 = Math.floor(seedVar/8162251); // first used: 4194301 term2 += seedVar % 257; seedVar = seedVar % 8162251; // ~ 22 bits so next line does not produce more than 53 bits (JS resolution) seedVar = seedVar * 1103515245 + term2 + 12345; // std by Kernighan and Ritchie // seedVar = seedVar + (seedVar>>>11) ; // my improvement // seedVar = seedVar + (Math.floor(seedVar/0x400000)) ; // += seedVar>>11 seedVar = seedVar + (Math.floor(seedVar/4194301)) ; retval = seedVar % limit; return retval; // return seedVar ; } // the seed (keycode) is passed by reference and auto updated herein function randN_byRef(keycode, limit) { var term2 = Math.floor(keycode.val/8162251); term2 += keycode.val % 257; seedVar = keycode.val % 8162251; seedVar = seedVar * 1103515245 + term2 + 12345; // std by Kernighan and Ritchie //seedVar = seedVar + (seedVar>>>11); // my improvement // seedVar = seedVar + (Math.floor(seedVar/0x400000)) ; // += seedVar>>11 seedVar = seedVar + (Math.floor(seedVar/4194301)) ; // setup the keycode seed for the next random # generation cycle //keycode.val = seedVar; // update passed by reference var // after multiple batch testing the best for case 432 are: // #1: 0x2000002000000 // #1: 0x4000004000000 // #1: 0x4000002000000 // #2: 0x2000000000000 // #3 0xffffffffffff keycode.val = randN(keycode.val, 0x4000004000000 ); return seedVar % limit; } // Produce a random 32 bit value which is well random accross all 32 bits, as is practical // require two independant 64 bit (psuedo, actually 53bit) running seeds (ok to have been initialized with 32b values) function rand32b(seedVar1, seedVar2) { pn1 = 65521; // Prime Number pn2 = 65551; // k = 112; // note that pn1 * pn2 +k, covers the range of 32 bits evenly (with negleable exception) k = seedVar1 % 223; return pn2 * randN(seedVar1,pn1) + randN(seedVar2,pn2) + k; } function rand32b_byRef(seedVar1, seedVar2) { pn1 = 65521; // Prime Number pn2 = 65551; // //k = 112; // note that pn1 * pn2 +k, covers the range of 32 bits evenly (with negleable exception) k = seedVar1.val % 223; return pn2 * randN_byRef(seedVar1,pn1) + randN_byRef(seedVar2,pn2) + k; } function verify_rand_funcs() { // compare randN() results limit = 0x1000000000000; byRef_limit = 0x4000004000000; iseed = 123456789; r1 = randN(iseed, limit); seed = randN(iseed, byRef_limit); r2 = randN(seed, limit); // to randN_byRef() results var refSeed = {val: iseed}; r1a = randN_byRef(refSeed, limit); r2a = randN_byRef(refSeed, limit); if (r2 != r2a) { alert("randN's rand number: " + r2 + "\nrandN_byRef's rand#: " + r2a); } // compare rand32b() results iseed2 = 19490407; // my birth day r1 = rand32b(iseed, iseed2); seed = randN(iseed, byRef_limit); seed2 = randN(iseed2, byRef_limit); r2 = rand32b(seed, seed2); // to rand32b_byRef() results var refSeed = {val: iseed}; var refSeed2 = {val: iseed2}; r1a = rand32b_byRef(refSeed, refSeed2); r2a = rand32b_byRef(refSeed, refSeed2); if (r2 != r2a) { alert("randN's rand number: " + r2 + "\nrandN_byRef's rand#: " + r2a); } } //---------------------------------------------------------------------------- // func: result = opA XOR opB for 32 bits // uses the unsigned 32 LSB of the inputs (where as the JS ^ first turns the inputs into signed values, boo hiss) function xor32(opA, opB) { result = (opA % 0x80000000) ^ (opB % 0x80000000); if ((opA>0x7FFFFFFF && opB < 0x80000000) || (opA<0x80000000 && opB>0x7FFFFFFF )) { result += 0x80000000; } return result; } //====================================== // Some VB like utilitits, used locally function StrMid(srctxt, pos, cnt) { if (null==cnt) cnt=-1; // default value if (cnt<0) cnt = srctxt.length; return srctxt.substr(pos-1, cnt); } function Len(srctxt) { return srctxt.length; } function Cstr(i) { return i;} function Int(i) { return Math.floor(i);} function Mid(srctxt, pos, cnt) { return StrMid(srctxt, pos, cnt); } function Left(srctxt, cnt) { return StrLeft(srctxt, cnt); } function StrLeft(srctxt, cnt) { return srctxt.substr( 0, cnt); } //====================================== // Shuffle the chars in Sstr per Nval function NShuffle(Nval, Sstr){ var HA; keyval = Nval; NAS = Sstr //Working AlphaNumericStr; HA = ""; ll = Len(Sstr); for (var i = 1; i <= Len(Sstr); i++) { Cut = (keyval % ll) + 1; HA = HA + Mid(NAS, Cut, 1); NAS = Mid(NAS, Cut + 1) + Left(NAS, Cut - 1); ll = ll - 1; } return HA; } // Put the Nval into the radix of Sradix (the chareg. "9876543210") function NSradix(Nval, Sradix){ Wval = Nval; base = Len(Sradix); Results = ""; while (Wval>0) { d = (Wval % base); Wval = Int(Wval/base); Results = Mid(Sradix, d+1, 1) + Results; } return (Results); } // Put the Nval into the radix of Sradix function NSradix425(Nval, Sradix){ Wval = Nval; base = 89; Results = ""; for (var i=0;i<5;i++) { d = (Wval % base); Wval = Int(Wval/base); Results = Mid(Sradix, d+1, 1) + Results; } return (Results); } // Reverve the NSradix(Nval, Sradix) process function deNSradix(str89, Sradix){ Nval = 0; base = Len(Sradix); while (str89) { //v = str89.charCodeAt(0); //alert("v: "+v); // need to find its pos in Sradix v = Sradix.indexOf(str89.charAt(0),0); //alert("v: "+v); Nval = base * Nval + v; str89 = Mid(str89, 2); } return (Nval); } function deNSradix425(str89, Sradix){ Nval = 0; base = 89; while (str89) { //v = str89.charCodeAt(0); //alert("v: "+v); // need to find its pos in Sradix v = Sradix.indexOf(str89.charAt(0),0); //alert("v: "+v); Nval = base * Nval + v; str89 = Mid(str89, 2); } return (Nval); }
|
Site Home
|
msg: Site Administrator
|
RonMiller
©1999-2021
From:
Include an email addr if a response is desired.