92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Utils;
|
|
|
|
class Crypt
|
|
{
|
|
private static $key = 'd82499ef6072d87a2f547ba57aa38825';
|
|
private static $rounds = 1001;
|
|
private static $customCharset = '-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
|
|
public static function XORCrypt($text)
|
|
{
|
|
$data = self::addPadding($text, 32);
|
|
$encryptedData = self::convertToCustomCharset($data);
|
|
$key = self::convertToCustomCharset(self::$key);
|
|
$keyLength = strlen($key);
|
|
|
|
for ($j = 0; $j < self::$rounds; $j++) {
|
|
$encryptedData = strrev($encryptedData);
|
|
|
|
$start = ($j * 12) % $keyLength;
|
|
$keyPart = substr($key, $start, 12);
|
|
$encryptedData = self::applyXOREncryption($encryptedData, $keyPart);
|
|
}
|
|
|
|
return $encryptedData;
|
|
}
|
|
|
|
private static function addPadding($text, $blockSize)
|
|
{
|
|
$paddingLength = $blockSize - (strlen($text) % $blockSize) - 1;
|
|
$padding = self::generateRandomPadding($paddingLength);
|
|
|
|
return $text . '.' . $padding;
|
|
}
|
|
|
|
private static function convertToCustomCharset($inputString)
|
|
{
|
|
$output = '';
|
|
|
|
for ($i = 0; $i < strlen($inputString); $i++) {
|
|
$char = $inputString[$i];
|
|
$charIndex = strpos(self::$customCharset, $char);
|
|
|
|
if ($charIndex === false) {
|
|
$output .= '~';
|
|
} else {
|
|
$output .= $char;
|
|
}
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
private static function generateRandomPadding($length)
|
|
{
|
|
$padding = '';
|
|
$charsetLength = strlen(self::$customCharset);
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomIndex = rand(0, $charsetLength - 1);
|
|
$padding .= self::$customCharset[$randomIndex];
|
|
}
|
|
|
|
return $padding;
|
|
}
|
|
|
|
private static function applyXOREncryption($data, $key, $decrypt = false)
|
|
{
|
|
$result = '';
|
|
$keyLength = strlen($key);
|
|
$charsetLength = strlen(self::$customCharset);
|
|
|
|
for ($i = 0; $i < strlen($data); $i++) {
|
|
$char = $data[$i];
|
|
$keyChar = $key[$i % $keyLength];
|
|
|
|
$charIndex = strpos(self::$customCharset, $char);
|
|
$keyIndex = strpos(self::$customCharset, $keyChar);
|
|
|
|
if ($charIndex !== false && $keyIndex !== false) {
|
|
$encryptedIndex = $decrypt ? ($charIndex - $keyIndex + $charsetLength) % $charsetLength : ($charIndex + $keyIndex) % $charsetLength;
|
|
$result .= self::$customCharset[$encryptedIndex];
|
|
} else {
|
|
$result .= '~';
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|