API接口加密方法

栏目: 编程工具 · 发布时间: 5年前

内容简介:为了防止提交到接口的明文泄密,可以对提交到接口的数据加密,可以用AES加密算法。微信公众平台官方API接口就是采用此算法。所有提交过来的数据都使用加密模式:AES-128-ECB (

为了防止提交到接口的明文泄密,可以对提交到接口的数据加密,可以用AES加密算法。微信公众平台官方API接口就是采用此算法。

加密方法:

所有提交过来的数据都使用 AES 加密算法+B ase64 算法加密:

1.AES加密参数:

加密模式:AES-128-ECB ( 可用更安全的aes-128-cbc-微信公众平台在用 ))

向量iv:空 (aes-128-cbc时需要)

密钥Key:“123456789”(请勿外泄)

填充:无( 微信公众平台用的是PKCS7填充

  1. 加密步骤:
  • 对数据进行AES加密。
  • 对AES加密后的数据进行Base64加密。

3.加密示例:

1)原始数据:“hello world”

2)AES加密后数据:“bH� �G:9�i_x0005_��”

3)Base64加密后数据:“YkilCuxHOgY5Bv9pGgXcwA==”

代码:

<?php
//$key previously generated safely, ie: openssl_random_pseudo_bytes
$key='123456789';
 
  
$plaintext="hello world";
 //$cipher = "aes-128-cbc";
 $cipher = "AES-128-ECB";

if (in_array($cipher, openssl_get_cipher_methods()))
{
     
     
    // $iv='1111111111111111';
    $iv='';
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA, $iv);
    $ciphertext =base64_encode($ciphertext);
    //store $cipher, $iv, and $tag for decryption later
    $original_plaintext = openssl_decrypt(base64_decode($ciphertext), $cipher, $key, OPENSSL_RAW_DATA, $iv);


    var_dump( $original_plaintext);
    var_dump( $ciphertext);
}

另可见 http://blog.p2hp.com/archives/5446

基于JWE的API接口加密方案设计


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning PHP and MySQL 5

Beginning PHP and MySQL 5

W. Jason Gilmore / Apress / 2006-01-23 / USD 44.99

Beginning PHP and MySQL 5: From Novice to Professional, Second Edition offers comprehensive information about two of the most prominent open source technologies on the planet: the PHP scripting langua......一起来看看 《Beginning PHP and MySQL 5》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码