Simple encryption

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Weasel5-12
Forum Commoner
Posts: 37
Joined: Tue Sep 16, 2008 6:58 am

Simple encryption

Post by Weasel5-12 »

I'm trying to do a simple data encryption for passwords. Upon which the encryption formula will become increasingly complex.

The following code words, up until i try to decrypt the password.

All help is MUCH appreciated

Code: Select all

<?php
    $password = "admin";
    $length = strlen($password);
    $array = str_split($password);
    
    for($i = 0; $i < $length; $i++){    
        $tempVal = $array[$i];
        $tempVal++;
        $array[$i] = $tempVal;
    }
    // Outputs admin as benjo
    
    echo "<br>Break <br>";
    
    for($i = 0; $i < $length; $i++){    
        $tempVal = $array[$i];
        $tempVal--;
        $array[$i] = $tempVal;  
    }
    // Outputs benjo as admin
?>
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Simple encryption

Post by papa »

Not good practice. Check out md5() and sha1() for example.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Simple encryption

Post by Mark Baker »

I wouldn't recommend using a Caesar cipher for passwords

But you're trying to increment and decrement character values using ++ and --. While this works for incrementing, you'll find that the PHP documentation specifically says that it doesn't work for decrementing.

To do the decrement, you have to convert the characters to a numeric value, then decrement, then convert them back to a character using the ord() and chr() functions.

Code: Select all

 
    for($i = 0; $i < $length; $i++){
        $tempVal = $array[$i];
        $tempVal = chr(ord($tempVal) -1);
        $array[$i] = $tempVal;
    }
 
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Simple encryption

Post by greyhoundcode »

If you want to keep it really simple, the str_rot13() function encodes as well as decodes, though it's obviously not encryption as such.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Simple encryption

Post by Mark Baker »

greyhoundcode wrote:If you want to keep it really simple, the str_rot13() function encodes as well as decodes, though it's obviously not encryption as such.
Pity str_rot13() will only rotate 13 characters... it might be more useful if it allowed a second parameter identifying how many steps to shift each character by
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Simple encryption

Post by greyhoundcode »

... it might be more useful if it allowed a second parameter identifying how many steps to shift each character by
You know I think I saw a function on one of these boards that does just that, though I can't seem to find it at the moment.
Post Reply