php des 加密解密实例

原创
2016-07-25 08:42:09 746浏览

des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库


下面是加密解密的实例


  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";//密钥
  4. $text = "Meet me at 11 o'clock behind the monument.";//需要加密的内容
  5. echo ($text) . "\n";
  6. $crypttext =base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));
  7. echo $crypttext . "\n";//加密后的内容
  8. echo mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,base64_decode($crypttext),MCRYPT_MODE_ECB,$iv);//解密后的内容
复制代码


在AES加密算法中通常会用到MCRYPT_RIJNDAEL_128、MCRYPT_RIJNDAEL_192、MCRYPT_RIJNDAEL_256三种,后面的128、192、256代表的是秘钥(也就是加密的Key)是多少bit的,比如使用的是MCRYPT_RIJNDAEL_128,那么用这个算法加密时秘钥长度就是128bit的,比如 $key = 'fjjda0&9^$$#+*%$fada',是20个字符,那在实际加密的时候只用到前16个字符加密(16*8=128),不足128bit的php中会用'\0'来补齐。


有的时候做项目对接的时候,可能你用的是Php加密的,而对方用的是java写的,对接的过程中就发现机加密后的内容对方解密不了,这是因为Php跟java在实现这个算法的时候有差别,要想正确加密解密需要两边都做下处理:

PHP:

  1. class Security {
  2. public static function encrypt($input, $key) {
  3. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  4. $input = Security::pkcs5_pad($input, $size);
  5. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  6. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  7. mcrypt_generic_init($td, $key, $iv);
  8. $data = mcrypt_generic($td, $input);
  9. mcrypt_generic_deinit($td);
  10. mcrypt_module_close($td);
  11. $data = base64_encode($data);
  12. return $data;
  13. }
  14. private static function pkcs5_pad ($text, $blocksize) {
  15. $pad = $blocksize - (strlen($text) % $blocksize);
  16. return $text . str_repeat(chr($pad), $pad);
  17. }
  18. public static function decrypt($sStr, $sKey) {
  19. $decrypted= mcrypt_decrypt(
  20. MCRYPT_RIJNDAEL_128,
  21. $sKey,
  22. base64_decode($sStr),
  23. MCRYPT_MODE_ECB
  24. );
  25. $dec_s = strlen($decrypted);
  26. $padding = ord($decrypted[$dec_s-1]);
  27. $decrypted = substr($decrypted, 0, -$padding);
  28. return $decrypted;
  29. }
  30. }
  31. $key = "1234567891234567";
  32. $data = "example";
  33. $value = Security::encrypt($data , $key );
  34. echo $value.'
    ';
  35. echo Security::decrypt($value, $key );
复制代码

Java:
  1. import javax.crypto.Cipher;
  2. import javax.crypto.spec.SecretKeySpec;
  3. import org.apache.commons.codec.binary.Base64;
  4. public class Security {
  5. public static String encrypt(String input, String key){
  6. byte[] crypted = null;
  7. try{
  8. SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
  9. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  10. cipher.init(Cipher.ENCRYPT_MODE, skey);
  11. crypted = cipher.doFinal(input.getBytes());
  12. }catch(Exception e){
  13. System.out.println(e.toString());
  14. }
  15. return new String(Base64.encodeBase64(crypted));
  16. }
  17. public static String decrypt(String input, String key){
  18. byte[] output = null;
  19. try{
  20. SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
  21. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  22. cipher.init(Cipher.DECRYPT_MODE, skey);
  23. output = cipher.doFinal(Base64.decodeBase64(input));
  24. }catch(Exception e){
  25. System.out.println(e.toString());
  26. }
  27. return new String(output);
  28. }
  29. public static void main(String[] args) {
  30. String key = "1234567891234567";
  31. String data = "example";
  32. System.out.println(Security.encrypt(data, key));
  33. System.out.println(Security.decrypt(Security.encrypt(data, key), key));
  34. }
  35. }
复制代码


加密解密, php, des


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:PHP UTF8字符集内简体和繁体互转 下一条:php 判断访问IP

相关文章

查看更多