Jump to content

Converting a number to another number (encrypt - decrypt)


Recommended Posts

I want to encrypt the ID number into another number. But then I want to be able to decrypt the encrypted number so that I can see the original number. Any simple ideas would be appreciated.

 

 

 

$original_num = "123-45";
$encrypted_num = encrypt($original_num);
echo $encrypted_num;
//$encrypted_num = somthing like 73632

 

$decrypted_num = decrypt($encrypted_num);
echo $decrypted_num;

 

 

 

I'm not an encryption expert by any means but many encryptions are designed so you can't decrypt back to the original (which is why you see the encrypted passwords in databases)

 

example = "md5" or "sha"

 

 

I assume base_64 doesn't work this way, however, right?

base_64 can be decrypted.  If all you're looking for is a simple method of encrypting and it doesn't need to be secure you do something as simple as multiplying it by 17 and adding "32" to it.  then to decrypt subtract "32" and divide by 17. (Or whatever numbers you'd want to use)

 

Easy enough, but again not secure.

<?php

// encrypt
$num = "123-45"; // starting number
$hyphen = !($tmp = strpos($num, '-')) ? 0 : $tmp; // locate the hyphen position
$num = str_replace('-', '', $num); // remove the hyphen
$num = $num * 4; // alter the number however you like
$num = (int)($hyphen . $num); // prefix the hyphen position to altered number and convert back to int
echo $num;

// decrypt
$hyphen = substr($num, 0, 1); // get hyphen position
$num = substr($num, 1); // trim hyphen position off the string
$num = $num / 4; // undo the number alteration
// put the hyphen back in the number
$num = substr($num, 0, $hyphen) . '-' . substr($num, $hyphen + 1);
echo $num;

?>

I might be off on the indexes in that second to last line, but that should give you an idea of a simple alteration you can make.  Feel fee to make it as complicated as you want.

Give http://www.tonymarston.co.uk/php-mysql/encryption.htm a look.

 

It describes a reversible encryption routine for PHP. There is an online test

harness so you can see it working, and you have access to the code as

well.

 

 

This website appears to be down

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.