[PHP] Upload encrypt image

Davide Cat

Nuovo Utente
5 Lug 2017
1
0
1
34
Ciao ragazzi,
Sto provando a caricare un immagine ma non riesco a cifrarla utilizzando il metodo openssl_encrypt. Qualcuno saprebbe aiutarmi?

// DEFINE our cipher
define('AES_256_CBC', 'aes-256-cbc');
// Generate a 256-bit encryption key
// This should be stored somewhere instead of recreating it each time
$encryption_key = openssl_random_pseudo_bytes(32);
// Generate an initialization vector
// This *MUST* be available for decryption as well
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));


$user = $_SESSION['username'];
$directorypath = "uploads/". $user; //specifies the directory where the file is going to be placed
mkdir($directorypath, 0777); //create directory

$target_path = "$directorypath/";
$target_path = $target_path . basename( $_FILES['fileToUpload']['name']);

$uploadOk = 1;

$imageFileType = pathinfo($directorypath,PATHINFO_EXTENSION);

$data = $_FILES["fileToUpload"]["tmp_name"];
// Encrypt $data using aes-256-cbc cipher with the given encryption key and
// our initialization vector. The 0 gives us the default options, but can
// be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
$encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);

// If we lose the $iv variable, we can't decrypt this, so:
// - $encrypted is already base64-encoded from openssl_encrypt
// - Append a separator that we know won't exist in base64, ":"
// - And then append a base64-encoded $iv
$encrypted = $encrypted . ':' . base64_encode($iv);

if(move_uploaded_file($encrypted, $target_path)) {
echo "The file ". basename( $_FILES['fileToUpload']['name'])." has been uploaded";
}else{
echo "There was an error uploading the file, please try again!";
}
 

Discussioni simili