reversible crypt?

I'm looking for a method of very simple encryption:

1) It must be able to encrypt a whole text file
2) The file must be decryptable using the same key
3) Security is not a big concern, mainly I just want the files to not be readable at first glance
3) I don't want to install any modules, .pm files, etc.

The reasoning behind this is that I have some files that I access with a script over the web, but the server they're stored on is a friend's, not mine.  I don't want him reading the files, but he's not the kind who would spend a lot of time trying to crack codes, so something very simple would do.  Any ideas?
oucherAsked:
Who is Participating?
 
prakashk021799Connect With a Mentor Commented:
Char to numeric ASCII value:
    Use the 'ord' function. For example:

    print ord('A');   # prints 65

Numeric ASCII value to Char:
    Use the 'chr' function. For example:

    print chr(65);    # prints the character A

Actually, if you want to encode non-alphabetic characters, the code above needs to be changed. Also, the same code will not work for decoding it. Rot13 works for alphabetic characters for both encoding and decoding because there are 26 letters in the alphabet. So, if you rotate a character by 13 once and then repeat the rotation, you will get the same character. But when you add numeric characters the same thing does not hold. That's why the script in my previous comment does not encode non-alphabetic characters.

If you want encode all characters, you will need to use two separate scripts for encoding and decoding. In the following example, 13 is just an arbitrary number. You can choose any number in place of it.

encode script:

map {$rotmap{chr($_)} = chr(($_ + 13) % 256)} (0..255);
while (<>) {
    s/(.)/$rotmap{$1}/g;
    print;
}

decode script:

map {$unrotmap{chr($_)} = chr(($_ + 256 - 13) % 256)} (0..255);
while (<>) {
    s/(.)/$unrotmap{$1}/g;
    print;
}
0
 
samayCommented:
replace characters by certain others..
and put it back..
a->b
b->b
c->d
...

z->a

0
 
prakashk021799Commented:
Here's a simple rot13'er:

while (<>) {
    tr/[a-zA-Z]/[n-za-mN-ZA-M]/;
    print;
}

The same code will encrypt as well as decrypt the text. Of course, it handles only alphabetic characters. If you want it to handle numeric and other special characters, you need to extend the patterns above.
0
 
oucherAuthor Commented:
Thanks prakashk, that's the kind of thing I'm looking for.  For adding numeric and symbolic characters, is there some easy way to add or subtract do it using ASCII numbers?  Is there a Perl function that will convert a number to a char and vice versa?
0
 
oucherAuthor Commented:
Thanks a lot!
0
All Courses

From novice to tech pro — start learning today.