Because of the need to pass a secret ID, so the use of symmetric encryption, but the MCRYPT_ENCRYPT algorithm encryption after the string is too long, so want to implement a custom encryption algorithm, the idea is as follows
First, the key is calculated SHA1, the first 32bit of the result, and then with the integer to be encrypted or to obtain an encrypted 32bit result
Grouping results: 2bit | 6bit | 6bit | 6bit | 6bit | 6bit
Each group is named: A0, A1, A2, A3, A4, A5
Also defines a dictionary array of length 64
$dict =array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',
' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ',
' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ',
' 01 ', ' 02 ', ' 03 ');
With each previously grouped value as the subscript for the dictionary array, the result is: $dict [A0]. $dict [A1]. $dict [A2]. $dict [A3]. $dict [A4]. $dict [A5]
The result of this encryption is a string of length 6-12, if the last 3 elements of the dictionary array are represented by other single characters, then the result is fixed to a 6-character string.
Because beginners PHP soon, the Library of PHP is not familiar with, ask the hero to help implement the encryption and decryption algorithm:
String Encrypt (int id,string key)
int decrypt (string text,string key)
Reply to discussion (solution)
Echo Encrypt (1234, ' abc '), Php_eol;echo decrypt (' 1TgGSY ', ' abc '), Function Encrypt ($id, $key) {$dict = array (' 1 ', ' 2 ', ' 3 ') , ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ' , ' X ', ' Y ', ' Z ', '-', ' = ', ' * '); $key = current (Unpack (' L ', SHA1 ($key, 1))); $id ^= $key; $t = Str_split (sprintf ('%036b ', $id), 6); foreach ($t as & $v) $v = $dict [Bindec ($v)]; return join ($t);} function decrypt ($s, $key) {$dict = array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ' , ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', '-', ' = ', ' * '; $dict = Array_flip ($dict); foreach (Str_split ($s) as $c) $r [] = sprintf ('%06b ', $dict [$c]); $id = Bindec (Join ($r)); $key = Current(Unpack (' L ', SHA1 ($key, 1))); return $id ^ $key;}1TgGSY
1234
I have also implemented the encryption process, but it does not look so elegant, the implementation of low efficiency, put out to foil the master demeanor
$key _string = ' abc ', Function Keytoint ($key) {$key _SHA1 = SHA1 ($key); $first _char = $key _SHA1 [0];if (Ord ($first _char) >) {return Hexdec (Hexdec ($first _char) & 7). substr ($key _sha1, 1, 7) ) | (-2147483648);} else {return Hexdec ($key _sha1);}} function Encrypt ($num) {$dict = array (' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' j ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' * ', '! '); $r 1 = $num ^ keytoint ($key _string), $r 2 = Decbin ($r 1), $r 3 = Array (Bindec (substr ($r 2, 0, 2)), Bindec (substr ( $r 2, 2, 6), Bindec (substr ($r 2, 8, 6)), Bindec (substr ($r 2,, 6)), Bindec (substr ($r 2,, 6)), Bindec (s Ubstr ($r 2, 6)); return $dict [$r 3 [0]]. $dict [$r 3 [1]]. $dict [$r 3 [2]]. $dict [$r 3 [3]]. $dict [$r 3 [4]]. $dict [$r 3 [5]];}
After the implementation of the algorithm, found that in the design of the algorithm, there is a flaw is not considered,
Because only the ID and key XOR, the result of encryption has a certain regularity,
Like what
1234->1tggsy
1235->1tggsx
Is there any good way to break the results?
This is messy enough.
$id = 1234; $key = ' AAA '; for ($i =1; $i <100; $i + +) {printf ("%-10d%s%s\n", $id, $s = Encrypt ($id + +, $key), decrypt ($s, $key));} function Encrypt ($id, $key) {$dict = array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' j ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', '-', ' = ', ' * '; $n = rand (0, 15); Srand ($n); $key = current (Unpack (' L ', substr (SHA1 ($key, 1), $n))); $id ^= $key; $t = Str_split (sprintf ('%04b%032b ', $n, $id), 6); foreach ($t as $i =>& $v) {$v = $dict [Bindec ($v)]; if ($i = = 0) shuffle ($dict); } return join ($t);} function decrypt ($s, $key) {$dict = array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ' , ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', '-', ' = ', ' * '); $m = Array_search ($s {0}, $dict); $n = $m >> 2; Srand ($n); Shuffle ($dict); $dict = Array_flip ($dict); foreach (Str_split ($s) as $i = + $c) {$r [] = sprintf ('%06b ', $i ==0? $m &0x03: $dict [$c]); } $id = Bindec (Join ($r)); $key = current (Unpack (' L ', substr (SHA1 ($key, 1), $n))); return $id ^ $key;}
1234 4rhk4b 12341235 od2ln* 12351236 wqkf8u 12361237 6k=gvu 12371238 bxecr* 12381239 = W-aoi 12391240 iiq3e1 12401241 z6umma 12411242 WLcnd8 12421243 rizj*m 12431244 4rhk47 124412 OD2LNT 12451246 wqkf8z 12461247 6k=gvj 12471248 bxecre 12481249 =w-aop 12491250 IiQ 3et 12501251 z6ummp 12511252 wlcndu 12521253 rizj*p 12531254 4rhk4s 12541255 od2lns 12551256 wqkf84 12561257 6k=gvn 12571258 bxecrl 12581259 =w-aot 12591260 iiq3ex 12601261 Z6umm 1 12611262 wlcndd 12621263 rizj*s 12631264 4rhk4h 12641265 od2lnq 12651266 wqkf83 12661267 6K=GVG 12671268 bxeCr5 12681269 =w-aoh 12691270 iiq3ep 12701271 z6ummc 12711272 Wlcnde 12721273 rizj*6 12731274 4rhk4i 12741275 od2ln= 12751276 wqkf8u 12761277 6k=gvi 12771278 BXECR9 12781279 =w-aol 12791280 iiq3bi 12801281 z6umhg 12811282 wlcnay 12821283 rizj6d 12831284 4rhk3z 1284 1285 od2l*n 12851286 wqkfbp 12861287 6k=gzj 12871288 bxec=o 12881289 =w-aed 12891290 I Iq3by 12901291 z6umh* 12911292 wlcnag 12921293 rizj6v 12931294 4rhk3f 12941295 od2l*e 129512 WQKFBJ 12961297 6k=gzm 12971298 bxec=n 12981299 =w-aew 12991300 iiq3bs 13001301 z6u Mhl 13011302 WLcna4 13021303 rizj6v 13031304 4rhk3u 13041305 od2l*v 13051306 wqkfbm 13061307 6k=gz* 13071308 bxec=-13081309 =w-aea 13091310 iiq3bm 13101311 z6umhe 13111312 Wlcna S 13121313 rizj6= 13131314 4rhk38 13141315 od2l*l 13151316 wqkfbs 13161317 6k=gz6 13171318 Bxec=q 13181319 =w-aen 13191320 iiq3bo 13201321 z6umhv 13211322 Wlcnau 13221323 Rizj61 13231324 4rhk3k 13241325 od2l*p 13251326 wqkfbv 13261327 6k=gzw 13271328 bxec=h 13281329 =w-ae-13291330 Iiq3bs 13301331 z6umhj 13311332 WLcna9 1332
In your design, the first section is only 2bit significant bit, so you can attach 4bit information on it.
And 0~15 's random number is exactly 4bit.
Algorithm, this random number plays a role of 2
1. Adjust key
2. Upset the dictionary
If you don't need too much privacy, bit arithmetic shifts are enough, save the CPU.
The landlord's answer is very praise
Plus the random number, 1 integers can correspond to 16 kinds of results, 64^6 the results of all the use, decryption also no longer judge is not an invalid string.
Is the landlord's answer is excellent, wrong
No, No.