PHP tea Encryption

Source: Internet
Author: User
<? PHP/*** PHP version of the tea encryption algorithm, provides attachment upload and download encryption, the decryption function * includes encrypt encryption decrypt decryption setkey setting key * during decryption, you need to set the amount of data to be filled in for encryption. This prevents redundant garbled encryption steps from a multiple of 8, which is 16 or 32, PHP is of the weak language type, and its Integer Range must be limited.Little endian low byte order* // * $ Key = '% 9 ^ q69le $ OMG: ion'; $ T = new tea (); $ jmstr = $ T-> readstr (); $ jiami = $ T-> encrypt ($ jmstr, $ key); // $ YC = strlen ($ date ); original length // $ bj = 8-$ YC % 8; // $ T-> setsign ($ BJ); $ Jimi = $ T-> decrypt ($ jiami, $ key); $ ZH = strlen ($ Jimi); $ bj = $ T-> getsign (); // $ T-> writestr (substr ($ Jimi, $ BJ, $ ZH); */class tea {private $ A, $ B, $ C, $ D; private $ n_iter, $ sign; public function _ construct () {$ this-> setiter (16);} private Function setiter ($ n_iter) {$ this-> n_iter = $ n_iter;} private function getiter () {return $ this-> n_iter;} public function setsign ($ sign) {return $ this-> Sign = $ sign;} public function getsign () {return $ this-> sign;} public function encrypt ($ data, $ key) {$ Sign = 8-strlen ($ data) % 8; // This value is the key value of 8 to be filled. $ n = $ this-> _ resize ($ data, 8); $ data_long [0] = $ sign; // The first encrypted value. This value is an 8-fold value to be filled. // $ this-> setsi GN ($ sign); // key formatted data in the order of little-Endian $ n_data_long = $ this-> _ str2long (1, $ data, $ data_long ); // format the key to 128-bit 16-byte 8-bit $ this-> _ resize ($ key, 16, true); If (''= $ key) $ key = '000000'; // convert key to long $ n_key_long = $ this-> _ str2long (0, $ key, $ key_long ); // key // encrypt the long data with the key $ enc_data = ''; $ W = array (0, 0); $ J = 0; $ k = array (0, 0, 0, 0); f Or ($ I = 0; $ I <$ n_data_long; $ I = $ I + 2) {// n_data_long is an integer data array length value. This array stores the converted data value. // get next key part of 128 bits if ($ J + 4 <= $ n_key_long) {$ K [0] = $ key_long [$ J]; $ K [1] = $ key_long [$ J + 1]; $ K [2] = $ key_long [$ J + 2]; $ K [3] = $ key_long [$ J + 3];} $ this-> _ encipherlong ($ data_long [$ I], $ data_long [$ I + 1], $ W, $ K ); // append the enciphered longs to the result $ enc_data. = $ this-> _ Long2str ($ W [0]); $ enc_data. = $ this-> _ long2str ($ W [1]);} return $ enc_data;} public function decrypt ($ enc_data, $ key) {// convert data to long $ n_enc_data_long = $ this-> _ str2long (0, $ enc_data, $ enc_data_long); // resize key to a multiple of 128 bits (16 bytes) $ this-> _ resize ($ key, 16, true); If (''= $ key) $ key = '123 '; // convert key to long $ n_key_long = $ this-> _ str2 Long (0, $ key, $ key_long); // decrypt the long data with the key $ DATA = ''; $ W = array (0, 0 ); $ J = 0; $ Len = 0; $ k = array (0, 0, 0, 0); $ Pos = 0; For ($ I = 0; $ I <$ n_enc_data_long; $ I + = 2) {// get next key part of 128 bits if ($ J + 4 <= $ n_key_long) {$ K [0] = $ key_long [$ J]; $ K [1] = $ key_long [$ J + 1]; $ K [2] = $ key_long [$ J + 2]; $ K [3] = $ key_long [$ J + 3];} $ this-> _ decipherlong ($ E Nc_data_long [$ I], $ enc_data_long [$ I + 1], $ W, $ K); if ($ I = 0) {$ this-> setsign ($ W [0]); // captures the filled space} $ data. = $ this-> _ long2str ($ W [0]); $ data. = $ this-> _ long2str ($ W [1]);} return $ data;} private function _ encipherlong ($ y, $ Z, & $ W, & $ K) {$ sum = (integer) 0; $ Delta = 0x9e3779b9; $ n = (integer) $ this-> n_iter; $ bjz_int = php_int_max; // The integer boundary value while ($ n --> 0) {$ sum + = $ delta; $ Y + = (($ Z <4) + $ K [0]) ^ ($ Z + $ sum) ^ ($ z> 5) + $ K [1]); if ($ Y <=-$ bjz_int) // adjust the key value of the displacement value {$ Y = $ Y + 4294967296;} else if ($ y >=$ bjz_int) {$ Y = $ y-4294967296;} $ Z + = ($ Y <4) + $ K [2]) ^ ($ Y + $ sum) ^ ($ Y> 5) + $ K [3]); // adjust the key value of the displacement value if ($ z <=-$ bjz_int) {$ z = $ Z + 4294967296;} else if ($ Z >=$ bjz_int) {$ z = $ z-4294967296 ;}}$ W [0] = $ Y; $ W [1] = $ Z;} private function _ decipherlong ($ y, $ Z, & $ W, & $ K ){ // Sum = delta <5, in general sum = delta * N $ n = (integer) $ this-> n_iter; $ Delta = 0x9e3779b9; if ($ n = 16) {$ sum = 0xe3779b90;} else if ($ n = 32) {$ sum = 0xc6ef3720 ;} else {$ sum = $ Delta * $ N;} $ bjz_int = php_int_max; // while ($ n --> 0) {$ WY = $ Y + $ sum; // displacement adjustment if ($ WY >=$ bjz_int) {$ WY = $ wy-4294967296 ;} else if ($ WY <=-$ bjz_int) {$ WY = $ WY + 4294967296;} $ Z-= ($ Y <4) + $ K [2]) ^ ($ WY) ^ ($ Y> 5) + $ K [3]); // adjust the key value of the displacement value if ($ z <=-$ bjz_int) {$ z = $ Z + 4294967296;} else if ($ Z >=$ bjz_int) {$ z = $ z-4294967296;} $ WZ = $ Z + $ sum; // displacement adjustment amount if ($ WZ >=$ bjz_int) {$ WZ = $ wz-4294967296;} else if ($ WZ <=-$ bjz_int) {$ WZ = $ WZ + 4294967296;} $ Y-= ($ z <4) + $ K [0]) ^ ($ WZ) ^ ($ z> 5) + $ K [1]); // adjust the key value of the displacement value if ($ Y <=-$ bjz_int) {$ Y = $ Y + 4294967296;} else if ($ y >=$ bjz_int) {$ Y = $ y-429496729 6 ;}$ sum-= $ delta; if ($ sum >=$ bjz_int) // adjust the sum count {$ sum = $ sum-4294967296 ;} else if ($ sum <=-$ bjz_int) {$ sum = $ sum + 4294967296; }}$ W [0] = $ Y; $ W [1] = $ Z;} private function _ resize (& $ data, $ size, $ nonull = false) {$ Dach = ''; // key $ n = strlen ($ data); if ($ DATA = '% 9 ^ q69le $ OMG: ion ') // modify the key // if ($ DATA = 'lzm0ffyatjaoc6: y') // modify the key {$ nmod = $ n % $ size ;} else {$ nmod = 8-$ n % $ size-4;} if ($ nmod> 0 )// If the value is greater than 0, fill in the vacancy {for ($ I = 0; $ I <$ nmod; $ I ++) {$ Dach. = CHR (0); // write it as a fixed value for testing} $ DATA = $ Dach. $ data; // fill in the vacancy to facilitate the shift} return $ N;} private function _ str2long ($ start, & $ data, & $ data_long) {$ n = strlen ($ data); $ TMP = unpack ('v * ', $ data ); // $ J = $ start; foreach ($ TMP as $ value) {$ data_long [$ J ++] = $ value ;} return $ J;} private function _ long2str ($ L) {return pack ('V', $ L);} // a log record letter Number function writelog ($ Str) {$ Showtime = date ("Y-m-d h: I: s "); $ file = "/home/Nemo/data/jmlog. log "; $ file_pointer = fopen ($ file," A "); fwrite ($ file_pointer, $ showtime. & quot; #5880 message: & quot;); fwrite ($ file_pointer, $ Str. "\ r \ n"); fclose ($ file_pointer);} // read the file function readstr () {$ fpath = "/home/Nemo/Apache/htdocs/tea/sysbian.txt"; $ ftext = file_get_contents ($ fpath); return $ ftext ;} // write the file function writestr ($ Str) {$ fpath = "/home/Nemo/apache2/htdocs/tea/sysbian.txt"; $ fp = fopen ($ fpath, 'A'); fwrite ($ FP, $ Str); fclose ($ FP);} // provides the function calculation time function get_microtime () {list ($ USEC, $ Sec) = explode ('', microtime (); Return (float) $ USEC + (float) $ Sec) ;}}?>

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.