perl question
Results 1 to 3 of 3

Thread: perl question

  1. #1
    Senior Member
    Join Date
    Apr 2002

    Question perl question

    I just need to add a simple low-grade encryption to my site (both client side and server-side, so I need to "translate" the following into javascript):

     print "enter string to xor: ";
     chomp ($str=<stdin>);
     my $line = '~' x 70;
     print "\n";
     print "$line\n";
     $random = random_string(length($str));
     $crypt = $str ^ $random;
     $restored = $crypt ^ $random;
     print "Message string:  $str\n";
     print "Random string:   $random\n";
     print "Crypted string:  $crypt\n";
     print "Restored string: $restored\n";
     print "\n";
     print "$line\n";
    # a random string of the same length as the
    # message is generated.. thats than
    # XOR'ed, character-by-character, on the
    # message, the result is mixed but when
    # you XOR this mix with the same random string,
    # you get the original message back 
    sub random_string {
     my $ret;
     for (1 .. $_[0]) { $ret .= chr(rand 256) }  
     return $ret;
    Now my question is what are the "^" in the code and how do I do that in javascript?

    If you need to know, I'm trying to set up a high score thing on a few javascript games that I downloaded, and my plan is to encrypt the high score, set it in a cookie (so the cookie can't easily be duplicated), and then retrieve it via this perl script. Hopefully I'll get it to work.

    Well if you have an easier solution to my high score thing, please share the wealth
    Either get busy living or get busy dying.

    -The Sawshank Redemption

  2. #2
    Senior Member roswell1329's Avatar
    Join Date
    Jan 2002
    The "^" operator in perl is the Bitwise exclusive OR operator (XOR). According to Programming Perl:

    Like C, Perl has bitwise AND, OR, and XOR (exclusive OR) operators: &, |, and ^. Note from the table at the start of this section that bitwise-AND has a higher precedence. These operators work differently on numeric values than they do on strings. (This is one of the few places where Perl cares about the difference.) If either operand is a number (or has been used as a number), then both operands are converted to type integer, and the bitwise operation is performed between the two integers. These integers are guaranteed to be at least 32 bits long, but may be 64 bits on some machines. The point is that there's an arbitrary limit imposed by the machine's architecture.

    If both operands are strings (and have not been used as numbers since being set), these operators do bitwise operations between corresponding bis from the two strings. In this case, there's no arbitrary limit, since strings aren't arbitrarily limited in size. If one string is longer than the other, the shorter string is considered to have a sufficient number of 0 bits on the end to make up the difference.
    So that's the formal answer. I'm afraid I can't explain it much further in laymans terms either, because I'm not much of an expert on bitwise operations. However, as an alternative to Javascript, I would think that PHP would be a much better language to be writing your application in. It would be much better equipped to store information like that client-side, and their functions for retrieval are very easy if you are at all familiar with perl. It also has support for the XOR operator. It's just xor in PHP! Sorry I couldn't be much more help for you.
    /* You are not expected to understand this. */

  3. #3
    Junior Member
    Join Date
    Dec 2002

    JavaScript xor Operator

    Although I would definitely agree with roswell1329 that you should find an alternative to JavaScript (such as PHP) for your game, in case you are still interested, the xor operator is the same thing, ^, in JavaScript. Here is a reference for all the JavaScript operators. Hope this helps .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts