• php-AES/CBC/PKCS7Padding加密的实现


    php5模式

    https://github.com/gunnzhao/AES-CBC-PKCS7Padding-/blob/master/AesCrypter.php

    public function encrypt($orig_data) {
            $encrypter = mcrypt_module_open($this->algorithm, '',
                $this->mode, '');
            $orig_data = $this->pkcs7padding(
                $orig_data, mcrypt_enc_get_block_size($encrypter)
            );
            mcrypt_generic_init($encrypter, $this->key, substr($this->key, 0, 16));
            $ciphertext = mcrypt_generic($encrypter, $orig_data);
            mcrypt_generic_deinit($encrypter);
            mcrypt_module_close($encrypter);
            return base64_encode($ciphertext);
        }
    

      

    php7模式:

    $cipher = "aes-256-cbc";
            $key = hash('sha256',   '@parkingwang.com' , true);
            $encrypted = openssl_encrypt(json_encode($data), 'AES-256-CBC', $key, 1, substr($key, 0, 16));
            $encrypt_msg = base64_encode($encrypted);
    

      

    注意:加密后的字节码使用Base64转换成字符串

    • 加密模式: CBC
    • 填充模式: PKCS7Padding
    • 加密密钥: 用户密钥 SHA256 的32 bytes
    • AES IV : 加密密钥的前 16 bytes
    • Base 64: Base64.DEFAULT

    加密过程:

    加密:padding->CBC加密->base64编码

    解密:base64解码->CBC解密->unpadding

    AES加密结果基准测试:

    用户密钥:

    909ed2d5fcf907c79fb9aa341a98febb65291c39
    

    明文:

    AABBCC测试数据
    

    密文:

    noMrTUS2A0YTcYaaPQSy9peqF6Mv/faMkI4yYHDvKjw=
  • 相关阅读:
    redis.conf
    redis 超全的操作
    Oracle数据库导入导出命令
    技巧总结
    C#把Object对象转换成JSON串
    vscode c++ 开发环境踩坑
    vscode python 开发环境+qgis开发
    算法设计与分析:贪心算法
    操作系统概念习题
    贪心算法:贪心选择性与优化子结构
  • 原文地址:https://www.cnblogs.com/akidongzi/p/9608022.html
Copyright © 2020-2023  润新知