Jump to content

[SOLVED] Password encrypter/decrypter - a little guidance please


niallniall

Recommended Posts

Hello,

 

A couple of people on here helped me make this yesterday: http://www.onlineworx.net/testing/gen1.php

 

It basically lets my encrypt sensitive information to passed around to my colleagues safely over the internet via email and instant messaging etc. I give trusted partners a copy of this to store locally and they can safely decrypt the information when it arrives.

 

I need the thing to be able to differentiate between capital and non capital letters but I cant figure out how to do it.

 

Here's the code:

 

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Make a password to send:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="encrypt"/>
</form>

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Unscramble password:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="decrypt"/>
<p>


<?php
if ($_POST['submit'] == "encrypt") {
$user_junk = strtolower($_POST['user_junk']);
$coded = "";
for ($i=0;$i<strlen($user_junk);$i++) {
	$char = $user_junk{$i};
	if ($char == "z") $char = "`"; // forced roll z to a
	if ($char == "9") $char = "/"; // forced roll 9 to 0
	$coded.= chr(ord($char) + 1);
}
echo "Encryted Password:<br>".$coded. "<br/><br/>";
}
if ($_POST['submit'] == "decrypt") {
$user_junk = strtolower($_POST['user_junk']);
$coded = "";
for ($i=0;$i<strlen($user_junk);$i++) {
	$char = $user_junk{$i};
	if ($char == "a") $char = "{";  // forced roll z to a
	if ($char == "0") $char = ":";  // forced roll 0 to 9
	$coded.= chr(ord($char) - 1);
}
   echo "Decrypted Password:<br>".stripslashes($coded). "<br/><br/>";
}

?>

 

Here's how I thought I could make it work! :( But alas it doesn't work.

 

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Make a password to send:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="encrypt"/>
</form>

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Unscramble password:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="decrypt"/>
<p>


<?php
if ($_POST['submit'] == "encrypt") {
$user_junk = strtolower($_POST['user_junk']);
$coded = "";
for ($i=0;$i<strlen($user_junk);$i++) {
	$char = $user_junk{$i};
	if ($char == "z") $char = "`"; // forced roll z to a
	if ($char == "9") $char = "/"; // forced roll 9 to 0
	$coded.= chr(ord($char) + 1);
}
echo "Encryted Password:<br>".$coded. "<br/><br/>";
}
if ($_POST['submit'] == "decrypt") {
$user_junk = strtolower($_POST['user_junk']);
$coded = "";
for ($i=0;$i<strlen($user_junk);$i++) {
	$char = $user_junk{$i};
	if ($char == "a") $char = "{";  // forced roll z to a
	if ($char == "0") $char = ":";  // forced roll 0 to 9
	$coded.= chr(ord($char) - 1);
}
   echo "Decrypted Password:<br>".stripslashes($coded). "<br/><br/>";
}

?>

 

 

 

Can one of you please help me get this thing to treat capitals and non capitals seperately.

 

Any help would me much appreciated.

 

Cheers

Link to comment
Share on other sites

First of all, if you really need encrypting / decrypting to transfer sensitive information, don't use so simple encoding-decoding methods... Check mCrypt.

 

Anyway, you are applying strtolower() on the input so it's all going to be small case.

 

I'd do it this way tho:

 

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Make a password to send:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="encrypt"/>
</form>

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Unscramble password:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="decrypt"/>
<p>

<?php

$chars[0] = array_merge(range(65,90), range(97,122), range(48,57));
$chars[1] = array_merge(range(66,90), array(65), range(98,122), array(97), range(49,57), array(48));
array_walk($chars[0], "chr2");
array_walk($chars[0], "chr2");


if ($_POST['submit'] == "encrypt")
{
$coded = str_replace($chars[0], $chars[1], $_POST['user_junk']);
echo "Encryted Password:<br>".$coded. "<br/><br/>";
}
if ($_POST['submit'] == "decrypt")
{
$coded = str_replace($chars[1], $chars[0], $_POST['user_junk']);
echo "Decryted Password:<br>".$coded. "<br/><br/>";
}

function chr2 (&$a)
{
$a = chr($a);
}

?>

 

EDIT- for some reason it doesn't work, I'll check it later tho.

 

Orio.

Link to comment
Share on other sites

lol it was more complicated than I've thought, I guess I was just thinking wrong. And I've learned something about str_replace()...

Anyway, this works:

 

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Make a password to send:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="encrypt"/>
</form>

<form action=" <?=$_SERVER['PHP_SELF']?>" method="post">
Unscramble password:<br />
<textarea name="user_junk" cols="20" rows="1">
</textarea>
<input type="submit" name="submit" value="decrypt"/>
<p>

<?php
if(!isset($_POST['submit']))
die;

$chars = array();
$regular = array_merge(range(65,90), range(97,122), range(48,57));
$temp[0] = array_merge(array(90), range(65,89), array(122), range(97,121), array(48), range(48,56));
$temp[1] = array_merge(range(66,90), array(65), range(98,122), array(97), range(49,57), array(48));
for($i=0; $i<count($temp[0]); $i++)
{
$chars[0][$regular[$i]] = $temp[0][$i];
$chars[1][$regular[$i]] = $temp[1][$i];
}



$coded = "";
$method = ($_POST['submit'] == "encrypt") ? 1 : 0;
for($i=0; $i<strlen($_POST['user_junk']); $i++)
{
$char = ord($_POST['user_junk']{$i});
if(isset($chars[$method][$char]))
	$coded .= chr($chars[$method][$char]);
}
if($method == 1)
echo "Encryted Password:<br>".$coded. "<br/><br/>";
else
echo "Decryted Password:<br>".$coded. "<br/><br/>";

?>

 

 

Enjoy,

Orio.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.