PHP and Java des encryption and decryption instances

Source: Internet
Author: User
Tags mcrypt

PHP and Java des encryption and decryption instances

This article mainly introduces PHP and Java des encryption and decryption instances. des encryption is one of the most popular encryption methods for Internet applications in symmetric encryption, this article provides implementation code for PHP and JAVA versions respectively. For more information, see

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:

  

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 ));

}

}

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.