Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

API接口加密方法

$
0
0

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

加密方法:

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

1.AES加密参数:

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

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

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

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

加密步骤: 对数据进行AES加密。 对AES加密后的数据进行Base64加密。 3.加密示例:

1)原始数据:“hello world”

2)AES加密后数据:“bH G:9i_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接口加密方案设计


Viewing all articles
Browse latest Browse all 12749

Trending Articles