/    Sign up×
Community /Pin to ProfileBookmark

two-way string encrypt/decrypt in php

Hello

I am looking for a way to encript and decript strings in php.
does anyone know of a function that does that?

I would like to use the encript in order to store an encripted password in the database.
I will use the decript to obtain the original password so it could be mail to the user (in case he/she forgot it).

At first I used hashing to encript the password, but I am not able to re-create the original password from the hash… I need another solution.

I will have to use a key for the encription, but I don’t need a private/public key pair. I need a single secret key which I will keep to myself.

thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@solavarApr 21.2004 — have a look at the [B]mcrypt()[/B]
Copy linkTweet thisAlerts:
@crh3675Apr 21.2004 — If you don't have mcrtypt installed, you can always use RC4 encryption


class.rc4crypt.php
<i>
</i>&lt;?

class rc4{

<i> </i>function rc4(){
<i> </i>}

<i> </i>function encrypt($pwd="",$data,$case=""){
<i> </i> $data=$case == 'de' ? $data = urldecode($data) : $data;
<i> </i> $v[] = "";$b[] = "";$s = "";$t = ""; $k = "";$f = "";$z = "";$l = 0;$x = 0;$a = 0;$j = 0;$l = strlen($pwd);$d=strlen($data);
<i> </i> for ($i = 0; $i &lt;= 255; $i++){$v[$i] = ord(substr($pwd, ($i % $l), 1)); $b[$i] = $i;}
<i> </i> for ($i = 0; $i &lt;= 255; $i++){$x = ($x + $b[$i] + $v[$i]) % 256; $s = $b[$i]; $b[$i] = $b[$x]; $b[$x] = $s; }
<i> </i> for ($i = 0; $i &lt; $d; $i++) {$a = ($a + 1) % 256; $j = ($j + $b[$a]) % 256;$t = $b[$a]; $b[$a] = $b[$j];$b[$j] = $t;$k = $b[(($b[$a] + $b[$j]) % 256)]; $f = ord(substr($data, $i, 1)) ^ $k;$z .= chr($f);}
<i> </i> $z=$case =="de" ? urldecode(urlencode($z)): $z;
<i> </i> return $z;
<i> </i>}

<i> </i>function decrypt($pwd,$data){
<i> </i> return $this-&gt;encrypt($pwd,urlencode($data),"de");
<i> </i>}

}

?&gt;


rc4test.php
<i>
</i>&lt;?php

include("class.rc4crypt.php");

$enc=new rc4();

$pass="mypassword";
$mystring="this is the string I am encrypting";

print "From:&lt;br&gt;";

print $mystring;

print "&lt;br&gt;&lt;br&gt;";

$encrypted_string=$enc-&gt;encrypt($mypassword,$mystring);

print "To:&lt;br&gt;";

print $encrypted_string;

print "&lt;br&gt;&lt;br&gt;";

$decrypted_string=$enc-&gt;decrypt($mypassword,$encrypted_string);

print "And Back:&lt;br&gt;";

print $decrypted_string;
?&gt;
Copy linkTweet thisAlerts:
@solavarApr 21.2004 — [i]Originally posted by crh3675 [/i]

[B]If you don't have mcrtypt installed, you can always use RC4 encryption





[/code] [/B]
[/QUOTE]



...thanks, been looking for something like this
×

Success!

Help @davidklonski spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...