Php将一个数字改成另一个可以改回原来的号码 [英] Php change a number into another number that can be changed back to the original
问题描述
使用PHP,我试图将一个数字编码成另一个数字,我可以解码回原来的数字。编码的字符串只需要数字,不能包含任何其他内容。
例如:10成为573563547892或类似的东西。
如何在PHP中执行这样的操作?我尝试了相当多的加密解密功能,但没有输出数字。
我在一个URL中寻找一些不容易猜到的东西。 >
所以: http://www.me .com / index.PHP?page = 20 成为 http:// www。 me.com/index.PHP?page=5705254782562466
相当沉重但非常好的加密,通过使用 ord
& chr
有点。虽然这有效,但考虑其他选项:只需使用字符串,而不是数字已经使它更简单( base64_encode
等):
<?php
class Crypter {
private $ key = ;
private $ iv ='';
函数__construct($ key,$ iv){
$ this-> key = $ key;
$ this-> iv = $ iv;
}
protected function getCipher(){
$ cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($ cipher,$ this-> key,$ this-> iv);
return $ cipher;
}
函数encrypt($ string){
$ binary = mcrypt_generic($ this-> getCipher(),$ string);
$ string =''; ($ i = 0; $ i< strlen($ binary); $ i ++){
$ string。= str_pad(ord($ binary [$ i]),3,'0' ,STR_PAD_LEFT);
}
return $ string;
}
函数解密($ encrypted){
//检查缺少的前导0的
$ encrypted = str_pad($ encrypted,ceil(strlen($ encrypted)/ 3)* 3,'0',STR_PAD_LEFT);
$ binary ='';
$ values = str_split($ encrypted,3);
foreach($ values as $ chr){
$ chr = ltrim($ chr,'0');
$ binary。= chr($ chr);
}
return mdecrypt_generic($ this-> getCipher(),$ binary);
}
}
$ crypt = new Crypter('secret key','12348765');
$ encrypted = $ crypt-> encrypt(1234);
echo $ encrypted.PHP_EOL;
//假缺少前导0
$ encrypted = ltrim($ encrypted,'0');
echo $ encrypted.PHP_EOL;
$ decryptpted = $ crypt-> decrypt($ encrypted);
echo $ decryptpted.PHP_EOL;
结果:
code> 057044206104214236155088
57044206104214236155088
1234
Using PHP, I'm trying to encode a number into another number that I can decode back to the original number. The encoded string needs to be only numbers and should not contain anything else.
Eg: 10 becomes 573563547892 or something like that.
How can I do something like this in PHP? I tried quite a few encrypt decrypt functions, but none output only numbers.
I looking for something to use in a URL that isn't easy to guess.
So: http://www.me.com/index.PHP?page=20 becomes http://www.me.com/index.PHP?page=5705254782562466
Quite heavy, but very good encryption, by using ord
& chr
a bit. While this works, consider other options: just being able to use strings rather then numbers already makes it a lot simpler (base64_encode
etc.):
<?php
class Crypter {
private $key = '';
private $iv = '';
function __construct($key,$iv){
$this->key = $key;
$this->iv = $iv;
}
protected function getCipher(){
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($cipher, $this->key, $this->iv);
return $cipher;
}
function encrypt($string){
$binary = mcrypt_generic($this->getCipher(),$string);
$string = '';
for($i = 0; $i < strlen($binary); $i++){
$string .= str_pad(ord($binary[$i]),3,'0',STR_PAD_LEFT);
}
return $string;
}
function decrypt($encrypted){
//check for missing leading 0's
$encrypted = str_pad($encrypted, ceil(strlen($encrypted) / 3) * 3,'0', STR_PAD_LEFT);
$binary = '';
$values = str_split($encrypted,3);
foreach($values as $chr){
$chr = ltrim($chr,'0');
$binary .= chr($chr);
}
return mdecrypt_generic($this->getCipher(),$binary);
}
}
$crypt = new Crypter('secret key','12348765');
$encrypted = $crypt->encrypt(1234);
echo $encrypted.PHP_EOL;
//fake missing leading 0
$encrypted = ltrim($encrypted,'0');
echo $encrypted.PHP_EOL;
$decrypted = $crypt->decrypt($encrypted);
echo $decrypted.PHP_EOL;
Result:
057044206104214236155088
57044206104214236155088
1234
这篇关于Php将一个数字改成另一个可以改回原来的号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!