php pkcs 7 填充解密,如何从AES加密字符串中添加/删除PKCS 7填充?

我创建了两个方法来执行填充和取消填充。使用phpdoc需要PHP 5。正如您注意到的,unpad函数包含大量异常处理,为每个可能的错误生成不少于4条不同的消息。

要获得PHP mcrypt的块大小,可以使用mcrypt_get_block_size,它还将块大小定义为字节而不是位。/**

* Right-pads the data string with 1 to n bytes according to PKCS#7,

* where n is the block size.

* The size of the result is x times n, where x is at least 1.

*

* The version of PKCS#7 padding used is the one defined in RFC 5652 chapter 6.3.

* This padding is identical to PKCS#5 padding for 8 byte block ciphers such as DES.

*

* @param string $plaintext the plaintext encoded as a string containing bytes

* @param integer $blocksize the block size of the cipher in bytes

* @return string the padded plaintext

*/function pkcs7pad($plaintext, $blocksize){

$padsize = $blocksize - (strlen($plaintext) % $blocksize);

return $plaintext . str_repeat(chr($padsize), $padsize);}/**

* Validates and unpads the padded plaintext according to PKCS#7.

* The resulting plaintext will be 1 to n bytes smaller depending on the amount of padding,

* where n is the block size.

*

* The user is required to make sure that plaintext and padding oracles do not apply,

* for instance by providing integrity and authenticity to the IV and ciphertext using a HMAC.

*

* Note that errors during uppadding may occur if the integrity of the ciphertext

* is not validated or if the key is incorrect. A wrong key, IV or ciphertext may all

* lead to errors within this method.

*

* The version of PKCS#7 padding used is the one defined in RFC 5652 chapter 6.3.

* This padding is identical to PKCS#5 padding for 8 byte block ciphers such as DES.

*

* @param string padded the padded plaintext encoded as a string containing bytes

* @param integer $blocksize the block size of the cipher in bytes

* @return string the unpadded plaintext

* @throws Exception if the unpadding failed

*/function pkcs7unpad($padded, $blocksize){

$l = strlen($padded);

if ($l % $blocksize != 0)

{

throw new Exception("Padded plaintext cannot be divided by the block size");

}

$padsize = ord($padded[$l - 1]);

if ($padsize === 0)

{

throw new Exception("Zero padding found instead of PKCS#7 padding");

}

if ($padsize > $blocksize)

{

throw new Exception("Incorrect amount of PKCS#7 padding for blocksize");

}

// check the correctness of the padding bytes by counting the occurance

$padding = substr($padded, -1 * $padsize);

if (substr_count($padding, chr($padsize)) != $padsize)

{

throw new Exception("Invalid PKCS#7 padding encountered");

}

return substr($padded, 0, $l - $padsize);}

这在任何方面都不会使Paŭlo Ebermann的答案失效,它基本上是代码&phpdoc中的相同答案,而不是描述。

请注意,向攻击者返回填充错误可能会导致填充甲骨文攻击它完全破坏了CBC(当使用CBC代替欧洲央行或安全认证密码时)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想要了解如何在 Spring Boot 使用 AES/CBC/PKCS7Padding 加解密字符串,我可以给您提供一些帮助。 首先,您需要使用 Java 的 `javax.crypto` 包来进行加解密操作。以下是一个示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.tomcat.util.codec.binary.Base64; public class AesUtil { private static final String ENCODING = "UTF-8"; private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/CBC/PKCS7Padding"; private static final String IV = "0123456789abcdef"; public static String encrypt(String content, String key) throws Exception { byte[] contentBytes = content.getBytes(ENCODING); byte[] keyBytes = key.getBytes(ENCODING); byte[] ivBytes = IV.getBytes(ENCODING); SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, ALGORITHM); IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv); byte[] encryptedBytes = cipher.doFinal(contentBytes); return Base64.encodeBase64String(encryptedBytes); } public static String decrypt(String content, String key) throws Exception { byte[] contentBytes = Base64.decodeBase64(content); byte[] keyBytes = key.getBytes(ENCODING); byte[] ivBytes = IV.getBytes(ENCODING); SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, ALGORITHM); IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv); byte[] decryptedBytes = cipher.doFinal(contentBytes); return new String(decryptedBytes, ENCODING); } } ``` 在上面的代码,我们使用 `Cipher` 类来进行加解密操作,其 `TRANSFORMATION` 变量指定了加密算法的名称和填充方式。`encrypt` 方法接受要加密的字符串和密钥作为参数,并返回加密后的字符串。`decrypt` 方法接受要解密字符串和密钥作为参数,并返回解密后的字符串。 请注意,为了确保加解密结果的一致性,必须使用相同的密钥和初始化向量(IV)进行加解密操作。在上面的代码,我们使用了一个硬编码的 IV 值,但在实际应用,应该使用一个随机生成的 IV 值,以增加加密的安全性。 希望这可以帮助您实现您的需求!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值