您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > PHP >

DES加密和解密PHP等语言的方法

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 09:56 点击:

PHP的加解密函数

class DesComponent {
 var $key = 12345678;

 function encrypt($string) {

  $ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);
  $iv=null;
  foreach ($ivArray as $element)
   $iv.=CHR($element);


   $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC ); 
       $string = $this->pkcs5Pad ( $string, $size ); 

  $data =  mcrypt_encrypt(MCRYPT_DES, $this->key, $string, MCRYPT_MODE_CBC, $iv);

  $data = base64_encode($data);
  return $data;
 }

 function decrypt($string) {

  $ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);
  $iv=null;
  foreach ($ivArray as $element)
   $iv.=CHR($element);

  $string = base64_decode($string);
  //echo("****");
  //echo($string);
  //echo("****");
  $result =  mcrypt_decrypt(MCRYPT_DES, $this->key, $string, MCRYPT_MODE_CBC, $iv);
   $result = $this->pkcs5Unpad( $result ); 

  return $result;
 }
 
 
  function pkcs5Pad($text, $blocksize) 
    { 
        $pad = $blocksize - (strlen ( $text ) % $blocksize); 
        return $text . str_repeat ( chr ( $pad ), $pad ); 
    } 
 
    function pkcs5Unpad($text) 
    { 
        $pad = ord ( $text {strlen ( $text ) - 1} ); 
        if ($pad > strlen ( $text )) 
            return false; 
        if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) 
            return false; 
        return substr ( $text, 0, - 1 * $pad ); 
    } 
 
}


$des = new DesComponent();
echo ($des->encrypt("19760519"));
echo "";

//die($des->decrypt("zLVdpYUM0qw="));
//die($des->decrypt("zLVdpYUM0qzEsNshEEI6Cg=="));

$t2 =$des->decrypt("zLVdpYUM0qw=");
echo $t2;
echo "--";
echo strlen($t2);
echo is_utf8($t2);


echo "";
$t3 = mb_convert_encoding($t2,"GB2312", "utf-8");
echo $t3;
echo "--";
echo strlen($t3);
echo is_utf8($t3);


echo "";


$t1 =$des->decrypt("zLVdpYUM0qzEsNshEEI6Cg==");
echo $t1;
echo "--";
echo strlen($t1);
echo is_utf8($t1);

echo "";
$t3 = mb_convert_encoding($t1, "utf-8","GB2312");
echo $t3;
echo "--";
echo strlen($t3);
echo is_utf8($t3);

function is_utf8($string) {
return preg_match(%^(?:
[x09x0Ax0Dx20-x7E] # ASCII
| [xC2-xDF][x80-xBF] # non-overlong 2-byte
| xE0[xA0-xBF][x80-xBF] # excluding overlongs
| [xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte
| xED[x80-x9F][x80-xBF] # excluding surrogates
| xF0[x90-xBF][x80-xBF]{2} # planes 1-3
| [xF1-xF3][x80-xBF]{3} # planes 4-15
| xF4[x80-x8F][x80-xBF]{2} # plane 16
)*$%xs, $string);
}
?>

Java的加解密函数

package ghj1976.Demo;


/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICEN

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • PHP测试
      • 十天学会php之第六天
      • 几种显示数据的方法的比较
      • 使用xmlhttp为网站增加域名查询功能
      • PHP+MYSQL+Javascript数据库查询结果的动态显示
      • 查找数组中指定键名的值
      • 用redis实现跨服务器session
      • 用新浪微博接口发送图片微博失败的原因
      • smarty局部缓存技术[源码分析]
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1