用于DES / CBC / PKCS5Padding加密和解密的PHP脚本 [英] PHP script for DES/CBC/ with PKCS5Padding encryption and decryption

查看:851
本文介绍了用于DES / CBC / PKCS5Padding加密和解密的PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果添加了PKCS#5填充,我想知道以下代码?如果没有添加?

  $ message =insert plaintext message here; 

$ iv = pack('H *','insert hex iv here');
$ key = pack('H *','insert hex key here');

$ enc = mcrypt_encrypt(MCRYPT_DES,$ key,$ message,MCRYPT_MODE_CBC,$ iv);

echo bin2hex($ enc);

我还想创建一个PHP代码来解密使用DES / CBC / PKCS5Padding创建的字符串。我认为上述代码可以被修改以获得解密。



对我来说重要的是获得PKCS#5 Padding和Unpadding脚本。

解决方案

不,没有添加。不幸的是,PHP / mcrypt使用零填充,直到消息是块大小的N倍。



要添加PKCS#5填充,请使用以下公式:

  p = b  -  l%b 

其中 l 是消息长度, b 是块大小,是余下的操作。然后在执行加密之前添加 p 字节,值为$ code> p 。


I would like to know in the following code if PKCS#5 padding is added ? If not how to add ?

$message = "insert plaintext message here";

$iv  = pack('H*', 'insert hex iv here');
$key = pack('H*', 'insert hex key here');

$enc = mcrypt_encrypt(MCRYPT_DES, $key, $message, MCRYPT_MODE_CBC, $iv);

echo bin2hex($enc);

I also want to create a PHP code to decrypt a string created with DES/CBC/PKCS5Padding. I think the above mentioned code can be modified to get a decryption.

The important thing for me is to get the PKCS#5 Padding and Unpadding script.

解决方案

No, it is not added. Unfortunately PHP / mcrypt uses zero padding until the message is N times the block size.

To add PKCS#5 padding, use the formula:

p = b - l % b

Where l is the message length, b is the block size and % is the remainder operation. Then add p bytes with the value p to the end before performing the encryption.

这篇关于用于DES / CBC / PKCS5Padding加密和解密的PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆