Des encryption is a symmetric encryption in the Internet application of a number of encryption methods, PHP through the MCrypt extension library to support DES encryption, to use DES Encryption in PHP, you need to install the MCrypt expansion Library first
The following is an instance of encryption decryption
Copy Code code as follows:
$iv _size = mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);
$iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
$key = "This is a very secret key";//Key
$text = "Meet me at one o ' clock behind the monument."; /Need encrypted content
Echo ($text). "\ n";
$crypttext =base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, $key, $text, MCRYPT_MODE_ECB, $iv));
Echo $crypttext. "\ n";/encrypted content
Echo Mcrypt_decrypt (mcrypt_rijndael_256, $key, Base64_decode ($crypttext), MCRYPT_MODE_ECB, $IV);/decrypted content
In the AES encryption algorithm is usually used in mcrypt_rijndael_128, mcrypt_rijndael_192, mcrypt_rijndael_256 three species, followed by 128, 192, 256 represents the secret key (that is, encryption key) is how many bit, such as the use of mcrypt_rijndael_128, then use this algorithm to encrypt the secret key length is 128bit, such as $key = ' fjjda0&9^$$#+*%$ Fada ', is 20 characters, that in the actual encryption when only use the first 16 characters of encryption (16*8=128), less than 128bit of PHP will be used ' ".
Sometimes when doing project docking, you may use PHP encryption, and the other side is written in Java, docking process to find the computer after the encryption of the contents of the other side can not decrypt, this is because PHP and Java in the implementation of this algorithm when there is a difference, to the correct encryption and decryption need to do both sides of the processing:
Php:
Copy Code code as follows:
<?php
Class Security {
public static function Encrypt ($input, $key) {
$size = Mcrypt_get_block_size (mcrypt_rijndael_128, MCRYPT_MODE_ECB);
$input = Security::p kcs5_pad ($input, $size);
$TD = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_ECB, ');
$iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);
Mcrypt_generic_init ($TD, $key, $IV);
$data = Mcrypt_generic ($TD, $input);
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
$data = Base64_encode ($data);
return $data;
}
private static function Pkcs5_pad ($text, $blocksize) {
$pad = $blocksize-(strlen ($text)% $blocksize);
Return $text. Str_repeat (Chr ($pad), $pad);
}
public static function Decrypt ($SSTR, $sKey) {
$decrypted = Mcrypt_decrypt (
mcrypt_rijndael_128,
$sKey,
Base64_decode ($SSTR),
Mcrypt_mode_ecb
);
$dec _s = strlen ($decrypted);
$padding = Ord ($decrypted [$dec _s-1]);
$decrypted = substr ($decrypted, 0,-$padding);
return $decrypted;
}
}
$key = "1234567891234567";
$data = "Example";
$value = Security::encrypt ($data, $key);
echo $value. ' <br/> ';
Echo Security::d Ecrypt ($value, $key);
Java:
Copy Code code as follows:
Import Javax.crypto.Cipher;
Import Javax.crypto.spec.SecretKeySpec;
Import org.apache.commons.codec.binary.Base64;
public class Security {
public static string encrypt (string input, string key) {
byte[] crypted = null;
try{
Secretkeyspec skey = new Secretkeyspec (Key.getbytes (), "AES");
Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");
Cipher.init (Cipher.encrypt_mode, skey);
crypted = Cipher.dofinal (Input.getbytes ());
}catch (Exception e) {
System.out.println (E.tostring ());
}
return new String (Base64.encodebase64 (crypted));
}
public static string decrypt (string input, string key) {
byte[] output = null;
try{
Secretkeyspec skey = new Secretkeyspec (Key.getbytes (), "AES");
Cipher Cipher = cipher.getinstance ("aes/ecb/pkcs5padding");
Cipher.init (Cipher.decrypt_mode, skey);
Output = cipher.dofinal (base64.decodebase64 (input));
}catch (Exception e) {
System.out.println (E.tostring ());
}
return new String (output);
}
public static void Main (string[] args) {
String key = "1234567891234567";
String data = "Example";
System.out.println (Security.encrypt (data, key));
System.out.println (Security.decrypt (Security.encrypt (data, key), key);
}
}