Password Code

Uhhuh, here we go from http://www.phpfreaks.com/forums/index.php?topic=259939.0 :
sha1 is not an encryption algorithm, so it is not possible to decrypt it. As seventheyejosh just stated, it is a hash (checksum.) And it is one-way and cannot be undone.

Ok well here is a function that can encrypt/decrypt codes with a secret key:

<?php // String EnCrypt + DeCrypt function // Author: halojoy, July 2006 function convert($str,$ky=''){ if($ky=='')return $str; $ky=str_replace(chr(32),'',$ky); if(strlen($ky)<8)exit('key error'); $kl=strlen($ky)<32?strlen($ky):32; $k=array();for($i=0;$i<$kl;$i++){ $k[$i]=ord($ky{$i})&0x1F;} $j=0;for($i=0;$i

Just call newkey() every time a new user is made. When a guy logs in, it will try decrypting his/her password by the key kept in the same row. When it finds it and it matches the username, boom! Done. But I guess you just can’t rely on sha1 or md5 to create a code for you.

Thank you so much for that :slight_smile:

2 questions tho. Firstly do i save this code and an individual php file i.e. new key.php and then just use the include function to run it on my registration/submit page? If so where do I insert this?

Secondly, I understand what you are saying about sha1 and md5 being one way, but i’m not trying to decrypt the code. when someone logs in it encrypts there code and matches it to the same code that has already been encrypted in the database. I hope that makes sense?

Sam

I guess I could try that. In the code above, it doesn’t matter if you put it in a seperate php file, as long as you require in fact, it is probably a good idea. Here’s how it works:
When a new user comes, call newkey();
When you try to make the code, save the $key variable because it contains the key and use convert(“The string you convert goes here”, $key); this will encrypt. To decrypt, you will still need the secret key: convert(“This works both ways”, $key) will change it back, as long as you still have the same key.

Well, I believe I never deleted the php files, I’ll have a look.

Sponsor our Newsletter | Privacy Policy | Terms of Service