Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Silly "encryption".....just for fun ;)

by Guitarded (Novice)
on Jul 29, 2002 at 10:03 UTC ( #185930=CUFP: print w/ replies, xml ) Need Help??

'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";

Comment on Silly "encryption".....just for fun ;)
Download Code
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 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...
(RhetTbull) Re: Silly "encryption".....just for fun ;)
by RhetTbull (Curate) on Jul 30, 2002 at 02:01 UTC
    Just say no to lame encryption! While useful as a learning exercise, posting weak encryption code in a public forum like this just invites some poor newbie to come along and use it. Yes, even though you tell us it's lame I bet someday someone will come and cut and paste your code. I have seen a seemingly competent programmer do that with an XOR snippet he found to send "encrypted" credit card data over email. If you don't really understand encryption and aren't using a well validated library, then you shouldn't be using encryption at all.

    Practice writing this stuff at home but please don't share with everyone.

      Really? Don't share it with anyone? It could be damaging? Perhaps I could email you a Kleenex?

      I think if anyone were to paste that out and try to use it for "encrypting" credit card data, they'd be wasting their time twice, not to mention that would fall under the category of NMFP.

      I'll take the chance that some knob will mistake weak encryption for strong encryption.

      Here, maybe this will make you feel better:

      use Digest::Perl::MD5; my $keystring="Enter a string here"; my @stack; push(@stack, $blahblahwhatever); my $digest=new Digest::Perl::MD5; $digest->add(@stack); my $serial=$digest->hexdigest();


      If that doesn't help, maybe you should write your congressman. ;) Or teach me a thing or two about encryption. Meanwhile, I'll continue to post whatever I please as long as smart cats like merlyn want to participate in the discussion. I think you're looking for www.pissandmoan.com.....You've got the wrong number. Have a bitchin day.
        Grow up.
        In my post, I didn't attack you, I didn't say you were stupid, I didn't say you shouldn't participate in the monastery. I didn't say you should be censored (as you imply). Heck, I didn't even downvote your node. I did give my opinion why I thought you shouldn't post such code here. If you are learning perl and have a question about your code, then it would have been (in my opinion) perfectly acceptable for you to post your snippet in the SOPW. But you didn't, you posted in in Cool Uses for Perl when in fact your code wasn't a cool use for perl (again, my perfectly valid opinion). Posting here thus implies that this is cool code that others might find interesting or useful. Having seen others make mistakes in the past by making such assumptions about such poor "encryption" I gave a warning. I was trying to help out you and others who might come later. I'm sorry if you got your feelings hurt.

        Also, your comment of "not to mention that would fall under the category of NMFP" shows a genuine lack of maturity. Perhaps you could learn a thing or two about interacting with others in a nice way by watching the posts of, as you put it, the "smart cats like merlyn." As a member of this fine monastery, I do care about the other users. All the code I post *IS* MFP. If I post code that I think is dangerous or bad, I'll tell people. It's called responsibility.

      If you don't really understand encryption and aren't using a well validated library, then you shouldn't be using encryption at all.

      i could not disagree more. IMHO, the less you know about encryption, the more you should use it -- how else is one to learn about it and how it works? don't get me wrong - i'm all for using those libraries that work, rather than reinventing the wheel. but if you learn better by breaking things and/or tinkering than by example, then by all means use what you can get your hands on.

      and frankly, if someone can't read past the disclaimers that the author placed, they deserve what falls on their heads. (yes, i'm a firm believer in Darwinism.)

      strfry()
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://185930]
Approved by djantzen
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-10-25 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls