去发现生活中的美好,记录生活中的点点滴滴

php实现aes加解密及与其他语言(python|java)互通

php admin 111℃

php aes加解密:

header("Content-Type: text/html;charset=utf-8");
$data = $_POST['aes_en'] ?? '';
$type = $_POST['type'] ?? '0';

$key = 'xxxxxxxxxxxxxxxxxxxxxxxx';//AES-128:16位密钥key   AES-192:24位密钥key   AES-256:32位密钥key
$iv  = '0000000000000000'; // 偏移量 16
if(!empty($data)){
    if ($type == 0){
        // 加密
        //0,OPENSSL_RAW_DATA=1,OPENSSL_ZERO_PADDING=2,OPENSSL_NO_PADDING=3
        $encrypt = base64_encode(openssl_encrypt($data, 'AES-192-CBC', $key, OPENSSL_RAW_DATA, $iv));
    }else{
        //解密
        $encrypt = openssl_decrypt(base64_decode($data), 'AES-192-CBC', $key, OPENSSL_RAW_DATA, $iv);
    }
}

java aes加解密:

public class AES {
    public static void main(String[] args) throws Exception {
        String aes = aesEncrypt("{}", "xxxxxxxxxxxxxxxxxxxxxxxx", "UTF-8");
        System.out.println(aes);
    }
    public static String aesEncrypt(String content, String aesKey, String charset) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(initIv());
            cipher.init(1, new SecretKeySpec(aesKey.getBytes(), "AES"), iv);
            byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
            return new String(Base64.encodeBase64(encryptBytes));
        } catch (Exception var6) {
            throw new Exception("AES加密失败", var6);
        }
    }
    private static byte[] initIv() {
        String vi = "0000000000000000";
        return vi.getBytes();
    }

python aes 加解密:

# coding=utf8
from Crypto.Cipher  import AES
import base64


# 密钥(key), 密斯偏移量(iv) CBC模式加密

BLOCK_SIZE = 16  # Bytes
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * \
                chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]

vi = '0000000000000000'

def AES_Encrypt(key, data):
    data = pad(data)
    # 字符串补位
    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
    encryptedbytes = cipher.encrypt(data.encode('utf8'))
    # 加密后得到的是bytes类型的数据,使用Base64进行编码,返回byte字符串
    encodestrs = base64.b64encode(encryptedbytes)
    # 对byte字符串按utf-8进行解码
    enctext = encodestrs.decode('utf8')
    return enctext


def AES_Decrypt(key, data):
    data = data.encode('utf8')
    encodebytes = base64.decodebytes(data)
    # 将加密数据转换位bytes类型数据
    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
    text_decrypted = cipher.decrypt(encodebytes)
    # 去补位
    text_decrypted = unpad(text_decrypted)
    text_decrypted = text_decrypted.decode('utf8')
    print(text_decrypted)
    return text_decrypted

if __name__ == '__main__':
    key = 'xxxxxxxxxxxxxxxxxxxxxxxx'

    data = '{"header":{"appVersion":"4.0.2","cmdId":0,"platformVersion":"4.0.2","cmdName":"h5_itou","userType":1,"uuid":"ztsnfm128we9k155tv88lr46xu1352yaf1xp","userID":"","platformCode":"h5mobile","token":""},"body":{"excludeExchange":1,"oddsType":1,"filterType":1,"matchId":"1853844","lotteryId":90}}'
    enctext = AES_Encrypt(key, data)
    print(enctext)

    s = AES_Decrypt(key, "vU9OZmOQo5fYC6i26WbVOzqcJai+LSrC++nO/P/SwGEoNQ8giQD7qiif21NWa8Y1W6PqVOaG6vTE2LLHcjOIruez2lAFUVo16+aam6t/Aoy6pG8jk4sI5cCZ6N3Vvk7rlyuqk1O2djE6o5K00S5FzSSpBVujc7djN1mv09t18EyJt/09RAIEmefNsAVnmBWHCuoXEX6OlxFYUyjTtOv2Wuo5MpcVpnUuvgKoNCvmp6/EY/B1rlAmnn/khsfUsXIbWkgdxAImSLsWV+BJuTJD2/yNaJwOERNsg6l516uAhG4=")
    print(s)

转载请注明:永盟博客 » php实现aes加解密及与其他语言(python|java)互通

喜欢 (2)