PHP, Java des encryption and decryption instance _php tutorial

Source: Internet
Author: User
Tags decrypt mcrypt

PHP, Java des encryption and decryption instances


This article mainly introduces the PHP, Java des Encryption and decryption instances, DES encryption is symmetric encryption in the Internet application of a more than one encryption method, this article gives the PHP and Java version of the implementation code, the need for friends can refer to the next

Des encryption is a symmetric encryption in the Internet application of a more than one encryption method, PHP through the MCrypt extension library to support DES encryption, to use DES Encryption in PHP, the need to first install the MCrypt extension Library

The following is an instance of encrypted 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."; /content that needs to be encrypted

Echo ($text). "\ n";

$crypttext =base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, $key, $text, MCRYPT_MODE_ECB, $iv));

Echo $crypttext. "\ n";//post-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, the following 128, 192, 256 is the secret key (that is, the encryption key) How many bits, such as using 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 only use the first 16 character encryption (16*8=128), less than 128bit PHP will use ' "".

Sometimes do project docking time, you may use PHP encryption, and the other side is written by Java, docking the process of the discovery machine after the encryption of the content 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 right 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));

}

}

http://www.bkjia.com/PHPjc/990991.html www.bkjia.com true http://www.bkjia.com/PHPjc/990991.html techarticle PHP, Java des Encryption and decryption examples This article mainly introduces the PHP, Java des Encryption and decryption instances, DES encryption is symmetric encryption in the Internet application of a more than a more encryption method, the paper, respectively ...

  • Related Article

    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.