php中加密解密DES类的简单使用方法示例
作者:OldKind超 时间:2023-09-07 23:28:44
本文实例讲述了php中加密解密DES类的简单使用方法。分享给大家供大家参考,具体如下:
在平时的开发工作中,我们经常会对关键字符进行加密,可能为了安全 也可能为了规范,所以要正确使用DES加密解密
代码1:
class DES
{
var $key; // 密钥
var $iv; // 偏移量
function __construct( $key, $iv=0 ) {
$this->key = $key;
if( $iv == 0 ) {
$this->iv = $key;
} else {
$this->iv = $iv;
// 创建初始向量, 并且检测密钥长度, Windows 平台请使用 MCRYPT_RAND
// mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );
}
}
function encrypt($str) {
//加密,返回大写十六进制字符串
$size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
$str = $this->pkcs5Pad ( $str, $size );
// bin2hex 把 ASCII 字符的字符串转换为十六进制值
return strtoupper( bin2hex( mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv ) ) );
}
function decrypt($str) {
//解密
$strBin = $this->hex2bin( strtolower( $str ) );
$str = mcrypt_cbc( MCRYPT_DES, $this->key, $strBin, MCRYPT_DECRYPT, $this->iv );
$str = $this->pkcs5Unpad( $str );
return $str;
}
function hex2bin($hexData) {
$binData = "";
for($i = 0; $i < strlen ( $hexData ); $i += 2) {
$binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) );
}
return $binData;
}
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 );
}
}
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; DES5 has a deprecated constructor in D:\phpstudy_pro\WWW\des\DES5.php on line 2
Fatal error: Uncaught Error: Call to undefined function mcrypt_get_block_size() in D:\phpstudy_pro\WWW\des\DES5.php:19 Stack trace: #0 D:\phpstudy_pro\WWW\des\1.php(10): DES5->encrypt('podsmia') #1 {main} thrown in D:\phpstudy_pro\WWW\des\DES5.php on line 19
mcrypt_cbc 以 CBC 模式加解密数据, 在PHP 5.5.0+被弃用, PHP 7.0.0被移除
mcrypt_encrypt / mcrypt_decrypt 使用给定参数加密 / 解密, 在PHP 7.1.0+被弃用, 在PHP 7.2.0+被移除
代码2:
class DES7
{
//要改的加密, 使用 openssl
public function desEncrypt($str,$key) {
$iv = $key;
$data = openssl_encrypt($str,"DES-CBC",$key,OPENSSL_RAW_DATA,$iv);
$data = strtolower(bin2hex($data));
return $data;
}
//要改的解密
public function desDecrypt($str,$key) {
$iv = $key;
return openssl_decrypt (hex2bin($str), 'DES-CBC', $key, OPENSSL_RAW_DATA,$iv);
}
}
希望本文所述对大家PHP程序设计有所帮助。
来源:https://blog.csdn.net/qq_36261130/article/details/104015050
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL中USING 和 HAVING 用法实例简析
MySQL设置global变量和session变量的两种方法详解
![](https://img.aspxhome.com/file/2023/9/111379_0s.png)
最强大最好最全的javascript 验证表单的例子
pytorch::Dataloader中的迭代器和生成器应用详解
python实现根据ip地址反向查找主机名称的方法
django template实现定义临时变量,自定义赋值、自增实例
一个二级伸缩下拉菜单代码
MySQL的双机热备份安装和配置
详解python实现多张多格式图片转PDF并打包成exe
![](https://img.aspxhome.com/file/2023/4/80774_0s.png)
SQL Server 2016的数据库范围内的配置详解
![](https://img.aspxhome.com/file/2023/5/94445_0s.png)
Pycharm以root权限运行脚本的方法
![](https://img.aspxhome.com/file/2023/0/107110_0s.jpg)
Python分割单词和转换命名法的实现
Mysql常用运算符与函数汇总
利用Vue实现一个markdown编辑器实例代码
![](https://img.aspxhome.com/file/2023/3/130103_0s.png)
Pandas缺失值填充 df.fillna()的实现
![](https://img.aspxhome.com/file/2023/3/95903_0s.png)
Python简单实现区域生长方式
python实现excel公式格式化的示例代码
![](https://img.aspxhome.com/file/2023/6/124776_0s.png)
YiiFramework入门知识点总结(图文教程)
![](https://img.aspxhome.com/file/2023/4/88274_0s.jpg)
详解PHP如何更好的利用PHPstorm的自动提示
mysql 5.7版本修改密码的简单方法
![](https://img.aspxhome.com/file/2023/1/77221_0s.png)