PHP, and phpdes. PHP encryption and decryption functions and DES encryption and decryption instances. phpdes encryption and decryption instances this article describes the application of the encryption and decryption functions in PHP and DES encryption and decryption. PHP encryption and decryption function and DES encryption and decryption instance, phpdes encryption and decryption instance
This article describes the application of the encryption and decryption function in PHP and DES encryption and decryption, and shares it with you for your reference. The details are as follows:
Example: example of php encryption and decryption
Encryption function:
The code is as follows:
/*
* Function: encrypt strings.
* Parameter 1: content to be encrypted
* Parameter 2: Key
*/
Function passport_encrypt ($ str, $ key) {// encryption function
Srand (double) microtime () * 1000000 );
$ Encrypt_key = md5 (rand (0, 32000 ));
$ Ctr = 0;
$ Tmp = '';
For ($ I = 0; $ I $ Ctr = strlen ($ encrypt_key )? 0: $ ctr;
$ Tmp. = $ encrypt_key [$ ctr]. ($ str [$ I] ^ $ encrypt_key [$ ctr ++]);
}
Return base64_encode (passport_key ($ tmp, $ key ));
}
The decryption code is as follows:
The code is as follows:
/*
* Function: decrypts strings.
* Parameter 1: ciphertext to be decrypted
* Parameter 2: Key
*/
Function passport_decrypt ($ str, $ key) {// decrypt the function
$ Str = passport_key (base64_decode ($ str), $ key );
$ Tmp = '';
For ($ I = 0; $ I $ Md5 = $ str [$ I];
$ Tmp. = $ str [++ $ I] ^ $ md5;
}
Return $ tmp;
}
Auxiliary functions:
The code is as follows:
/*
* Auxiliary functions
*/
Function passport_key ($ str, $ encrypt_key ){
$ Encrypt_key = md5 ($ encrypt_key );
$ Ctr = 0;
$ Tmp = '';
For ($ I = 0; $ I $ Ctr = strlen ($ encrypt_key )? 0: $ ctr;
$ Tmp. = $ str [$ I] ^ $ encrypt_key [$ ctr ++];
}
Return $ tmp;
}
Use
The code is as follows:
$ Str = 'Author: www.jb51.net ';
$ Key = '000000 ';
$ Encrypt = passport_encrypt ($ str, $ key );
$ Decrypt = passport_decrypt ($ encrypt, $ key );
Echo 'original: ', $ str ."
";
Echo 'ciphertext: ', $ encrypt ."
";
Echo 'Translation: ', $ decrypt ."
";
The specific code of the DES encryption and decryption function is as follows:
The code is as follows:
<? Php
Class DES
{
Var $ key;
Var $ iv; // Offset
Function DES ($ key, $ iv = 0 ){
// The key length is 8, for example, 1234 abcd.
$ This-> key = $ key;
If ($ iv = 0 ){
$ This-> iv = $ key;
} Else {
$ This-> iv = $ iv; // mcrypt_create_iv (mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );
}
}
Function encrypt ($ str ){
// Encrypted, returns an uppercase hexadecimal string
$ Size = mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC );
$ Str = $ this-> pkcs5Pad ($ str, $ size );
Return strtoupper (bin2hex (mcrypt_cbc (MCRYPT_DES, $ this-> key, $ str, MCRYPT_ENCRYPT, $ this-> iv )));
}
Function decrypt ($ str ){
// Decrypt
$ StrBin = $ this-> hex2bin (strtolower ($ str ));
$ Str = mcrypt_cbc (MCRYPT_DES, $ this-> key, $ strBin, MCRYPT_DECRYPT, $ this-> iv );
$ Str = $ this-> pkcs5Unpad ($ str );
Return $ str;
}
Function hex2bin ($ hexData ){
$ BinData = "";
For ($ I = 0; $ I <strlen ($ hexData); $ I + = 2 ){
$ BinData. = chr (hexdec (substr ($ hexData, $ I, 2 )));
}
Return $ binData;
}
Function pkcs5Pad ($ text, $ blocksize ){
$ Pad = $ blocksize-(strlen ($ text) % $ blocksize );
Return $ text. str_repeat (chr ($ pad), $ pad );
}
Function pkcs5Unpad ($ text ){
$ Pad = ord ($ text {strlen ($ text)-1 });
If ($ pad> strlen ($ text ))
Return false;
If (strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad)
Return false;
Return substr ($ text, 0,-1 * $ pad );
}
}
?>
The test results are as follows:
The code is as follows:
$ Str = '000000 ';
$ Key = '1234abcd ';
$ Crypt = new DES ($ key );
$ Mstr = $ crypt-> encrypt ($ str );
$ Str = $ crypt-> decrypt ($ mstr );
Echo $ str. '<=>'. $ mstr;
Example 2
The code is as follows:
The code is as follows:
/**
* Parameters that need to be passed in the url through the get method for encryption
*/
Function args_encode ($ data ){
If (is_array ($ data )){
$ String = http_build_query ($ data );
Return base64_encode ($ string );
} Else {
Return false;
}
}
/**
* Get the parameters passed by the get method in the url
*/
Function getargs (){
$ String = base64_decode ($ _ GET ['args ']);
Parse_str ($ string, $ g );
Return $ g;
}
I hope this article will help you with PHP programming.
Php encryption and decryption functions
Base64_encode plus
Base64_decode
Php des encryption function
The two functions are as follows:
Encryption Function: encrypt
Function encrypt ($ encrypt, $ key = ""){
$ Iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND );
$ Passcrypt = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ encrypt, MCRYPT_MODE_ECB, $ iv );
$ Encode = base64_encode ($ passcrypt );
Return $ encode;
}
Decryption function: decrypt
Function decrypt ($ decrypt, $ key = ""){
$ Decoded = base64_decode ($ decrypt );
$ Iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND );
$ Decrypted = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ key, $ decoded, MCRYPT_MODE_ECB, $ iv );
Return $ decrypted;
}
Examples in this article describe the application of the encryption and decryption function in PHP and DES encryption and decryption, and share it with you for your reference ....