I want to decrypt file with extention .txt .docx .xlsx and .pdf from uploading file.
encrypt.php
<?php
include "koneksi.php";
function generateKeyPairFromFileName(){
$config = array(
"default_md" => "sha512",
"private_key_bits" => 512,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$keypair = openssl_pkey_new($config);
if (!$keypair) {
die("Failed to generate key pair");
}
openssl_pkey_export($keypair, $privateKey, null, $config);
$publicKey = openssl_pkey_get_details($keypair);
$publicKey = $publicKey['key'];
return array(
'publicKey' => $publicKey,
'privateKey' => $privateKey
);
}
function savePublicKeyToLocal($publicKey, $fileName) {
$publicKeyPath = $fileName . '_public_key.pem'; // Ganti dengan lokasi penyimpanan kunci publik Anda
file_put_contents($publicKeyPath, $publicKey);
return $publicKeyPath;
}
// Fungsi untuk menyimpan kunci pribadi ke file lokal
function savePrivateKeyToLocal($privateKey, $fileName) {
$privateKeyPath = $fileName . '_private_key.pem'; // Ganti dengan lokasi penyimpanan kunci pribadi Anda
file_put_contents($privateKeyPath, $privateKey);
return $privateKeyPath;
}
// Fungsi untuk mengenkripsi file menggunakan AES
function encryptFile($data, $key) {
// Buat vektor inisialisasi (IV) acak
$iv = openssl_random_pseudo_bytes(16);
// Enkripsi data menggunakan AES-256-CBC dengan kunci dan IV yang diberikan
$encryptedData = openssl_encrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
// Base64 encode hasil enkripsi
$encryptedDataBase64 = base64_encode($encryptedData);
// Simpan atau kirim data terenkripsi ke tempat penyimpanan atau penerima
// echo "Data terenkripsi: " . $encryptedDataBase64;
return $encryptedDataBase64;
}
if(isset($_POST['generate'])){
$uploadedFileName = $_FILES['file']['name'];
$filetmp = $_FILES['file']['tmp_name'];
// Membaca isi file
$fileContent = file_get_contents($filetmp);
$KeyPair = generateKeyPairFromFileName();
$publicKeyPath = savePublicKeyToLocal($KeyPair['publicKey'], $uploadedFileName);
$publicKeyPath = savePrivateKeyToLocal($KeyPair['privateKey'], $uploadedFileName);
$encryptedFileContent = encryptFile($fileContent, $KeyPair['publicKey']);
// echo $encryptedFileContent;
$query = "INSERT INTO upload (nama_file, filedata) VALUES ('$uploadedFileName', '$encryptedFileContent')";
if (mysqli_query($conn, $query)) {
echo "File berhasil diunggah dan dienkripsi.";
} else {
echo "Gagal mengunggah dan menyimpan file ke database: " . mysqli_error($conn);
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
Pilih file untuk diunggah (PDF, DOCX, XLSX, TXT, maks 15 MB):
<input type="file" name="file">
<button name="generate" type="submit">Generate</button>
</form>
I save the private and public key on local. The script above is for encrypt data before save on the database.
ecnrypt.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "koneksi.php";
// Fungsi dekripsi
function decryptFile($data, $key) {
// Decode data terenkripsi dari base64
$encryptedData = base64_decode($data);
// Ambil IV dari data terenkripsi
$iv = openssl_random_pseudo_bytes(16);
$decryptedData = openssl_decrypt($encryptedData, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
// Kembalikan data yang telah didekripsi
return $decryptedData;
}
if(isset($_GET['nama_file'])){
// Nama file
$fileName = $_GET['nama_file'];
// Membaca kunci privat
try {
$privateKeyPath = $fileName . '_private_key.pem';
if (!file_exists($privateKeyPath)) {
throw new Exception("File kunci privat tidak ditemukan: " . $privateKeyPath);
}
$privateKey = file_get_contents($privateKeyPath);
} catch (Exception $e) {
echo "Gagal membaca kunci privat: " . $e->getMessage();
exit;
}
// Mengambil data file terenkripsi dari database
$query = "SELECT filedata FROM upload WHERE nama_file = '$fileName'";
$result = mysqli_query($conn, $query);
if ($result && mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
// Dekripsi data
$decryptedData = decryptFile($row['filedata'], $privateKey);
// Tampilkan data yang telah didekripsi
echo $decryptedData;
} else {
echo "File tidak ditemukan!";
}
}
The file that i want to decrypt is empty. i mean return empty on decrypted data. is anyone have same problem ?
Expecting file with decrypted data.
I've reviewed my PHP script, and it appears to effectively combine RSA and AES encryption for file uploads. And works for me. Thank you.
encrypt.php
decrypt.php