PHP AES加密视频地址:实现无限制长度地址加密和解密
以下是一个示例代码,实现了对视频地址的AES加密和解密:
加密代码 encrypt.php:
<?php
if (!isset($_GET['url'])) {
die('Video URL not provided.');
}
$url = $_GET['url'];
$key = 'mysecretkey'; // 加密密钥,需要保密
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $url, MCRYPT_MODE_CBC, $iv);
$encrypted = $iv . $encrypted;
$result = 'ergou-' . base64_encode($encrypted);
echo json_encode(['result' => $result]);
解密代码 decrypt.php:
<?php
if (!isset($_GET['url'])) {
die('Encrypted URL not provided.');
}
$url = $_GET['url'];
$key = 'mysecretkey'; // 加密密钥,需要保密
$encrypted = base64_decode(substr($url, 6)); // 去掉前面的 'ergou-'
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = substr($encrypted, 0, $iv_size);
$encrypted = substr($encrypted, $iv_size);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);
echo json_encode(['result' => $decrypted]);
使用示例:
假设将要加密的视频地址为 https://example.com/video.mp4,加密密钥为 mysecretkey。
加密:
访问 encrypt.php?url=https://example.com/video.mp4 输出结果:
{"result":"ergou-L1ZpZGVvLm1wNAo="}
解密:
访问 decrypt.php?url=ergou-L1ZpZGVvLm1wNAo= 输出结果:
{"result":"https://example.com/video.mp4"}
原文地址: https://www.cveoy.top/t/topic/lRoU 著作权归作者所有。请勿转载和采集!