加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 搜索优化 > 正文

des加密解密的操作流程

发布时间:2022-06-08 01:16:55 所属栏目:搜索优化 来源:互联网
导读:des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库 下面是加密解密的步骤 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv = mcrypt_c
  des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库
 
  下面是加密解密的步骤
 
  $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";//密钥
 
  $text = "Meet me at 11 o'clock behind the monument.";//需要加密的内容
 
  echo ($text) . " ";
 
  $crypttext =base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));
 
  echo $crypttext . " ";//加密后的内容
 
  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中会用''来补齐。
 
  有的时候做项目对接的时候,可能你用的是Php加密的,而对方用的是java写的,对接的过程中就发现机加密后的内容对方解密不了,这是因为Php跟java在实现这个算法的时候有差别,要想正确加密解密需要两边都做下处理:
 
  PHP:
 
  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:
 
  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));
 
  复制代码。
 

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!