AES加密

42 篇文章 0 订阅

PHP

调用

$linkPhone = 'URPsQ8ZOZDWtkqT7HhOiKw==';
$privateKey = 'asiainfoIIS20182';
$iv =   "asiainfoIIS20182";
$AES = new AES($privateKey, $iv);
$decrypted = $AES->decrypt($linkPhone);
x$JH = $AES->encrypt('18885000138');
echo $JH.'|'.$decrypted;

代码

<?php
namespace App\Http\Controllers;


class Aes
{
    /**
     * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得
     */
    protected $method;

    /**
     * var string $secret_key 加解密的密钥
     */
    protected $secret_key;

    /**
     * var string $iv 加解密的向量,有些方法需要设置比如CBC
     */
    protected $iv;

    /**
     * var string $options (不知道怎么解释,目前设置为0没什么问题)
     */
    protected $options;

    /**
     * 构造函数
     *
     * @param string $key 密钥
     * @param string $method 加密方式
     * @param string $iv iv向量
     * @param mixed $options 还不是很清楚
     *
     */
    public function __construct($key, $iv = '', $method = 'AES-128-CBC', $options = 0)
    {
        // key是必须要设置的
        $this->secret_key = isset($key) ? $key : 'morefun';

        $this->method = $method;

        $this->iv = $iv;

        $this->options = $options;
    }

    /**
     * 加密方法,对数据进行加密,返回加密后的数据
     *
     * @param string $data 要加密的数据
     *
     * @return string
     *
     */
    public function encrypt($data)
    {
        return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }

    /**
     * 解密方法,对数据进行解密,返回解密后的数据
     *
     * @param string $data 要解密的数据
     *
     * @return string
     *
     */
    public function decrypt($data)
    {
        return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }
}

JS

调用

var ki = "asiainfoIIS20182";
//aes加密
function encrypt(word) {
    var key = CryptoJS.enc.Utf8.parse(ki); //16位
    var iv = CryptoJS.enc.Utf8.parse(ki);
    var encrypted = '';
    var srcs = CryptoJS.enc.Utf8.parse(word);
    encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    //return encrypted.ciphertext.toString();
    return encrypted.toString();
}

代码

安装或者引入CryptoJS,百度一大堆
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值