1 Reply Latest reply on Jan 16, 2010 7:51 AM by MudkipsAreCool

    Help with encrypting a vigneri(sp?) cipher.


      Currently I am building a function that takes in a string and codeword and outputs a string of numbers seperated by spaces. What the function does is adds the ascii codes of the corresponding characters together. Example, input string is "Hello my name is Pascal" and the code word is "apple".


      The function would do this


      H e l l o   m y   n a m e   i s   P a s c a l

      a p p l e a p p l e a p p l e a p p l e a p p


      The function returns an output variable, which would contain something like


      ### ### ### ## ### ### ###


      The first ### is the sum of the ascii codes of H and a, the second ### is the sum of the ascii codes of e and p, so on.


      I have been having some problems lately however. For example, putting in a string of "Five times three is fifteen" with "Hello" as the codeword yields this:


      142 206 226 209 173 104 217 213 217 181 173 216 140 224 188 176 215 209 209 173 104 206 223 140 104 174 206 210 224 188 173 202 218


      I know this is wrong right off the bat because the input string has 27 characters and the encrypted code has 33 "characters" of three numbers. There is also that duplicate 209 which shouldn't be in there.


      Anyways, here is my code for that function. Tabbing is off because of control paste but it should be easy enough to understand.


      CodeLetter is just a variable that counts which character in the codeword the loop is at and resets to 0 (the first char of the codeword) is it gets equal to the codeword length. Because if the codeword has 5 characters, it will have a length of 5, but the codeletter will need to reset at 4 (01234).


      Is there anything obvious that I am missing that needs to be changed so that this function runs perfectly? Thanks in advance. If more code is needed to solve my problem simply ask and I will provide it. Again, thank you!


      // function for the vigneri cipher. Code is the codeword, Data is the message to be encrypted.




      public function CipherEncrypt(Code:String, Data:String):String{


      var Output:String="";


      var CodeLetter:int=0;


      // for y, which starts at zero, loops until y>= Data.length. Increment y by 1 each loop.



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


      // if the codeletter variable is less than the codeword length, add (to the end of output) the sum



      // of the ASCII codes of the corresponding characters in the code word and the message.



      if (CodeLetter <= (Code.length-1)){


      " ";




      // if the codeletter variable is equal to or greater than the codeword length, reset codeletter to zero



      // and repeat what was done above.



      if (CodeLetter >= (Code.length-1)){



      " ";




      /* For example, if the codeword was Apple and the message was "I like cheese!". The function would return

      the sum of the ASCII codes of: (A+I) (p+space) (p+l) (p+i) (l+k) (e+e). At this point, codeletter would reset and

      codeword would star again from the beginning, further returning: (A+space) (p+c) (p+h). And so on. */



      return (Output);