起因是打算通过机器人发文章,但是担心换行等内容通过get传输的时候被干掉,所以打算通过压缩或加密方式传输。
参考:https://blog.csdn.net/qq_35511686/article/details/104944937
<?php
echo encryptDecrypt('password', '这里是正文',0);
echo encryptDecrypt('password', '这里是密文',1);
function encryptDecrypt($key, $string, $decrypt){
if($decrypt){
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");
return $decrypted;
}else{
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
return $encrypted;
}
}
?>
php7+环境,因为mcrypt_decrypt已经弃用,所以上面代码已经不能用了,要使用下面的方法了。
<?php
$data = '';
$passwd = 'eAEN4w';//加密密钥
$options = 0;//数据格式选项(可选)
$iv = '';//加密初始化向量(可选)
$str = encryptDecrypt($data, $passwd, 1);
echo $str . "\n";
$jiemi = encryptDecrypt($str, $passwd);
echo $jiemi;
function encryptDecrypt($data, $passwd, $jiami = 0, $method = 'DES-ECB')
{
if ($jiami) {
if (strlen($data) % 16) {
$data = str_pad($data, strlen($data) + 16 - strlen($data) % 16, "\0");
}
$result = openssl_encrypt($data, $method, $passwd, OPENSSL_NO_PADDING);
$result = base64_encode($result);
$result = strtr($result, '+/', '-_');
//替换+/
} else {
$data = strtr($data, '-_', '+/');
//替换-_
$result = openssl_decrypt(base64_decode($data), $method, $passwd, OPENSSL_NO_PADDING);
}
return $result;
}
使用固定的密码总觉得不是很安全,所以更变态点。
<?php
$pa = make_id(6);//设置密码
$pass = $pa . "666";
$content="";要加密的正文
$jiami = encryptDecrypt($content,$pass, 1);
$l = strlen($jiami);
$url = "http://localhost/?l=" . $l . "&content=" . $jiami . "&pa=" . $pa;
function encryptDecrypt($data, $passwd, $jiami = 0, $method = 'DES-ECB')
{
if ($jiami) {
if (strlen($data) % 16) {
$data = str_pad($data, strlen($data) + 16 - strlen($data) % 16, "\0");
}
$result = openssl_encrypt($data, $method, $passwd, OPENSSL_NO_PADDING);
$result = base64_encode($result);
$result = strtr($result, '+/', '-_');
} else {
$data = strtr($data, '-_', '+/');
$result = openssl_decrypt(base64_decode($data), $method, $passwd, OPENSSL_NO_PADDING);
}
return $result;
}
function make_id($length)//设置长度
{
$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9');// 密码字符集,可任意添加你需要的字符
$keys = array_rand($chars, $length); // 在 $chars 中随机取 $length 个数组元素键名
$makid = '';
for ($i = 0; $i < $length; $i++) {
$makid .= $chars[$keys[$i]];// 将 $length 个数组元素连接成字符串
}
return $makid;
}
接收文件如下:
<?php
$content = $_GET['content'];
$pass = $_GET['pa'] . "666";
if ($_GET['l'] != strlen($content)) {
echo '传输失败!' . "l=" . $_GET['l'] . " strlen=" . strlen($keyword);
die();
}
$content = encryptDecrypt($content,$pass);
if (empty($content)) {
echo "解密失败,pass=" . $pass . "\n content=" . $keyword;
die();
}
function encryptDecrypt($data, $passwd, $jiami = 0, $method = 'DES-ECB')
{
if ($jiami) {
if (strlen($data) % 16) {
$data = str_pad($data, strlen($data) + 16 - strlen($data) % 16, "\0");
}
$result = openssl_encrypt($data, $method, $passwd, OPENSSL_NO_PADDING);
$result = base64_encode($result);
$result = strtr($result, '+/', '-_');
} else {
$data = strtr($data, '-_', '+/');
$result = openssl_decrypt(base64_decode($data), $method, $passwd, OPENSSL_NO_PADDING);
}
return $result;
}
参考文档:https://blog.csdn.net/zhemejinnameyuanxc/article/details/83383434