6 Replies Latest reply on May 7, 2009 11:20 AM by nate15810

    Code (letters/numbers) generator through flash

    nate15810

      is it possible to script in flash so that you can have smoething like the enigma machine? like have an input text box where you input a letter or words then have another output box where the translated work or letters would come out?  and if so, can anyone provide me with the actionscript?

      or would i have to venture into Dreamweaver?

      I'm running CS3 at school and CS4 at home.  not at all sure how to use Actionscript 3.0, as i have always made movies using 2.0.

      thanks

        • 1. Re: Code (letters/numbers) generator through flash
          kglad Adobe Community Professional & MVP

          you can make an encoder/encrypter but it's not likely to be as sophisticated as an enigma machine.

           

          what's the purpose of this?

          • 2. Re: Code (letters/numbers) generator through flash
            nate15810 Level 1

            it's a project for school...i just want a simple code generator...mor along the lines of enter A and you get Z, enter B and you get Y, enter C->X, and so on but can be changed into symbols and other stuff...and can convery words like for instance CAB->XZY

            • 3. Re: Code (letters/numbers) generator through flash
              Rothrock Level 5

              Pretty much any algorithm that is understood (especially old ones) can be implemented in some fashion using Actionscript. Here is a Vigenère cipher implemented in AS2. What you are asking for is typically called a Caeser Cipher or a simple shift cipher.

               

              http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

              http://en.wikipedia.org/wiki/Caeser_cipher

               

              Anyways you can use the same technique I've shown bellow of stepping through the plainText length and getting each character fromCharCode and then doing things with it. It is much easier if you only use capitals and no spaces or puncutation -- which is pretty common especially for the old school encryptions.

               

              var key:String = "CHICKEN";
              var plainText:String = "ATTACKATDAWN";

              trace(encrypt(plainText, key));

              // Vigenère cipher
              function encrypt(plainText:String, key:String):String
              {
              var cipherText:String = "";
              for (var i = 0; i < plainText.length; i++) {
                cipherText += String.fromCharCode(((plainText.charCodeAt(i) - 65 + key.charCodeAt(i % key.length) - 65) % 26) + 65);
              }
              return cipherText;
              }
              function decrypt(cipherText:String, key:String):String
              {
              var plainText:String = "";
              for (var i = 0; i < cipherText.length; i++) {
                var val:Number=((Number(cipherText.charCodeAt(i) - 65) - Number(key.charCodeAt(i % key.length) - 65)) % 26)
                val = (val>0) ? val : 26 + val;
                plainText += String.fromCharCode( val % 26 + 65);
              }
              return plainText;
              }

               

               

              BTW: How does one attach code to the new forums?

              • 4. Re: Code (letters/numbers) generator through flash
                kglad Adobe Community Professional & MVP

                the only way i found to copy and paste is to toggle html off and use <pre> </pre> tags to surround the pasted text/code.

                • 5. Re: Code (letters/numbers) generator through flash
                  kglad Adobe Community Professional & MVP

                  here's an as3 version that uses the shuffleF function to randomize characters.  (note:  .lt. is < and .gt. is > ):

                   

                   

                  var s:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%^&*()_,./?+-";
                  var a:Array = s.split("");
                  var alphaA:Array = a.slice();
                  shuffleF(a);
                  
                  function shuffleF(a:Array):Array {
                       var n:uint = a.length;
                       var temp:*;
                       while (n .gt. 1) {
                            var k:uint = Math.floor(Math.random()*n);
                            n--;
                            temp = a[n];
                            a[n] = a[k];
                            a[k] = temp;
                       }
                       return a;
                  }
                  
                  function encryptF(s:String):String{
                       var tempA:Array = s.split("");
                       var n:uint = 0;
                       var newA:Array = [];
                       while(n .lt. tempA.length){
                            newA.push(a[alphaA.indexOf(tempA[n])]);
                            n++;
                       }
                       return newA.join("");
                  }
                  function decryptF(s:String):String{
                       var tempA:Array = s.split("");
                       var n:uint = 0;
                       var newA:Array = [];
                       while(n .lt. tempA.length){
                            newA.push(alphaA[a.indexOf(tempA[n])]);
                            n++;
                       }
                       return newA.join("");
                  }
                  • 6. Re: Code (letters/numbers) generator through flash
                    nate15810 Level 1

                    so where do i put this, since AS3 isn't my thing...and how do i get a text box to be able to type into to the the other letters?