当前位置:网站首页>PHP Basics - detailed explanation of DES encryption and decryption in PHP
PHP Basics - detailed explanation of DES encryption and decryption in PHP
2022-07-05 01:25:00 【Wild young man】
One 、 brief introduction
DES It's a common symmetric encryption , Its full name is Data Encryption Standard, Data encryption standard , Is a block algorithm using key encryption . The key length is 64 position (bit), Over digit key ignored . The so-called symmetric encryption means that the encryption and decryption keys are the same , Symmetric encryption generally follows a fixed length , Divide the string to be encrypted into blocks , Less than a whole block or at the end of which there are special padding characters .
Cross language DES Encryption and decryption often have problems , Often the filling method is wrong 、 The coding is inconsistent or the encryption and decryption mode is not corresponding .
- Fill mode :pkcs5、pkcs7、iso10126、ansix923、zero.
- Encryption mode :DES-ECB、DES-CBC、DES-CTR、DES-OFB、DES-CFB.
- The output type : No code ,base64 code ,hex code .
As a software developer , It can be tested by tools DES Encryption and decryption , Here's an online tool :http://tool.chacuo.net/cryptdes
Two 、 Realization
PHP Provides Mcrypt A series of functions to implement DES Encryption and decryption of , But the functions in this extension have been abandoned in succession , since PHP 7.2.0 rise , Will move to PECL.
So this code uses a more general OPENSSL How to achieve DES Encryption and decryption of , The specific implementation and use code are as follows :
<?php
/**
* Created by PhpStorm.
* Title:DES Encryption and decryption class
* User: Wanzhou Chen
* Date: 2022/04/24
* Time: 22:00
*/
namespace crypt;
class DES
{
const OUTPUT_NULL = '';
const OUTPUT_BASE64 = 'base64';
const OUTPUT_HEX = 'hex';
/**
* @var string $method Encryption and decryption methods , It can be done by openssl_get_cipher_methods() get
*/
protected $method;
/**
* @var string $key Encryption and decryption key
*/
protected $key;
/**
* @var string $output Output format nothing 、base64、hex
*/
protected $output;
/**
* @var string $iv Decrypted vector
*/
protected $iv;
/**
* @var string $options
*/
protected $options;
/**
* DES constructor.
* @param string $key
* @param string $method
* ECB: DES-ECB、DES-EDE3 ( by ECB Mode time ,$iv If it is empty, you can )
* CBC: DES-CBC、DES-EDE3-CBC、DESX-CBC
* CFB: DES-CFB8、DES-EDE3-CFB8
* CTR
* OFB
*
* @param string $output
* base64、hex
*
* @param string $iv
* @param int $options
*/
public function __construct($key, $method = 'DES-ECB', $output = self::OUTPUT_NULL, $iv = '', $options = OPENSSL_RAW_DATA | OPENSSL_NO_PADDING)
{
$this->key = $key;
$this->method = $method;
$this->output = $output;
$this->iv = $iv;
$this->options = $options;
}
/**
* encryption
*
* @param $str
* @return string
*/
public function encrypt($str)
{
$str = $this->padding($str, 8);
$sign = openssl_encrypt($str, $this->method, $this->key, $this->options, $this->iv);
if ($this->output == self::OUTPUT_BASE64) {
$sign = base64_encode($sign);
} else if ($this->output == self::OUTPUT_HEX) {
$sign = bin2hex($sign);
}
return $sign;
}
/**
* fill
*
* @param $str
* @param $blockSize
* @return string
* @internal param $blocksize
*/
private function padding($str, $blockSize)
{
$pad = $blockSize - (strlen($str) % $blockSize);
return $str . str_repeat(chr($pad), $pad);
}
/**
* Decrypt
*
* @param $encrypted
* @return string
*/
public function decrypt($encrypted)
{
if ($this->output == self::OUTPUT_BASE64) {
$encrypted = base64_decode($encrypted);
} else if ($this->output == self::OUTPUT_HEX) {
$encrypted = hex2bin($encrypted);
}
$sign = @openssl_decrypt($encrypted, $this->method, $this->key, $this->options, $this->iv);
$sign = $this->unPadding($sign);
$sign = rtrim($sign);
return $sign;
}
/**
* To fill in
*
* @param $str
* @return string
*/
private function unPadding($str)
{
$pad = ord($str{strlen($str) - 1});
if ($pad > strlen($str)) {
return false;
}
return substr($str, 0, -1 * $pad);
}
}
Class call
$key = 'key123456';
$iv = 'iv123456';
// DES CBC encryption
$des = new DES($key, 'DES-CBC', DES::OUTPUT_BASE64, $iv);
echo $base64Sign = $des->encrypt('Hello DES CBC');
echo "\n";
echo $des->decrypt($base64Sign);
echo "\n";
// DES ECB encryption
$des = new DES($key, 'DES-ECB', DES::OUTPUT_HEX);
echo $base64Sign = $des->encrypt('Hello DES ECB');
echo "\n";
echo $des->decrypt($base64Sign);
3、 ... and 、 Related links
1.DES Encryption and decryption tools : http://tool.chacuo.net/cryptdes
Mcrypt Official documents : http://php.net/manual/zh/book.mcrypt.php
OPENSSL Official document of encryption and decryption function :
openssl_encrypt: http://php.net/manual/zh/function.openssl-encrypt.php
openssl_decrypt: http://php.net/manual/zh/function.openssl-decrypt.php
边栏推荐
- C语音常用的位运算技巧
- 【FPGA教程案例10】基于Verilog的复数乘法器设计与实现
- 142. Circular linked list II
- Introduction to the gtid mode of MySQL master-slave replication
- 小程序直播 + 电商,想做新零售电商就用它吧!
- Take you ten days to easily complete the go micro service series (IX. link tracking)
- Arbitrum:二维费用
- [microprocessor] VHDL development of microprocessor based on FPGA
- Wechat applet: wechat applet source code download new community system optimized version support agent member system function super high income
- ROS command line tool
猜你喜欢
What happened to those who focused on automated testing?
【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
SAP UI5 应用开发教程之一百零六 - 如何提高 SAP UI5 应用路由 url 的可读性试读版
Basic operation of database and table ----- phased test II
phpstrom设置函数注释说明
Arbitrum:二维费用
Call Huawei order service to verify the purchase token interface and return connection reset
抓包整理外篇——————状态栏[ 四]
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
随机推荐
[microprocessor] VHDL development of microprocessor based on FPGA
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
Are you still writing the TS type code
Introduction to the gtid mode of MySQL master-slave replication
Check if this is null - checking if this is null
Database performance optimization tool
微信小程序:独立后台带分销功能月老办事处交友盲盒
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Poap: the adoption entrance of NFT?
[Chongqing Guangdong education] National Open University spring 2019 1042 international economic law reference questions
[wave modeling 3] three dimensional random real wave modeling and wave generator modeling matlab simulation
抓包整理外篇——————状态栏[ 四]
Introduction to redis (1)
There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
[FPGA tutorial case 9] design and implementation of clock manager based on vivado core
FEG founder rox:smartdefi will be the benchmark of the entire decentralized financial market
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
To sort out messy header files, I use include what you use
[CTF] AWDP summary (WEB)
Database postragesq PAM authentication