PHP des encryption:
$ cipher_list = mcrypt_list_algorithms (); // List of encryption algorithms supported by mcrypt
$ mode_list = mcrypt_list_modes (); // The list of encryption modes supported by mcrypt
// print_r ($ cipher_list);
// print_r ($ mode_list);
function encrypt ($ key, $ data) {
$ td = mcrypt_module_open ("des", "", "ecb", ""); // Use MCRYPT_DES algorithm, ecb mode
$ size = mcrypt_enc_get_iv_size ($ td); // Set the size of the initial vector
$ iv = mcrypt_create_iv ($ size, MCRYPT_RAND); // Create initial vector
$ key_size = mcrypt_enc_get_key_size ($ td); // Returns the maximum supported key length (in bytes)
$ salt = ‘‘;
$ subkey = substr (md5 (md5 ($ key). $ salt), 0, $ key_size); // complex processing of the key and set the length
mcrypt_generic_init ($ td, $ subkey, $ iv);
$ endata = mcrypt_generic ($ td, $ data);
mcrypt_generic_deinit ($ td);
mcrypt_module_close ($ td);
return $ endata;
}
function decrypt ($ key, $ endata) {
$ td = mcrypt_module_open ("des", "", "ecb", ""); // Use MCRYPT_DES algorithm, ecb mode
$ size = mcrypt_enc_get_iv_size ($ td); // Set the size of the initial vector
$ iv = mcrypt_create_iv ($ size, MCRYPT_RAND); // Create initial vector
$ key_size = mcrypt_enc_get_key_size ($ td); // Returns the maximum supported key length (in bytes)
$ salt = ‘‘;
$ subkey = substr (md5 (md5 ($ key). $ salt), 0, $ key_size); // complex processing of the key and set the length
mcrypt_generic_init ($ td, $ subkey, $ iv);
$ data = rtrim (mdecrypt_generic ($ td, $ endata)). ‘\ n’;
mcrypt_generic_deinit ($ td);
mcrypt_module_close ($ td);
return $ data;
}
$ key = "www.tencent.com";
// $ data = "Returns the maximum supported key length (involving sending fees);
$ data = "dadfafdafd, I am a good boy";
$ endata = encrypt ($ key, $ data);
$ data1 = decrypt ($ key, $ endata);
echo $ endata; // Direct output, it is garbled on the webpage, and processed with base64_encode, it becomes a total of 64 characters registered by characters, arrays, plus signs, slashes, etc.
echo base64_encode ($ endata);
echo $ data1;
All of PHP's cryptographic algorithms and models:
//rijndael-128,rijndael-192,rijndael-256就是AES加密,3种分别是使用不同的数据块和密钥长度进行加密。
Array (
[0] => cast-128 [1] => gost
[2] => rijndael-128 [3] => twofish
[4] => arcfour
[5] => cast-256 [6] => loki97
[7] => rijndael-192 [8] => saferplus
[9] => wake
[10] => blowfish-compat
[11] => des
[12] => rijndael-256 [13] => serpent
[14] => xtea
[15] => blowfish
[16] => enigma
[17] => rc2
[18] => tripledes
) Array (
[0] => cbc
[1] => cfb
[2] => ctr
[3] => ecb
[4] => ncfb
[5] => nofb
[6] => ofb
[7] => stream
)
In general, you can use the code block above, if you ask yourself to engage a class, the following classes are available:
<? php
/ *
* In the class, the functional part of the function is not refined, and needs further modification
* /
class Mymcrypt {
public $ key = "www.tence.com"; // must be a string, if it is a number
// encryption
public function do_mencrypt ($ input)
{
// $ key = substr (md5 ($ this-> key), 0, 24);
// $ td = mcrypt_module_open (‘des‘, ‘‘, ‘ecb’, ‘‘);
// $ td = mcrypt_module_open (MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_ECB, ‘‘);
$ td = mcrypt_module_open (MCRYPT_RIJNDAEL_256, ‘‘, MCRYPT_MODE_ECB, ‘‘);
$ iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ td), MCRYPT_RAND);
// No key size setting
$ key_size = mcrypt_enc_get_key_size ($ td); // Returns the maximum supported key length (in bytes)
$ salt = ‘‘;
$ subkey = substr (md5 (md5 ($ this-> key). $ salt), 0, $ key_size); // Complex key processing and setting the length
mcrypt_generic_init ($ td, $ subkey, $ iv);
$ encrypted_data = mcrypt_generic ($ td, $ input);
mcrypt_generic_deinit ($ td);
mcrypt_module_close ($ td);
// return trim (chop ($ this-> base64url_encode ($ encrypted_data)));
return $ encrypted_data;
}
// decrypt
// $ input-stuff to decrypt
public function do_mdecrypt ($ input)
{
// $ key = substr (md5 ($ this-> key), 0, 24);
// $ td = mcrypt_module_open (‘des‘, ‘‘, ‘ecb’, ‘‘);
// $ td = mcrypt_module_open (MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_ECB, ‘‘);
$ td = mcrypt_module_open (MCRYPT_RIJNDAEL_256, ‘‘, MCRYPT_MODE_ECB, ‘‘);
$ iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ td), MCRYPT_RAND);
// No key size setting
$ key_size = mcrypt_enc_get_key_size ($ td); // Returns the maximum supported key length (in bytes). You can also set it yourself, such as 24.
$ salt = ‘‘;
$ subkey = substr (md5 (md5 ($ this-> key). $ salt), 0, $ key_size); //// Complex key processing, and set the length
mcrypt_generic_init ($ td, $ subkey, $ iv);
$ decrypted_data = mdecrypt_generic ($ td, $ input);
mcrypt_generic_deinit ($ td);
mcrypt_module_close ($ td);
// return trim (chop ($ decrypted_data));
return $ decrypted_data;
}
// Base64 needs attention during the url transfer process
// After base64 encryption is transmitted in the url, "+", "/" will be replaced with "-", "_", and the equal sign "=" at the end will be removed.
// In addition, the base64 encrypted length must be a multiple of 4, so you can restore the "=" sign according to this
function base64url_encode ($ data) {
return rtrim (strtr (base64_encode ($ data), ‘+ /‘, ‘-_‘), ‘=‘);
// return $ data; // garbled if not processed
// return base64_encode ($ data);
}
function base64url_decode ($ data) {
return base64_decode (str_pad (strtr ($ data, ‘-_’, ‘+ /‘), strlen ($ data)% 4, ‘=‘, STR_PAD_RIGHT));
}
}
// usage
$ myMcrypt = new Mymcrypt ();
$ data = ‘PD867H4V9J6B’;
$ value = $ myMcrypt-> do_mencrypt ($ data);
// $ value = $ myMcrypt-> base64url_encode ($ value); // can use special processed base64_encode
$ value = base64_encode ($ value); // Convenient to display on web pages
$ value = trim ($ value); // Sometimes there will be a lot of reservations for strings
echo "$ value <br/> length:". strlen ($ value). "<br/>";
$ value = base64_decode ($ value); // Decrypt after decoding
$ value = $ myMcrypt-> do_mdecrypt ($ value);
$ value = trim ($ value);
echo "$ value <br length:".strlen($value)."<br/>";
RELATED links:
1190000000668272//php Common cryptographic functions, summary of good
http://www.cnblogs.com/hongfei/archive/2012/06/19/2555504.html//The first half of the concept is well explained
http://blog.csdn.net/zhang_red/article/details/39890539//Examples of actual use
http://blog.csdn.net/tsxw24/article/details/7644244//Concise version Example
Encrypt and decrypt with PHP's MCrypt module (AES, DES, etc.)