PHP, Java des encryption decryption instance

Source: Internet
Author: User
Tags base64 decrypt mcrypt php and strlen

This article mainly introduces PHP, Java des Encryption and decryption instance, DES encryption is symmetric encryption in the Internet application of a relatively many encryption methods, this article gives the PHP and Java version of the implementation code, the need for friends can refer to the

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

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 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 kinds, the following 128, 192, 256 is the secret key (that is, encrypted key) 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 to complement.

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:

The code is as follows:

  

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. '
';

Echo Security::d Ecrypt ($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);

}

}

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.