Home  >  Article  >  Backend Development  >  php des encryption and decryption example

php des encryption and decryption example

WBOY
WBOYOriginal
2016-07-25 08:42:091013browse

des encryption is an encryption method that is widely used on the Internet in symmetric encryption. PHP supports des encryption through the mcrypt extension library. To use des encryption in Php, you need to install the mcrypt extension library first


The following is an example of encryption and decryption


  1. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  2. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  3. $key = "This is a very secret key";//Key
  4. $text = "Meet me at 11 o'clock behind the monument.";//Content that needs to be encrypted
  5. echo ($text) . "n";
  6. $crypttext =base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));
  7. echo $crypttext . "n";//Encrypted content
  8. echo mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,base64_decode($crypttext),MCRYPT_MODE_ECB,$iv);//Decrypted content
Copy code


In the AES encryption algorithm, MCRYPT_RIJNDAEL_128, MCRYPT_RIJNDAEL_192, and MCRYPT_RIJNDAEL_256 are usually used. The following 128, 192, and 256 represent the number of bits of the secret key (that is, the encrypted Key). For example, if MCRYPT_RIJNDAEL_128 is used, then use The length of the secret key when encrypted by this algorithm is 128 bits. For example, $key = 'fjjda0&9^$$#+*%$fada' is 20 characters. In the actual encryption, only the first 16 characters are used for encryption (16* 8=128), will be used in PHP that is less than 128bit


Sometimes when doing project docking, you may be using Php encryption, while the other party is using Java. During the docking process, you will find that the encrypted content cannot be decrypted by the other party. This is because Php and Java are different. There are differences when implementing this algorithm. To correctly encrypt and decrypt, you need to do the following processing on both sides:

PHP:

    class Security {
  1. public static function encrypt($input, $key) {
  2. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  3. $input = Security::pkcs5_pad($ input, $size);
  4. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  5. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  6. mcrypt_generic_init($td, $key, $iv) ;
  7. $data = mcrypt_generic($td, $input);
  8. mcrypt_generic_deinit($td);
  9. mcrypt_module_close($td);
  10. $data = base64_encode($data);
  11. return $data;
  12. }
  13. private static function pkcs5_pad ($text, $blocksize) {
  14. $pad = $blocksize - (strlen($text) % $blocksize);
  15. return $text . str_repeat(chr($pad), $pad);
  16. }
  17. public static function decrypt($sStr, $sKey) {
  18. $decrypted= mcrypt_decrypt(
  19. MCRYPT_RIJNDAEL_128,
  20. $sKey,
  21. base64_decode($sStr),
  22. MCRYPT_MODE_ECB
  23. );
  24. $dec_s = strlen($decrypted);
  25. $padding = ord($decrypted[$dec_s-1]);
  26. $decrypted = substr($decrypted, 0, -$padding);
  27. return $decrypted;
  28. }
  29. }
  30. $key = "1234567891234567";
  31. $ data = "example";
  32. $value = Security::encrypt($data , $key );
  33. echo $value.'
    ';
  34. echo Security::decrypt($value, $key ) ;
Copy code
Java:
    import javax.crypto.Cipher;
  1. import javax.crypto.spec.SecretKeySpec;
  2. import org.apache.commons.codec.binary.Base64;
  3. public class Security {
  4. public static String encrypt (String input, String key){
  5. byte[] crypted = null;
  6. try{
  7. SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
  8. Cipher cipher = Cipher.getInstance("AES/ECB/ PKCS5Padding");
  9. cipher.init(Cipher.ENCRYPT_MODE, skey);
  10. crypted = cipher.doFinal(input.getBytes());
  11. }catch(Exception e){
  12. System.out.println(e.toString() );
  13. }
  14. return new String(Base64.encodeBase64(crypted));
  15. }
  16. public static String decrypt(String input, String key){
  17. byte[] output = null;
  18. try{
  19. SecretKeySpec skey = new SecretKeySpec (key.getBytes(), "AES");
  20. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  21. cipher.init(Cipher.DECRYPT_MODE, skey);
  22. output = cipher.doFinal(Base64. decodeBase64(input));
  23. }catch(Exception e){
  24. System.out.println(e.toString());
  25. }
  26. return new String(output);
  27. }
  28. public static void main(String[] args) {
  29. String key = "1234567891234567";
  30. String data = "example";
  31. System.out.println(Security.encrypt(data, key));
  32. System.out.println(Security.decrypt(Security .encrypt(data, key), key));
  33. }
  34. }
Copy code


Encryption and decryption, php, des


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn