0

there are lot of number starting from 0-.... and we don't wanna show this ordered id to anyone and we have to give unique number somewhere so how to encode number to another number.

and each encoded number nust represent just one number. just like encryption not hashing. we also need uniqueness means if 56 give 842 then no else should give 842.

here similar quetions for C# Encrypt a number to another number of the same length but how can we do that in php

thanks.

Community
  • 1
  • 1
dev.meghraj
  • 8,542
  • 5
  • 38
  • 76
  • I guess the answer which `MarcB` gave is in PHP. – Shankar Narayana Damodaran Jan 21 '14 at 07:55
  • If you actually need some unique database id then create a unique ID that is run through sha256 once and use this as a primary key or some other indexed column by which you retrieve. You probably do not need to reverse this number anyway. You just need to make it unpredictable. Hash collisions are neglectable in your case (as far as I can see your case). and you will need some number representation of it if this is indeed a hard requirement for you – Samuel Jan 21 '14 at 08:07

2 Answers2

1

I have no idea why people overcomplicate things by inventing own encryption systems which may either be not good or unnecessary.

PHP offers you mcrypt:http://de3.php.net/mcrypt A good way to encrypt and decrypt something in a cryptographically strong way.

Note: Base64 is not a good solution as this is no encryption

Edit: Again relying on some user created encryption is not recommended. It is not well tested and relies partially on security by obscurity. Albeit mcrypt requires some calls to the mcrypt api it allows you to use cryptographically strong systems like MCRYPT_RIJNDAEL_256, MCRYPT_TWOFISH256 etc.

I do not know anything about your security requirements but NEVER invent the wheel again. When you make a mistake in such things you won't notice as a malicious user won't tell you that but simply exploit your system.

See https://security.stackexchange.com/questions/18197/why-shouldnt-we-roll-our-own

Community
  • 1
  • 1
Samuel
  • 6,126
  • 35
  • 70
  • is it possible to do this with mcrypt? ok let me read about mcrypt – dev.meghraj Jan 21 '14 at 07:59
  • i agree with you.. i don't wanna make my own thats why i ask here.... becuase i know i can secretly make things like (($i*454)-475) etc etc but i want strong and reliable things... i just hate reinventing wheel – dev.meghraj Jan 21 '14 at 08:05
-3

You can use base64_encode & base64_decode in case of PHP.

For Example:

<?php
$order_id = "123";
$enc=base64_encode($order_id);//MTIz
echo base64_decode($enc);//123
?>
Harshal
  • 3,562
  • 9
  • 36
  • 65