1 Reply Latest reply on Jan 15, 2010 8:34 AM by Gregory Lafrance

    Need help with decryption program; for loops etc..

    MudkipsAreCool

      Currently I am working on a simple encryption program, which changes inputted characters to ASCII codes. One key simply adds a user inputted number to the ascii codes, while the second key changes the spaces to the number of letters in the preceding word and then converts to ascii code and flips the whole string around. I have one function, Spacer, which will change a string (i.E Hello World!) To (Hello5World!). The output of spacer is then sent to ComplexEncrypt, which returns ascii codes of the characters in reverse order. Currently I am having trouble decrypting.

       

      For example, the User Encrypt function with an input of "Hey dude!" and a user-inputted shift of 5 will return this:

       

      77 106 126 37 105 122 105 106 38
      End

       

      What I need to know is how to get a for loop to look at each group of digits (each two or three digit number) so that I can preform a -5 on them and return

      a String.fromCharCode so that the string of numbers will decrypt back to the original string.

       

      Here is my code so far, the spacing and layout is off but I'm sure it is still possible to understand.

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

       

      <mx:Script>

      <![CDATA[

      public

       

       

       

       

       

      var CH:String;

       

      //This function encrypts the text according to the selected method, converting the characters to ascii code and

       

       

      // then incrementing said code by whatever number user has inputted into the numeric stepper

       

       

      public function UserEncrypt(Data:String, Shift:int):String{

       

      var Output:String="";

       

      for (var i:int=0; i< Data.length; ++i){

      Output+=((Data.charCodeAt(i))+ Shift)+

      " ";

      }

      Output+=

      "\n";

      Output+=

      "End";

       

      return (Output)

      }

       

      // The Spacer function takes in a string and replaces the spaces with the number of letters in the

       

       

      // preceding word.

       

       

      public function Spacer(Data:String):String{

       

      // variables for output and counting the characters.

       

       

      var Output:String="";

       

      var WordLength:int=0;

       

      for (var p:int=0; p<Data.length; ++p){

       

      if (Data.charCodeAt(p)!=32){

      Output+=(Data.charAt(p))+

      "";

      WordLength+=1;

      }

       

      if (Data.charCodeAt(p)==32){

      Output+=((WordLength)+

      "");

      WordLength=0;

      }

      }

       

      return (Output);

      WordLength=0;

      }

       

      // this function starts at the end of a string and goes to the beginning, returning ascii codes along the way

       

       

      // and effectively reversing the string.

       

       

      public function ComplexEncrypt(Data:String, Shift:int):String{

       

      var Output:String="";

       

      for (var u:int=((Data.length)-1); u >= 0; --u){

      Output+=Data.charCodeAt(u

      " ";

      }

      Output+=

      "\n";

      Output+=

      "End";

       

      return (Output);

      }

       

      ]]>

      </mx:Script>

       

      <mx:Panel x="12" y="14" width="889" height="469" layout="absolute">

       

      <mx:TextInput x="34" y="22" id="StringIn" width="229" height="53"/>

       

      <mx:Button click="NumberOut.text=UserEncrypt(StringIn.text, ShiftStepper.value)" x="18" y="214" label="User Encrypt" id="User"/>

       

      <mx:Button click="NumberOut.text=ComplexEncrypt(Spacer(StringIn.text), ShiftStepper.value)" x="127" y="214" label="Complex Encrypt" id="Complex"/>

       

      <mx:Button click="NumberOut.text=ComplexEncrypt(NumberOut.text, ShiftStepper.value)" x="265" y="214" label="Cipher Encrypt" id="Cipher"/>

       

      <mx:TextArea x="301" y="21" id="NumberOut" width="269" height="121"/>

       

      <mx:NumericStepper visible="true" x="172" y="118" id="ShiftStepper" minimum="-20"

      maximum="

      20"/>

       

      <mx:Label visible="true" x="35" y="120" text="Enter Amount to Shift" id="ShiftLabel" />

       

      </mx:Panel>

      </mx:Application>

       

      Thanks in advance for your help!!!

        • 1. Re: Need help with decryption program; for loops etc..
          Gregory Lafrance Level 6

          This code solves your problem:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="changeFunc();">
            <mx:Script>
              <![CDATA[
                private function changeFunc():void{
                  var result:String = strTxt.text;
                  var arr:Array = result.split(" ");
                  for(var a:uint=0;a<arr.length;a++){
                    arr[a] = Number(arr[a]) + Number(inTxt.text);
                  }
                  result = arr.join(" ");
                  outTxt.text = result;
                }
              ]]>
            </mx:Script>
            <mx:Form>
              <mx:FormItem label="Num:">
                <mx:TextInput id="inTxt" change="changeFunc();" 
                  width="30" text="5"/>      
              </mx:FormItem>
              <mx:FormItem label="Before:">
                <mx:TextInput id="strTxt" editable="false" 
                  text="77 106 126 37 105 122 105 106 38"
                  width="300"/>
              </mx:FormItem>
              <mx:FormItem label="After:">
                <mx:TextInput id="outTxt" editable="false"
                  width="300"/>
              </mx:FormItem>
            </mx:Form>
          </mx:Application>
          


          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services