<? PHP // Encode64 encoding can replace encodeURI, encodeURIComponent, and endode functions at the same time // Because the selected characters are not encoded. Class Encode64 { Function code ($ str ){ $ KEY = 'paawo65gouf7ik2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh '; $ A = StrToBytes ($ str ); $ Len = count ($ ); $ Res = $ len % 3; $ S = ""; $ I = 2; $ v = 0; For (; $ I <$ len; $ I + = 3 ){ $ V = $ a [$ I-2] + ($ a [$ I-1] <8) + ($ a [$ I] <16 ); $ S. = $ KEY [$ v & 0x3f]; $ S. = $ KEY [($ v> 6) & 0x3f]; $ S. = $ KEY [($ v> 12) & 0x3f]; $ S. = $ KEY [($ v> 18)]; } If ($ res = 1 ){ $ V = $ a [$ I-2]; $ S. = $ KEY [$ v & 0x3f]; $ S. = $ KEY [($ v> 6) & 0x3f]; } Else if ($ res = 2 ){ $ V = $ a [$ I-2] + ($ a [$ I-1] <8 ); $ S. = $ KEY [$ v & 0x3f]; $ S. = $ KEY [($ v> 6) & 0x3f]; $ S. = $ KEY [($ v> 12) & 0x3f]; } Return $ s; } Function decode ($ codeStr ){ $ KEY = 'paawo65gouf7ik2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh '; $ Dic = array (); For ($ I = 0; $ I <64; $ I ++ ){ $ Dic [$ KEY [$ I] = $ I; } $ Len = strlen ($ codeStr ); $ Res = $ len % 4; $ CLen = floor ($ len/4) * 3; If ($ res = 2) $ clen + = 1; Elseif ($ res = 3) $ clen + = 2; $ Code = range (0, $ clen ); $ I = 3; $ v = 0; $ j = 0; For (; $ I <$ len; $ I + = 4 ){ $ V = $ dic [$ codeStr [$ I-3]; $ V + = $ dic [$ codeStr [$ I-2] <6; $ V + = $ dic [$ codeStr [$ I-1] <12; $ V + = $ dic [$ codeStr [$ I] <18; $ Code [$ j] = $ v & 0xff; $ Code [$ j + 1] = ($ v> 8) & 0xff; $ Code [$ j + 2] = ($ v> 16) & 0xff; $ J + = 3; } If ($ res = 2 ){ // The correct number of bytes must be 2 or 3. If no value is 1, discard it. $ V = $ dic [$ codeStr [$ I-3]; $ V + = $ dic [$ codeStr [$ I-2] <6; $ Code [$ j] = $ v & 0xff; } Else if ($ res = 3 ){ $ V = $ dic [$ codeStr [$ I-3]; $ V + = $ dic [$ codeStr [$ I-2] <6; $ V + = $ dic [$ codeStr [$ I-1] <12; $ Code [$ j] = $ v & 0xff; $ Code [$ j + 1] = ($ v> 8) & 0xff; } Return BytesToStr ($ code ); } } Function BytesToStr ($ bytes ){ $ Str = ''; Foreach ($ bytes as $ ch ){ $ Str. = chr ($ ch ); } Return iconv ('utf-16be', 'utf-8', $ str ); } Function StrToBytes ($ str ){ $ Str = iconv ('utf-8', 'utf-16BE ', $ str ); $ Len = strlen ($ str ); $ Bytes = array (); For ($ I = 0; $ I <$ len; $ I ++ ){ $ Bytes [] = ord ($ str [$ I]); } Return $ bytes; } ?> |