'lamencrypt.pl'

This little script simulates a sort of weak encryption that I saw used by an old Win95 app to convert Registration info to garbage and back again. It stored the Company Name and address, etc., in Access database tables by the following method: The ASCII decimal value of each character of each string for a given field would be incremented by the place value from the left, starting with 1. For example, "asdf" would become "bugj". Anyway, I thought a worthy beginner's excercise in Perl might be to create a script that does the conversion. So here it is...
#!/usr/bin/perl # -u option performs the conversion in reverse if (@ARGV && $ARGV[0] ne "-u") { print "\nInvalid option.\n"; die "usage: lamencrypt.pl [option] (-u decrypts)\n"; } print "\nEnter string to translate: "; chomp(my $input_string = <STDIN>); my @user_input = split //, $input_string; my $element = 0; my $output_string; foreach $user_input (@user_input){ my $value = ord($user_input[$element]); my $uncounter = $element; while ($uncounter >= 0) { if (@ARGV && $ARGV[0] =~ /-u/){ $value--; $uncounter--; } else { $value++; $uncounter--; } } $element++; $output_string .= chr($value); } while (length($output_string) > $element){ chop($output_string); } print "\nLamely encrypted: "; print "$output_string\n";

Replies are listed 'Best First'.
•Re: Silly "encryption".....just for fun ;)
by merlyn (Sage) on Jul 29, 2002 at 13:37 UTC
      Roger that Vector, Victor... :) I now have:
      if (@ARGV && $ARGV[0] ne "-u") { print "\nInvalid option.\n"; die "usage: lamencrypt.pl [option] (-u decrypts)\n"; } print "\nEnter string to translate: "; chomp(my $x = <STDIN>); my @output = foo($x); sub foo { my $x = shift; if (@ARGV && @ARGV[0] =~ /-u/) { vec($x, $_ - 1, 8) -= $_ for 1..length($x); } else { vec($x, $_ - 1, 8) += $_ for 1..length($x); } return $x; } print "\nLamely encrypted: "; print "@output\n";

      BTW, I tried to use the ternary operator ?: , as in:
      @ARGV && @ARGV[0] =~ /-u/ ? vec($x, $_ -1, 8) -= $_ for 1..length($x) +: vec($x, $_ -1, 8) += $_ for 1..length($x);

      ...but it aborted due to a syntax error near "$_ for". I tried declaring local $_, because I thought perhaps $_ was being clobbered, but it didn't help. Can anyone tell me why that didn't work?
        Can anyone tell me why that didn't work?
        Because "EXPR for EXPR" is not an EXPR, but a STATEMENT. And question-colon selects EXPRs, not STATEMENTs.

        That's also why "EXPR for EXPR for EXPR" doesn't work.

        -- Randal L. Schwartz, Perl hacker

        You could do:
        my $flg = (@ARGV && @ARGV[0] =~ /-u/)? -1 : 1; vec($x, $_ - 1, 8) += $flg * $_ for 1..length($x);
        Also, as long as you're doing this for education, take a look at Getopt::Std, and that ARGV logic can be simplified further...
Re: Silly "encryption".....just for fun ;)
by Juerd (Abbot) on Jul 29, 2002 at 10:26 UTC

    Another WTDI:

    sub foo { my @copy = @_; for (@copy) { my $counter; s/(.)/chr ++$counter + ord $1/ge; } return @copy; } print foo('asdf');
    The s/// line is the line that does the actual "encryption".

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: Silly "encryption".....just for fun ;)
by Anonymous Monk on Jul 30, 2002 at 12:11 UTC
    The best silly encryption is to add an appropriate legal notice.
A reply falls below the community's threshold of quality. You may see it by logging in.