PHP 简单的加密解密算法

<?php
/**
*
* @创建时间:2015-3-12 下午2:07:33
* @作者:YuMing
* @描述:异或加密解密类
*/
class Yihuo extends CI_Controller { //加密秘钥
public $key0 = '123456'; function __construct() {
parent::__construct ();
header ( "Content-type:text/html;charset=utf-8" );
} public function index(){
$this->load->view('templates/header');
$this->load->view('yihuo');
$this->load->view('templates/footer');
} /**
*
* @创建时间:2015-3-12 下午2:06:47
* @作者:YuMing
* @描述:异或加密
* @param string $str
* @return string
*/
public function encode($str = '') { $data = $this->input->post();
if(!empty($data['msg'])){
$str =$data['msg'];
}
$keyBytes = $this->getBytes ( $this->key0 );
$bytes = $this->getBytes ( $str );
for($i = 0; $i < count ( $bytes ); $i ++) {
foreach ( $keyBytes as $keyBytes0 ) {
$bytes [$i] = $bytes [$i] ^ $keyBytes0;
}
} $encode_str= $this->toStr ( $bytes ); echo $encode_str;
return $encode_str;
}
/**
*
* @创建时间:2015-3-12 下午2:06:35
* @作者:YuMing
* @描述:异或解密
* @param unknown $str
* @return string
*/
public function decode($str ='') {
$data = $this->input->post();
if(!empty($data['msg'])){
$str =$data['msg'];
} $keyBytes = $this->getBytes ( $this->key0 );
$bytes = $this->getBytes ( $str );
for($i = 0; $i < count ( $bytes ); $i ++) {
foreach ( $keyBytes as $keyBytes0 ) {
$bytes[$i] = $bytes [$i] ^ $keyBytes0;
}
}
$decode_str= $this->toStr ( $bytes );
echo $decode_str;
return $decode_str;
}
/**
*
* @创建时间:2015-3-12 下午2:05:59
* @作者:YuMing
* @描述:将Bytes数组转换为String
* @param unknown $bytes
* @return string
*/
public static function toStr($bytes) {
$str = '';
foreach ( $bytes as $ch ) {
$str .= chr ( $ch );
}
return $str;
}
/**
*
* @创建时间:2015-3-12 下午2:06:18
* @作者:YuMing
* @描述:将String转换为Bytes数组
* @param unknown $string
* @return multitype:number
*/
public static function getBytes($string) {
$bytes = array ();
for($i = 0; $i < strlen ( $string ); $i ++) {
$bytes [] = ord ( $string [$i] );
}
return $bytes;
}
}

  

上一篇:ruby 查询mysql方法


下一篇:【Python】迭代器