PHP 简单的加密解密算法

本文介绍了一个使用PHP实现的异或加密解密类。该类通过定义加密密钥,可以对输入的字符串进行加密和解密操作。文章详细展示了加密和解密的过程,并提供了完整的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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;
	}
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值