A simple keyword cipher in javascript

I’m reading (slowly) the book CODES, CIPHERS, SECRETS AND CRYPTIC COMMUNICATION by Fred B. Wrixon.

The first example of a cipher is a simple letter substitution – the alphabet is basically shifted.

So I’ve written a letter substitution function that requires two letters and the phrase to be encrypted.

var keywordCipher = function(char1, char2, plaintext) {
  console.log("got ", char1, char2, plaintext);
  var comp1 = "abcdefghijklmnopqrstuvwxyz";
  var diff = comp1.indexOf(char2) - comp1.indexOf(char1);
  var msg = {
    value: plaintext,
    error: 0,
    message: "diff: " + diff
  var trans = "";
  for(var i =0; i < plaintext.length; i++) {
    var plainChar = plaintext.charAt(i);
    if(plainChar == " ") {
      trans = trans.concat(" ");
    } else {
      var transCharPos = (comp1.indexOf(plainChar) + diff)%comp1.length;
      var transChar = comp1.charAt(transCharPos);
      trans = trans.concat(transChar);
  msg.value = trans;
  return msg;

You can see this in action here.

Posted: March 22nd, 2017 | Author: | Filed under: Code Reference | No Comments »

Flash ActionScript 2 PrintJob function example

This ActionScript 2 code demonstrates how to use the PrintJob() function to generate a print job from JPG images in a subfolder.

In summary, what occurs is that all the images are imported/created as child movie clips of the parent movie clip, printZone, and then all the child movie clips are passed to the PrintJob object, pj.

The biggest problem with this code is that for some reason the print dialog box does not give you the option to print selected pages.

var printPath:String = "pages/"; // The is relative path to the subfolder containing the images.
var printLabel:String = "page-"; // For some reason I decided to use "page-" as the prefix for all the image files.
var printType:String = ".jpg"; // This is the file type for the images. You can use SWF files as well as PNGs instead, if you like.
var numPages:Number = 32; // The number of pages is hard coded but, given a chance to do it again, I'd probably go with an XML file definition.
var printXMin:Number = 0; // This variable and the next define the position on the page for the image. In this case, the upper left-hand corner is set to zero.
var printYMin:Number = 0;
var printXMax:Number = 1275; // This variable and the next define the number of pixels to force into the page from the source image.
var printYMax:Number = 1650; // And I set these variable to match the actual sizes of the JPG images so it would print the entire JPG image.
var printXScale:Number = 50; // An actual 8 1/2 X 11 sheet of paper is about 637.5 X 825 screen pixels and since the images are so much larger
var printYScale:Number = 50; // I had to scale them down 50% to fit on the page.

printCatalog = function() { // This function is called when some print button movie clip is released.
  loadingIndicator._visible = true; // The loadingIndicator movie clip nests a progressBar movie clip that is used to feedback progress for processes in general.
  var printLoader:MovieClipLoader = new MovieClipLoader(); // This is the MovieClipLoader object that manages to import the JPGs to the printZone movie clip.
  var printLoaderListener:Object = new Object(); // This object hosts the functions that are called for the MovieClipLoader object.
  var pageCount:Number = 1; // This variable keeps track of what page is being processed. 


  printLoaderListener.onLoadProgress = function (target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {// This function updates the loadingIndicator to show progress of loading the individual pages.
    loadingIndicator.progressBar._width = Math.round(bytesLoaded/bytesTotal*100);

  printLoaderListener.onLoadInit = function (mc:MovieClip) {//This function is called when the image is done loading.
    pageCount++; //When the image is loaded then the pageCounter is incremented. This could probably be put after the if-statement and the (numPages+1) term in the if-statement could be simply numPages.
    if (pageCount==(numPages+1)) {
      printDocument(); // If this is the last page then go ahead and print the document.
    } else {
      printNextPage(); // Only when the image is loaded do we proceed to load the next page image.    }

  printDocument = function () {//This function simply takes the entire array of movie clip children from printZone and adds them as pages to the PrintJob() object, pj.
    var pj = new PrintJob();
    if(pj.start()) {
      var i = new Number();
      for (i=1;i<=numPages;i++) {
        printZone["page"+i]._xscale = printXScale;
        printZone["page"+i]._yscale = printYScale;
        pj.addPage(eval("printZone.page"+i), {xMin:printXMin,xMax:printXMax,yMin:printYMin,yMax:printYMax},{printAsBitmap:true}, 1);
        loadingIndicator.progressBar._width = Math.round(i/numPages*100);//Here the progress of the pages adds is reflected in the loadingIndicator movie clip.
      loadingIndicator._visible = false;
      delete pj;

  printNextPage = function () { //This function parses the file name of the page and then
    var pageString = new String();
    if (pageCount<10) {// For whatever reason, I formatted the files to be "page-001.jpg, page-002.jpg, ... , page-010.jpg, etc,..." This if-then statement formats the page element to match.
      pageString = "00"+pageCount.toString();
    } else {
      pageString = "0"+pageCount.toString();
    var fileName:String = printPath+printLabel+pageString+printType;
    printZone.createEmptyMovieClip("page"+pageCount, pageCount);
    printLoader.loadClip(fileName, eval("printZone.page"+pageCount));// Here the image is called to load.

  printNextPage(); // So here's where we start by loading the first page.

This swath of code is fairly down-and-dirty. It could probably stand to have some error handlers. Also, a lot of this information could be offloaded to an XML file and make it more flexible.


Posted: July 14th, 2009 | Author: | Filed under: Code Reference | Tags: , | 1 Comment »

Action Script 2.0 for playing a loop of a series of SWF files defined in an array.

So, yeah. Say you want to create a SWF that imports a series of SWFs and loops them. Well, here’s one way to do this.

// Action Script 2.0 for playing a loop of a series of SWF files defined in an array.
// Completion of the individual SWFs is defined as the event when the current frame
// is equal to the total number of frames.

fscommand("showmenu", "false");

// You'll need a movie clip on the stage named movies_mc to hang the SWFs from. If you decide not to have one you can uncomment the following line to generate one.
//this.createEmptyMovieClip("movies_mc", 1);
var main_mc:MovieClipLoader = new MovieClipLoader();

var mcListener:Object = new Object();

var movieTitle:Array = ["movie1.swf", "movie2.swf", "movie3.swf", "movie4.swf", "movie5.swf"];
var i:Number = 0;
var ii:Number = 0;

mcListener.onLoadInit = function(target_mc:MovieClip) {
    target_mc.onEnterFrame = function() {
        if (this._currentframe >= this._totalframes) {
            trace("total frames = " + this._totalframes);

            loadNextMovie(movieTitle[modFunction(i, movieTitle.length)]);
            delete this.onEnterFrame;
        } else {
            trace(target_mc._currentframe+" OF "+target_mc._totalframes);

function loadNextMovie(moviePath) {
    if (i>0){main_mc.unloadClip(movies_mc);}
    trace("loading movie:" + moviePath + ", i="+i);
    main_mc.loadClip(moviePath, movies_mc);

function modFunction(n:Number,b:Number):Number {
  //****** MOD FUNCTION
  // n = number to apply the mod function to. b = base of for the mod function
  var result:Number = new Number();
  result = n - b*(Math.floor(n/b));
  return result;


This script should be loaded into the first frame of a single frame FLA.

Posted: May 25th, 2009 | Author: | Filed under: Code Reference | Tags: , | No Comments »

In ActionScript 3 the TextArea object CSS attribute can be fussy

You can get different results from the same SWF if it’s executed locally versus a web server.

Suppose you have a TextArea object called “mytext” and you want this object to use css.

code 1

mytext.styleSheet = css;

code 2

mytext.styleSheet = css;

code 1 will render the TextArea object just fine on the local system but will not correctly render the css (specifically the line-height) from a web server.

code 2, however, will render the css correctly in both cases.

Posted: April 30th, 2009 | Author: | Filed under: Code Reference | Tags: | No Comments »

A stand alone flash player

Here I need to make a flash stand-alone player that will play on PC and Mac. It will import a .swf that itself nests a .flv.

package {
  import flash.display.*;
  import flash.net.*;
  import flash.system.fscommand;
  public class basic_standalone extends MovieClip {
    public function basic_standalone () {
      var request:URLRequest = new URLRequest("themovie.swf");
      var loader:Loader = new Loader();
      //This next line prevents the viewer from rescaling the content but in Mac
      //you can still scale the .app window.
      stage.scaleMode = StageScaleMode.NO_SCALE

I had to add a mask the size of the stage and an empty sublayer to prevent assets being viewed outside the stage area if the user decided to scale the viewing window. I’d like to prevent the viewer from resizing the window but outside of using Director I haven’t found a good way to do it yet.



Posted: June 6th, 2008 | Author: | Filed under: Code Reference | No Comments »

FLV’s don’t always work with relative pathing in XP browsers.

Say you want to create a Flash .swf that references a .flv with relative path. What you may discover, if you’re trying to view the .swf on a PC/Windows XP, is that the .flv does not play – and this is the case with Firefox, IE6, IE7, and Safari. But you may discover that it will work on Mac with Safari and Firefox! Also, you may discover that it will work on Vista on the same browsers!

So, the flash with .flv doesn’t work on PC XP with browsers Firefox, IE6, IE7, or Safari. But it does work on Mac Firefox and Safari as well as Vista Firefox, IE7, and Safari.

Why is this? I don’t know. But it seems that the problem is that the files reside locally. A work-around that I used was to post the files on a web server. I also discovered that it will work if accessed through FTP.


Posted: June 4th, 2008 | Author: | Filed under: Code Reference | No Comments »

A simple MOD function for Flash

private function modfunction(n:Number,b:Number):Number {
  //****** MOD FUNCTION
  // n = number to apply the mod function to. b = base of for the mod function
  var result:Number = new Number();
  result = n - b*(Math.floor(n/b));
  return result;

Posted: June 2nd, 2008 | Author: | Filed under: Code Reference | No Comments »

Creating a text field in actionscript 3

So the Flash developers said, “Hey! Dragging and dropping text is waay too easy. Can’t we just make it harder?”

So they devised a really hard way to do it. As follows:

package {
  import flash.display.Sprite;
  import flash.text.TextField;

  public class TextField_condenseWhite extends Sprite {
    public function TextField_condenseWhite() {
      var tf1:TextField = createTextField(0, 0, 200, 50);
      tf1.condenseWhite = false;
      tf1.htmlText = "keep    on\n\ttruckin'";

      var tf2:TextField = createTextField(0, 120, 200, 50);
      tf2.condenseWhite = true;
      tf2.htmlText = "keep    on\n\ttruckin'";

    private function createTextField(x:Number, y:Number, width:Number, height:Number):TextField {
      var result:TextField = new TextField();
      result.x = x;
      result.y = y;
      result.width = width;
      result.height = height;
      result.border = true;
      return result;



Posted: May 30th, 2008 | Author: | Filed under: Code Reference | No Comments »

HTML Javascript popup window

<SCRIPT TYPE="text/javascript">
  function popup(mylink, windowname) {
    if (! window.focus)return true;
    var href;
    if (typeof(mylink) == 'string'){
      window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
      return false;

<A HREF="popupbasic.html" onClick="return popup(this, 'notes')">my popup</A>



Posted: May 30th, 2008 | Author: | Filed under: Code Reference | No Comments »