Website đang trong giai đoạn hoàn thiện và chạy thử nghiệm, rất mong nhận được sự đóng góp ý kiến của mọi người để website hoàn thiện tốt hơn! Đóng góp

Table of Content

Mã hóa chuỗi kí tự bằng PHP, giải mã bằng JavaScript

Bài viết hướng dẫn cách mã hóa dữ liệu bằng PHP và giải mã dữ liệu đó bằng JavaScript.

Mã hóa chuỗi kí tự bằng PHP, giải mã bằng JavaScript


Mã hóa dữ liệu với PHP

Sử dụng hàm sau. Lưu ý kiểu dữ liệu truyền vào phải là chuỗi kí tự (String).

function CryptoJSAesEncrypt($passphrase, $plain_text) {
    $salt = openssl_random_pseudo_bytes(256);
    $iv = openssl_random_pseudo_bytes(16);

    $iterations = 999;  
    $key = hash_pbkdf2("sha512", $passphrase, $salt, $iterations, 64);

    $encrypted_data = openssl_encrypt($plain_text, 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iv);
    
    $data = array("ciphertext" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "salt" => bin2hex($salt));
    return json_encode($data);
}

Giải mã dữ liệu với JavaScript

Chúng ta sử dụng CryptoJS để giải mã. Thêm CryptoJS vào trang bằng cách nhúng liên kết sau.

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

Thêm hàm sau.

function CryptoJSAesDecrypt(passphrase, encrypted_json_string) {
    var obj_json = JSON.parse(encrypted_json_string);

    var encrypted = obj_json.ciphertext;
    var salt = CryptoJS.enc.Hex.parse(obj_json.salt);
    var iv = CryptoJS.enc.Hex.parse(obj_json.iv);   

    var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999});

    var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv});
    return decrypted.toString(CryptoJS.enc.Utf8);
}

Ví dụ sử dụng

<?php $text = CryptoJSAesEncrypt('key', 'Đây là văn bản cần mã hóa'); ?>
<script type="text/javascript">
    document.write(CryptoJSAesDecrypt('key', <?php echo $text; ?>));
</script>

Chúc các bạn thành công!


Không phải bug nào cũng xấu, có bug giúp ta tỉnh ra

Post a Comment