I know it is old topic but I played a bit my small OTP library and I would like to ask you an advice. Everything is perfect but, I am sure that no administrator want's to see something like "php_value memory_limit 500000M" :D.
I am not going to reinvent wheel but I really tried to find some library for encrypting data and I will be not satisfied with AES, mcrypt, etc. because there is no 100% safety if size of encrypted data is smaller than size of key. I will be really happy if someone will show me right direction.
My library working great but it looks that for 1 GB file I will need at least one server room ;) And because I am working on commercial solution, with "a bit" higher security level I will be not satisfied with just other library.
Many many thanks for all answers.
So here is it:
<?php
/** OtpFile - One time pad base64 file encryption
* @author Tomas Stofik, https://www.tomasstofik.com/
* @copyright 2018 Tomas Stofik
*/
final class OtpFile
{
private static $charSet = array(
'+','/','0','1','2','3','4','5','6','7','8','9','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'
);
public static function encryptFile(
$originalFilePath,
$encryptedFilePath,
$keyFilePath)
{
if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) {
if($originalFileData = self::existsFile($originalFilePath)) {
$originalFileBase64Data = base64_encode($originalFileData);
$originalFileBase64DataLength = strlen($originalFileBase64Data) - 1;
$originalFileBase64DataArray = str_split($originalFileBase64Data);
$encryptedData = NULL;
$encryptedDataKey = NULL;
for ($i=0; $i <= $originalFileBase64DataLength; $i++) {
$randKey = rand(0, sizeOf(self::$charSet) - 1);
$arrayKey = array_search(
$originalFileBase64DataArray[$i],
self::$charSet
);
if($randKey > $arrayKey) {
$str='-'.($randKey - $arrayKey);
} elseif($randKey < $arrayKey) {
$str = ($randKey + $arrayKey);
} else {
$str = $randKey;
}
$encryptedData .= self::$charSet[$randKey];
$encryptedDataKey .= $str.';';
}
$encryptedDataString = $encryptedData;
$encryptedDataKeyString = $encryptedDataKey;
if(!self::existsFile($keyFilePath)) {
file_put_contents($keyFilePath, $encryptedDataKeyString);
}
if(!self::existsFile($encryptedFilePath)) {
file_put_contents($encryptedFilePath, $encryptedDataString);
}
return 'OK';
} else {
return 'Source file not exists';
}
} else {
return 'Encrypted data already exists';
}
}
public static function decryptFile(
$encryptedFilePath,
$keyFilePath,
$decryptedFilePath)
{
$keyFileData = self::existsFile($keyFilePath);
$encryptedFileData = self::existsFile($encryptedFilePath);
$encryptedFileDataLength = strlen($encryptedFileData) - 1;
if($encryptedFileData && $keyFileData) {
$encryptedFileDataArray = str_split($encryptedFileData);
$keyFileDataArray = explode(';',$keyFileData);
$decryptedData = NULL;
for ($i=0; $i <= $encryptedFileDataLength; $i++) {
$positionCurrent = array_search($encryptedFileDataArray[$i], self::$charSet);
$positionEncrypted = $keyFileDataArray[$i];
if ($positionEncrypted == $positionCurrent) {
$move = $positionEncrypted;
} elseif($positionEncrypted < 0) {
$move=$positionEncrypted + $positionCurrent;
} elseif($positionEncrypted > 0) {
$move=$positionEncrypted - $positionCurrent;
} else {
$move='0';
}
$decryptedData .= self::$charSet[$move];
}
if(!self::existsFile($decryptedFilePath)) {
file_put_contents(
$decryptedFilePath,
base64_decode(
$decryptedData
)
);
return 'OK';
} else {
return 'Decrypted data already exists';
}
}
}
private static function existsFile($filePath)
{
$fileData = @file_get_contents($filePath);
if($fileData) {
return $fileData;
}
return FALSE;
}
}
/* Using
$originalFilePath = 'original.jpg';
$keyFilePath = 'Otp_Key_' . $originalFilePath;
$encryptedFilePath = 'Otp_Data_' . $originalFilePath;
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath;
echo OtpFile::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath);
echo OtpFile::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath);
*/