[<prev] [next>] [day] [month] [year] [list]
Message-ID: <CAFf+5zgmy9p3vLHKY6qeTKJO=RQa8z0H8dNs8BfqDr9D-1GC9g@mail.gmail.com>
Date: Thu, 5 Jun 2025 10:23:42 +0530
From: Amit <amitchoudhary0523@...il.com>
To: linux-kernel@...r.kernel.org
Subject: Encrypt decrypt file using password as key (simple algorithm) (File: encrypt_decrypt_file.php).
Encrypt decrypt file using password as key (simple algorithm) (File:
encrypt_decrypt_file.php).
---------------------------------
encrypt_decrypt_file.php
---------------------------------
<?php
$e_option_present = FALSE;
$d_option_present = FALSE;
$op = "";
$op_str = "";
$input_file = "";
$output_file = "";
$in_handle = NULL;
$out_handle = NULL;
$password = "";
$max_password_len = 20;
$actual_password_len = 0;
$help_msg = "Please see how to use this program/script by using the --help" .
" option (encrypt_decrypt_file --help).";
$buf_size = 8192;
// error handler function
function custom_error_handler($errno, $errstr, $errfile, $errline)
{
//echo "Got error/notice/warning, etc.\n";
//echo $errno . "\n";
//echo $errtsr . "\n";
//echo $errfile . "\n";
//echo $errline . "\n";
return true;
} // end of custom_error_handler
// set to the user defined error handler
$old_error_handler = set_error_handler("custom_error_handler");
function print_usage()
{
echo ("Usage:\n\n" .
" Syntax:\n\n" .
" encrypt_decrypt_file OPTION INPUT_FILE\n\n" .
" Description:\n\n" .
" encrypt_decrypt_file will encrypt or decrypt INPUT_FILE\n" .
" according to the option given by the user.\n" .
" \n" .
" encrypt_decrypt_file will ask the user for a password and\n" .
" then use this password to encrypt or decrypt the INPUT_FILE\n" .
" (according to the option given by the user).\n" .
" \n" .
" The password for decrypting the input file should be the\n" .
" same as the password given for encrypting the original input\n" .
" file.\n" .
" \n" .
" The name of the output file will be\n" .
" input_file_name.[encrypted|decrypted] and it will be created\n" .
" in the same directory in which the INPUT_FILE is present.\n" .
" \n" .
" In case, the directory already has a file with the same name\n" .
" as the output file name, then this script will exit without\n" .
" doing anything and print an appropriate error message.\n" .
" \n" .
" Basically, this can be considered as protecting a file with\n" .
" a password.\n" .
" \n" .
" Options:\n\n" .
" -e\n" .
" Encrypt the INPUT_FILE.\n\n" .
" -d\n" .
" Decrypt the INPUT_FILE.\n\n" .
" --help\n".
" Print this usage/help and exit.\n\n");
} // end of print_usage
// print debug info
function print_debug_info()
{
global $argc, $argv, $input_file, $output_file;
echo "DEBUG_INFO_START:\n\n";
echo "Number of arguments = " . ($argc - 1) . "\n\n";
for ($i = 1; $i < $argc; $i++) {
$arg = trim($argv[$i]);
echo "Argument " . $i . ": " . $arg . "\n";
}
echo "\n";
echo "input_file: '$input_file'\n";
echo "output_file: '$output_file'\n";
echo "\nDEBUG_INFO_END\n\n";
} // end of print_debug_info
function close_file_handles()
{
global $in_handle, $out_handle;
fclose($in_handle);
fclose($out_handle);
} // end of close_file_handles
function delete_output_file()
{
if (unlink($output_file) == FALSE) {
echo "Error: Failed to delete output file. Please delete it" .
" manually. Exiting..\n";
}
} // end of delete_output_file
echo "\n";
//---- trim all elements of argv[] (including argv[0]) and store in an arg_list
// so that I don't have to trim argv[] everywhere.
//---- check testing from here.
//---- check correctness of program.
// check for --help option first
for ($i = 1; $i < $argc; $i++) {
$arg = trim($argv[$i]);
if ($arg === '--help') {
print_usage();
exit(0);
}
}
if ($argc != 3) {
echo "Error: (Only) two arguments are required: -e|-d and an input file" .
" name. Exiting..\n";
echo "\n";
echo "Number of arguments given = " . ($argc - 1) . "\n";
echo "\n";
echo "Arguments given are: ";
if ($argc > 1) {
echo "\n";
for ($i = 1; $i < $argc; $i++) {
$arg = trim($argv[$i]);
echo " Argument " . $i . ": " . $arg . "\n";
}
} else {
echo "None\n";
}
echo "\n";
echo $help_msg;
echo "\n\n";
exit(1);
}
for ($i = 1; $i < $argc; $i++) {
$arg = trim($argv[$i]);
if ($arg === "-e") {
$e_option_present = TRUE;
$op = "encrypt";
} else if ($arg === "-d") {
$d_option_present = TRUE;
$op = "decrypt";
} else {
$input_file = $arg;
}
} // end of for loop
if (($e_option_present == FALSE) && ($d_option_present == FALSE)) {
echo "Error: Neither '-e' option nor '-d' option is given." .
" This is invalid. One of the options should be given. Exiting..\n";
echo "\n";
echo $help_msg;
echo "\n\n";
exit(1);
}
if (($e_option_present == TRUE) && ($d_option_present == TRUE)) {
echo "Error: Both '-e' option and '-d' option are given." .
" This is invalid. Only one option should be given. Exiting..\n";
echo "\n";
echo $help_msg;
echo "\n\n";
exit(1);
}
if (strlen($input_file) == 0) {
echo "Error: Input file name is empty. Exiting..\n";
echo "\n";
echo $help_msg;
echo "\n\n";
exit(1);
}
// set output file name
$op_str = $op . "ed";
$output_file = $input_file . "." . $op_str;
//print_debug_info();
// print names of input file and output file
echo "Input file: $input_file\n";
echo "Output file (to be created): $output_file\n\n";
// check if output file already exists by trying to read it
$op_str = $op . "ion";
$op_str = ucfirst($op_str);
if (($out_handle = fopen($output_file, "r")) != FALSE) { // output file exists
fclose($out_handle);
echo "\n";
echo "Error: Output file already exists. $op_str was not done." .
" Exiting..\n\n";
exit(1);
}
// open input file for reading
if (($in_handle = fopen($input_file, "rb")) == FALSE) {
echo "\n";
echo "Error: Failed to open input file for reading. $op_str was not done." .
" Exiting..\n\n";
exit(1);
}
// get password from user
$op_str = $op . "ing";
while (strlen($password) == 0) {
echo "Please enter password for $op_str input file" .
" (max. $max_password_len characters): ";
if (($password = fread(STDIN, $max_password_len)) == FALSE) {
echo "Error: Failed to read password entered by the user on standard" .
" input (STDIN). Exiting..\n\n";
fclose($in_handle);
exit(1);
}
// remove \r and \n from password
$password = str_replace("\r", "", $password);
$password = str_replace("\n", "", $password);
$actual_password_len = strlen($password);
//var_dump($password);
} // end of while strlen password
// open output file for writing
if (($out_handle = fopen($output_file, "wb")) == FALSE) {
echo "\n";
echo "Error: Failed to open output file for writing. $op_str was not" .
" done. Exiting..\n\n";
fclose($in_handle);
exit(1);
}
while (!feof($in_handle)) {
//testing
//echo "Press enter to resume testing: ";
//fread(STDIN, 20);
if (($buf = fread($in_handle, $buf_size)) == FALSE) {
echo "\n";
echo "Error: Failed to read input file. $op_str was not done." .
" Exiting..\n\n";
close_file_handles();
delete_output_file();
echo "\n";
exit(1);
}
$buf_len = strlen($buf);
for ($bi = 0, $pi = 0;
$bi < $buf_len;
$bi++, $pi = (($pi + 1) % $actual_password_len)) {
//echo "$buf[$bi], ";
//echo "$password[$pi], ";
$buf[$bi] = ($buf[$bi] ^ $password[$pi]) ^ chr(0x80);
//echo "$buf[$bi]\n";
//print_r(unpack("H*", $buf[$bi]));
} // end of for loop
//testing
//echo "Press enter to resume testing: ";
//fread(STDIN, 20);
if (($num_bytes_written = fwrite($out_handle, $buf, $buf_len)) == FALSE) {
echo "\n";
echo "Error: Failed to write to output file. $op_str was not done." .
" Exiting..\n\n";
close_file_handles();
delete_output_file();
echo "\n";
exit(1);
}
//testing
//$num_bytes_written = 1234;
//echo "Press enter to resume testing: ";
//fread(STDIN, 20);
if ($num_bytes_written != $buf_len) {
echo "\n";
echo "Error: Number of bytes read from the input file are not equal" .
" to the number of bytes written to the output file. Output file" .
" is corrupted. Deleting output file and exiting..\n";
close_file_handles();
delete_output_file();
echo "\n";
exit(1);
}
} // end of while !feof
$op_str = $op . "ed";
echo "\n";
echo "Input file was $op_str successfully.\n\n";
echo "Output file was created successfully and is ready.\n\n";
close_file_handles()
?>
----
Powered by blists - more mailing lists