A two-way encryption/decryption method (php)

Source: Internet
Author: User
Tags ereg
A two-way encryption/decryption method (php) <P> Based on hexadecimal conversion </P> <P> & lt ;? <Br/> // madebyhuyang @ 2005-01-20 | 11-04-06 <

Base on hexadecimal conversion

// Made by huyang @ 2005-01-20 | 11-04-06
# Use n-carry to m-carry
#0 ~ 9A-Z, which can be understood as a 36-digit range at most
Print'Encryption and decryption method';
Class carry
{
Function carry ($ n, $ m)
{
$ This-> n = $ n;
$ This-> m = $ m;
$ This-> chn ['0'] = 0;
$ This-> chn ['1'] = 1;
$ This-> chn ['2'] = 2;
$ This-> chn ['3'] = 3;
$ This-> chn ['4'] = 4;
$ This-> chn ['5'] = 5;
$ This-> chn ['6'] = 6;
$ This-> chn ['7'] = 7;
$ This-> chn ['8'] = 8;
$ This-> chn ['9'] = 9;
$ This-> chn ['A'] = 10;
$ This-> chn ['B'] = 11;
$ This-> chn ['c'] = 12;
$ This-> chn ['D'] = 13;
$ This-> chn ['E'] = 14;
$ This-> chn ['F'] = 15;
$ This-> chn ['G'] = 16;
$ This-> chn ['H'] = 17;
$ This-> chn ['I'] = 18;
$ This-> chn ['J'] = 19;
$ This-> chn ['K'] = 20;
$ This-> chn ['L'] = 21;
$ This-> chn ['M'] = 22;
$ This-> chn ['n'] = 23;
$ This-> chn ['O'] = 24;
$ This-> chn ['P'] = 25;
$ This-> chn ['Q'] = 26;
$ This-> chn ['R'] = 27;
$ This-> chn ['s '] = 28;
$ This-> chn ['t'] = 29;
$ This-> chn ['u'] = 30;
$ This-> chn ['V'] = 31;
$ This-> chn ['w'] = 32;
$ This-> chn ['x'] = 33;
$ This-> chn ['Y'] = 34;
$ This-> chn ['Z'] = 35;
$ This-> cn [0] = '0 ';
$ This-> cn [1] = '1 ';
$ This-> cn [2] = '2 ';
$ This-> cn [3] = '3 ';
$ This-> cn [4] = '4 ';
$ This-> cn [5] = '5 ';
$ This-> cn [6] = '6 ';
$ This-> cn [7] = '7 ';
$ This-> cn [8] = '8 ';
$ This-> cn [9] = '9 ';
$ This-> cn [10] = 'a ';
$ This-> cn [11] = 'B ';
$ This-> cn [12] = 'C ';
$ This-> cn [13] = 'd ';
$ This-> cn [14] = 'e ';
$ This-> cn [15] = 'F ';
$ This-> cn [16] = 'g ';
$ This-> cn [17] = 'h ';
$ This-> cn [18] = 'I ';
$ This-> cn [19] = 'J ';
$ This-> cn [20] = 'K ';
$ This-> cn [21] = 'l ';
$ This-> cn [22] = 'M ';
$ This-> cn [23] = 'n ';
$ This-> cn [24] = 'O ';
$ This-> cn [25] = 'p ';
$ This-> cn [26] = 'q ';
$ This-> cn [27] = 'R ';
$ This-> cn [28] ='s ';
$ This-> cn [29] ='t ';
$ This-> cn [30] = 'u ';
$ This-> cn [31] = 'V ';
$ This-> cn [32] = 'w ';
$ This-> cn [33] = 'x ';
$ This-> cn [34] = 'y ';
$ This-> cn [35] = 'Z ';
}
Function check1 ($)
{
If (ereg ("[0-9]", $ ))
If ($ a> = 2)
If ($ a <= 36)
Return true;
Return false;
}
Function check2 ($)
{
$ La = 0;
For ($ j = 0; $ j {
If ($ this-> chn [substr ($ a, $ j, 1)] >=$ this-> n)
$ La = 1;
If ($ la)
Break;
}
If ($ la)
Return false;
Else
Return true;
}
Function toDEC ($ a) // $ n-> Decimal
{
If ($ this-> n = 10)
Return $;
Else
{
$ A = strrev ($ );
$ K = 0;
For ($ I = 0; $ I {
$ K + = $ this-> chn [substr ($ a, $ I, 1)] * pow ($ this-> n, $ I );
}
Return $ k;
}
}
Function gethigh ($)
{
If ($ a <= 1)
Return 1;
Else
{
For ($ I = 0; I I <100; $ I ++)
{
If (pow ($ this-> m, $ I)> $)
Break;
}
Return $ I;
}
}
Function toM ($ a) // decimal-> $ m
{
$ A1 = $ this-> gethigh ($ );
$ Res = "";
For ($ I = 1; $ I <= $ a1; $ I ++)
{
$ U = ($ a-$ a % pow ($ this-> m, ($ a1-$ I)/(pow ($ this-> m, ($ a1-$ I )));
$ A-= $ u * pow ($ this-> m, ($ a1-$ I ));
$ Res. = $ this-> cn [$ u];
}
Return $ res;
}
Function get ($)
{
If (! $ This-> n) $ this-> n = 10;
If (! $ This-> m) $ this-> m = 10;
If ($ this-> check1 ($ this-> n) & ($ this-> check1 ($ this-> m )))
{
If (ereg ("[0-9A-Z]", $ ))
{
If ($ this-> check2 ($ ))
{
Return $ this-> toM ($ this-> toDEC ($ ));
}
Else
Return false;
}
Else
Return false;
}
Else
Return false;
}
}
Class han
{
Function han ()
{
For ($ I = 0; $ I <10; $ I ++)
{
$ This-> hu1 [$ I] = $ I;
$ This-> hu2 [$ I] = $ I;
}
For ($ I = 10; $ I <36; $ I ++)
{
$ This-> hu1 [$ I] = chr ($ I + 55 );
$ This-> hu2 [chr ($ I + 55)] = $ I;
}
For ($ I = 36; $ I <62; $ I ++)
{
$ This-> hu1 [$ I] = chr ($ I + 61 );
$ This-> hu2 [chr ($ I + 61)] = $ I;
}
$ This-> hu1 [62] = chr (42 );
$ This-> hu1 [63] = chr (43 );
$ This-> hu2 [chr (42)] = 62;
$ This-> hu2 [chr (43)] = 63;
}
Function tocode ($ str) // Convert a group of characters into code 0 ~ 65536
{
$ Huyang1 = new carry (10, 33 );
$ J = 0;
For ($ I = 0; $ I {
$ P = ord (substr ($ str, $ I, 1 ));
If ($ p> 160)
{
$ Q = ord (substr ($ str, ++ $ I, 1 ));
$ P = $ p * 256 + $ q;
}
Else
$ P = 255*256 + $ p;
$ Ret. = $ huyang1-> get ($ p );
$ J ++;
}
Return $ ret;
}
Function getcode ($ str)
{
$ Huyang = new carry (3, 10 );
$ Res = "";
For ($ I = 0; $ I <(strlen ($ str)/4); $ I ++)
{
$ A = $ huyang-> get (substr ($ str, ($ I * 4), 4 ));
$ A2 = $ a % 256;
$ A_1 = ($ a-$ a2)/256;
If ($ a1 = 255)
$ Res. = chr ($ a2 );
Else
$ Res. = chr ($ a1). chr ($ a2 );
}
Return $ res;
}
Function encode ($ str)
{
$ Res = "";
$ Huyang = new carry (35, 7 );
$ Huyang1 = new carry (8, 10 );
$ Str1 = $ this-> tocode ($ str );
$ K = ceil (strlen ($ str1)/3 );
For ($ I = 0; $ I <$ k; $ I ++)
{
$ Word = $ this-> hu1 [rand (0, 61)];
$ Res1 = $ huyang1-> get ($ huyang-> get (substr ($ str1, ($ I * 3), 3 )));
$ Res11 = ($ res1-$ res1 % (64*64)/(64*64 );
$ Res1-= $ res11 * 64*64;
$ Res12 = ($ res1-$ res1 % 64)/64;
$ Res1-= $ res12 * 64;
$ Res13 = $ res1;
If ($ I = ($ K-1 ))
{
If ($ res11! = 0)
{
$ Res. = $ this-> hu1 [$ res11]. $ this-> hu1 [$ res12]. $ this-> hu1 [$ res13]. $ word;
}
Elseif ($ res12! = 0)
{
$ Res. = $ this-> hu1 [$ res12]. $ this-> hu1 [$ res13]. $ word;
}
Elseif ($ res13! = 0)
{
$ Res. = $ this-> hu1 [$ res13]. $ word;
}
}
Else
$ Res. = $ this-> hu1 [$ res11]. $ this-> hu1 [$ res12]. $ this-> hu1 [$ res13]. $ word;
}
Return trim ($ res );
}
Function discode ($ str)
{
$ Len = ceil (strlen ($ str)/4 );
$ Res = "";
$ A = strlen ($ str)-4 * ($ len-1 );
For ($ I = 0; $ I <$ len; $ I ++)
{
If ($ I! = ($ Len-1 ))
{
$ Res1 = substr ($ str, $ I * 4, 1 );
$ Res2 = substr ($ str, ($ I * 4 + 1), 1 );
$ Res3 = substr ($ str, ($ I * 4 + 2), 1 );
$ Res11 = $ this-> hu2 [$ res1];
$ Res12 = $ this-> hu2 [$ res2];
$ Res13 = $ this-> hu2 [$ res3];
$ Res14 = $ res11 * 64*64 + $ res12 * 64 + $ res13;
$ Res. = "". $ res14;
}
Else
{
If ($ a % 4 = 0)
{
$ Res1 = substr ($ str, $ I * 4, 1 );
$ Res2 = substr ($ str, ($ I * 4 + 1), 1 );
$ Res3 = substr ($ str, ($ I * 4 + 2), 1 );
$ Res11 = $ this-> hu2 [$ res1];
$ Res12 = $ this-> hu2 [$ res2];
$ Res13 = $ this-> hu2 [$ res3];
$ Res14 = $ res11 * 64*64 + $ res12 * 64 + $ res13;
$ Res. = "". $ res14;
}
Elseif ($ a % 4 = 2)
{
$ Res1 = substr ($ str, $ I * 4, 1 );
$ Res11 = $ this-> hu2 [$ res1];
$ Res14 = $ res11;
$ Res. = "". $ res14;
}
Elseif ($ a % 4 = 3)
{
$ Res1 = substr ($ str, $ I * 4, 1 );
$ Res2 = substr ($ str, ($ I * 4 + 1), 1 );
$ Res11 = $ this-> hu2 [$ res1];
$ Res12 = $ this-> hu2 [$ res2];
$ Res14 = $ res11 * 64 + $ res12;
$ Res. = "". $ res14;
}
}
}
Return trim ($ res );
}
Function decode ($ str)
{
$ Str = $ this-> discode ($ str );
$ A = explode ("", $ str );
$ Res = "";
$ Huyang = new carry (7, 35 );
$ Huyang1 = new carry (10, 8 );
For ($ I = 0; $ I {
// $ Res. = $ huyang-> get ($ a [$ I]);
If ($ I = (count ($ a)-1 ))
$ Res. = $ huyang-> get ($ huyang1-> get ($ a [$ I]);
Else
{
$ B = $ huyang-> get ($ huyang1-> get ($ a [$ I]);
If (strlen ($ B) = 0)
$ Res. = "000 ";
Elseif (strlen ($ B) = 1)
$ Res. = "00". $ B;
Elseif (strlen ($ B) = 2)
$ Res. = "0". $ B;
Else
$ Res. = $ B;
}
}
Return $ this-> getcode ($ res );
}
}
$ S = microtime ();
$ St = explode ('', $ s );
$ St1 = $ st [0];
$ St2 = $ st [1];
$ Huyang = new han;
$ Str = $ _ POST ['str'];
If (! $ Str) $ str = "Enter the query statement !! ";
$ Len = strlen ($ str );
If ($ submit)
$ A = $ huyang-> encode ($ str );
If ($ submit1)
$ A = $ huyang-> decode ($ str );
$ A = str_replace ("\", "\", $ );
$ A = str_replace ("\" "," \ "", $ );
$ A = str_replace ("\ '", "\'", $ );
$ S = microtime ();
$ St = explode ('', $ s );
$ Sta = $ st [0];
$ Stb = $ st [1];
$ Ss1 = $ sta-$ st1;
$ Ss2 = $ stb-$ st2;
$ Cus = $ ss1 + $ ss2;
$ Cus1 = $ cus/$ len;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.