ID obfuscation algorithm class and usage example implemented by PHP
- 2021-10-27 06:52:22
- OfStack
This paper describes the class and usage of ID obfuscation algorithm implemented by PHP. Share it for your reference, as follows:
<?php
/**
* ID Confusation algorithm
*/
class IdCrypt
{
/**
* For integers id Perform reversible confusion
*/
public static function encodeId($id)
{
$sid = ($id & 0xff000000);
$sid += ($id & 0x0000ff00) << 8;
$sid += ($id & 0x00ff0000) >> 8;
$sid += ($id & 0x0000000f) << 4;
$sid += ($id & 0x000000f0) >> 4;
$sid ^= 11184810;
return $sid;
}
/**
* To pass encodeId Confused id Make a restore
*/
public static function decodeId($sid)
{
if (!is_numeric($sid)) {
return false;
}
$sid ^= 11184810;
$id = ($sid & 0xff000000);
$id += ($sid & 0x00ff0000) >> 8;
$id += ($sid & 0x0000ff00) << 8;
$id += ($sid & 0x000000f0) >> 4;
$id += ($sid & 0x0000000f) << 4;
return $id;
}
}
$idstr = new IdCrypt();
echo $encodeid = $idstr->encodeId('12345678');
echo "<br/>";
echo $decodeid = $idstr->decodeId($encodeid);
?>
Run results:
13309518
12345678
PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.ofstack.com/password/txt_encode
MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password
Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode
For more readers interested in PHP related contents, please check the special topics of this site: "Summary of php Encryption Methods", "Summary of PHP Encoding and Transcoding Operation Skills", "Summary of PHP Mathematical Operation Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Tutorial of PHP Data Structure and Algorithm", "Summary of php Programming Algorithm" and "Summary of php Regular Expression Usage"
I hope this article is helpful to everyone's PHP programming.