1 Reply Latest reply on Jan 23, 2010 1:46 PM by FlexBeginner3

    Encryptor/Decryptor

    FlexBeginner3

      Hi there, I'm currently making a flex application which is supposed to both encrypt and decrypt inputted text, but I've run into an issue. The idea of the encryption is that each space in the text is to be identified by placing the number of letters in the preceding word before the word, removing the space. And then, the word is to be reversed. I have this working, but I cannot decrypt this message properly as I cannot replace the numbers with spaces. Here is my code for anyone to look at. Hopefully someone can help, as it will be greatly appreciated:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
           <mx:Script>
                <![CDATA[
           
           public var Numbers:Array;               
           
           public function Encrypt2(text:String,num:Number):String
           {
                var tempQ:String="";
                for (var q:int=((text.length)-1);q>=0;--q){
                     tempQ+=((text.charCodeAt(q))+num) + " ";     
                }     
                Numbers=tempQ.split(" ", 9999);     
                return(tempQ);     
           }
           
           public function Decrypt2(Data:Array, Shift:int):String
           {
                var tempo:String="";
                for (var q:int=(Numbers.length-2); q>=0; --q)
                {
                     tempo+=String.fromCharCode(Numbers[q]-Shift);
                }
                return (tempo);
           }
           
           public function All():void
           {
                     var BaseText:Array=T1.text.split(' ',20000);
                     var high:String="";
                     for (var j:int=0; j < BaseText.length;j++){
                          BaseText[j]=String(BaseText[j].length)+BaseText[j];
                     }
                     for (var h:int=0; h < BaseText.length;h++){
                          high+=BaseText[h];
                     }
                     T2.text=Encrypt2(high,N1.value);
                     }
           
           public function All2():void     
                {     
                     T3.text=(Decrypt2(T2.text.split(" ",200000),N1.value));
                     
                }
                     
                ]]>
           </mx:Script>
           <mx:Panel 
                x="317.5" 
                y="10" 
                width="445" 
                height="479" 
                layout="absolute" 
                id="P1" 
                title="Encrypter/Decrypter" 
                fontFamily="Arial" 
                fontSize="17" 
                fontWeight="bold">
                
                <!--a numeric stepper which acts as a key-->
                <mx:NumericStepper 
                     x="108" 
                     y="10" 
                     id="N1" 
                     minimum="-255" 
                     maximum="255" 
                     stepSize="1"/>
                
                <!--TextAreas for input and output text-->
                <mx:TextArea 
                     x="109" 
                     y="45" 
                     width="300" 
                     height="92" 
                     id="T1"/>
                <mx:TextArea 
                     x="108.5" 
                     y="185.5" 
                     width="300" 
                     height="92" 
                     id="T2" 
                     editable="false"/>
                <mx:TextArea 
                     x="108.5" 
                     y="326" 
                     width="300" 
                     height="92" 
                     id="T3" 
                     editable="false"/>
                                    
                <!--lables to for user interface-->
                <mx:Label 
                     x="60" 
                     y="12" 
                     text="Key" 
                     fontFamily="Arial" 
                     fontSize="16" 
                     fontWeight="bold"/>
                <mx:Label 
                     x="16" 
                     y="46" 
                     text="Input Text" 
                     fontFamily="Arial" 
                     fontSize="16" 
                     fontWeight="bold"/>
                <mx:Label 
                     x="16.5" 
                     y="186.5" 
                     text="Encrypted" 
                     fontFamily="Arial" 
                     fontSize="16" 
                     fontWeight="bold"/>
                <mx:Label 
                     x="54.5" 
                     y="208.5" 
                     text="Code" 
                     fontFamily="Arial" 
                     fontSize="16" 
                     fontWeight="bold"/>
                <mx:Label 
                     x="16.5" 
                     y="327" 
                     text="Decrypted" 
                     fontFamily="Arial" 
                     fontSize="16" 
                     fontWeight="bold"/>
                <mx:Label 
                     x="62.5" 
                     y="351" 
                     text="Text" 
                     fontFamily="Arial" 
                     fontSize="16" 
                     fontWeight="bold"/>     
                     
                <!--Buttons that trigger decryption and encryption-->
                <mx:Button 
                     x="13.5" 
                     y="148.5" 
                     label="Encrypt" 
                     click="All()"/>
                <mx:Button 
                     x="14.5" 
                     y="289" 
                     label="Decrypt" 
                     click="All2()"/>
                
           </mx:Panel>
           
      </mx:Application>