<?php
//http://web.chacuo.net/netrsakeypair 在线生成秘钥
header("Content-type: text/html; charset=utf-8"); $PassWork='chenlongzhen@.com'; $PrivateKey='-----BEGIN ENCRYPTED PRIVATE KEY----- MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQI76ce+I7qz3cCAggA MBQGCCqGSIb3DQMHBAguzOm+n9y1AASCAoDtX0fUn2KdFmVu5ujfKIqW9PrLV+1Q PuCUYXmveFHyJjaZUWrXMSqakdNtURWnPaxXsE3fRB3I0qLD+lEwQBfNcsQHGjTY tcY6XFThAQnudbmgYmapzh8oZXbbj4YDsvlooMMF2CSYUzeMmbSSTBzF67EUtUkW NhhmlaRTNug/keM27pNEk9ZbqNDmVddIiI1WuUv/skFWewxE6Ek3wveaGrgtfXCP tS0oWkG4Jzxzeu2u4NsdKXN8h9pcL4JAoGYqyWGRvuv9PBIpeE3BYGC2nPuloKZ4 YSevunMAVcxrgoToERq7gq/9PUlmjQCvsyxAsSG6MQX7MzHoSL3OxgMaHQER3xlB hwY2w8/uf2M5qAqtvn25r9Nm5zQG99ZlcI5dyEGXnEFTA7iTuYpITgN0O8PTbP9w 2JV+KjKqOqkzXW/eL7e7FrIvdWpxusr5n78KKiIR/SrwUN1BD1LladZOdQ5quYQ7 NXFCKIjbLGwFu+3XDtBRIkd2N1DF52S3NQKqWLzGyQvUCcR3gcOR0ha3fGKcSA76 cHR4WaOoeZTASUxdrgx2XsOyVVddfhZ8tlXFxFGPGDdesr23yLo3pA2kVcFIAZDB MiyWUJk5Wj13Kv1fLUOi/6yoqCk8tN7z1asKw9yyZg1NZjE4LQ5DNB+voo4ALpRN H5xFSpiQ0RLIuoFcsmQFj3HgFk20sMzW1dKPB2Au1W2VsBqxLW0ijiQRQafNsRj8 UscsTOn1yhp1MoA0xL23wA1dtFrKTZbeHg7Rh7KLe/28OG4QMUzTo5Wjo//zioWC a8RwT35PTF3+0lrq8XHDtQpp+TmsEYP2OKwNVgdkzCf60++w44z5O0C8 -----END ENCRYPTED PRIVATE KEY----- '; $PublicKey='-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8ELnZmVlqWOEicvitSZDWRrJa r+GOL6K20NYV3gmdJlR0nrn9QmxjT2dqMFfuS8/H//Dq3A1m+7e/T9yXukaVePST z6IcWA2O5yPJjBqn34zwxA6I8J5fsBbG+kgnckkO5+PhWl01Bif2p0HZD7J/faSd FVjUsKTRt1EfZoPQSQIDAQAB -----END PUBLIC KEY-----'; /** * @WJTEncode WebJsonToken加密 * ================================= * @Data 传入数据 * @Key 秘钥 * @Passwork 秘钥密码 */ function WJTEncode($Data, $Key, $Passwork = "") { // 判断是否为数组 $Data = is_array($Data) ? json_encode($Data) : $Data; //echo $Data;die; // 设置头部及内容部分 $Public = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSU0EifQ==.' . base64_encode($Data); // 生成签名 if ($Res = openssl_pkey_get_private($Key, $Passwork)) { // 判断签名是否成功 if (openssl_private_encrypt($Public, $Out, $Res)) { // 输出WJT密文 return $Public . '.' . $Out; } else { return false; } // 释放秘钥 openssl_free_key($Res); } else { return false; } } $Time=time(); $YacTime=7*24*3600; $data=array('age'=>100,'sex'=>'男','lisu'=>htmlspecialchars('nishishou')); $restart= WJTEncode($data, $PrivateKey, $PassWork); setcookie("Token", $restart, $Time + $YacTime); print_r($_COOKIE['Token']); /** * @WJTDecode WebJsonToken解密 * ================================= * @Data 密文 * @Key 公钥 */ function WJTDecode($Data, $Key) { // 提取各部分内容 preg_match("/^(.*?)\.(.*?)\.(.*?)$/si", $Data, $Public); // 判断格式是否正确 if ($Public) { // 初始化公钥 if ($Res = openssl_pkey_get_public($Key)) { // 判断签名是否正确 if (openssl_public_decrypt($Public[3], $Out, $Res)) { // 判断签名与实际上是否相同 if ($Out === ($Public[1] . '.' . $Public[2])) { // 解析数据详情 $PublicData = base64_decode($Public[2]); // 判断解析是否出问题 if ($PublicData) { // 判断是否Json if ($JsonArray = json_decode($PublicData, true)) { $PublicData = $JsonArray; } // 输出密文 return $PublicData; } else { return false; } } else { return false; } } else { return false; } // 释放秘钥 openssl_free_key($Res); } else { return false; } } else { return false; } } //print_r( WJTDecode($restart,$PublicKey)); $restart=$_COOKIE['Token']; echo $restart; $str=WJTDecode($restart,$PublicKey); print_r($str);