PHP and Javades encryption and decryption instances, and des encryption and decryption. For PHP and Javades encryption and decryption instances, des encryption and decryption des encryption is a more popular encryption method for Internet applications in symmetric encryption. php supports des encryption through the mcrypt Extension Library, to encrypt and decrypt instances in PHP and Java des, des encryption and decryption
Des encryption is an encryption method that involves many Internet applications in symmetric encryption. php uses the mcrypt extension library to support des encryption and uses des encryption in Php, install the mcrypt Extension Library first.
The following is an example of encryption and decryption.
The code is 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 11 o 'clock behind the monument."; // the content to be encrypted
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, MCRYPT_RIJNDAEL_128, MCRYPT_RIJNDAEL_192, and MCRYPT_RIJNDAEL_256 are usually used. the values 128, 192, and 256 indicate the bit of the Key (that is, the encrypted Key, for example, if MCRYPT_RIJNDAEL_128 is used, the key length is BITs. for example, $ key = 'fjda0 & 9 ^ $ # + * % $ fada ', it is a string of 20 characters. in actual encryption, only the first 16 characters are used for encryption (16*8 = 128). in php with less than bits, '\ 0' is used to complete the encryption.
Sometimes you may use Php encryption for project interconnection, while the other party uses java to write data, during the connection process, it is found that the encrypted content cannot be decrypted by the other party. this is because Php and java differ in the implementation of this algorithm. to correct encryption and decryption, both sides must perform the following operations:
PHP:
The code is as follows:
<? Php
Class Security {
Public static function encrypt ($ input, $ key ){
$ Size = mcrypt_get_block_size (MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB );
$ Input = Security: pkcs5_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 .'
';
Echo Security: decrypt ($ value, $ key );
Java:
The code is 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 ));
}
}
For example, des encryption and decryption. des encryption and decryption is a type of encryption that is widely used in Internet applications in symmetric encryption. php supports des encryption through the mcrypt Extension Library, to be in...