Hướng dẫn what is aes encryption in php? - Mã hóa aes trong php là gì?

Bất cứ khi nào chúng tôi cần tích hợp API tài chính của bên thứ ba, họ cho phép gửi và nhận dữ liệu ở dạng được mã hóa thay vì văn bản đơn giản.

Nói chung, tất cả những gì họ cần mã hóa AES cho bảo mật dữ liệu. & NBSP;

Trong hướng dẫn này, tôi sẽ chỉ cho bạn cách sử dụng mã hóa và giải mã AES trong PHP.AES encryption and decryption in PHP.

Mã hóa hoạt động bằng cách lấy văn bản đơn giản và chuyển đổi nó thành văn bản mật mã bằng một số phương thức và thuật toán.

Mã hóa AES là gì và nó hoạt động như thế nào?

AES là viết tắt của tiêu chuẩn mã hóa nâng cao. Mã hóa AES có ba mật mã khối là AES-128 (128 bit), AES-192 (192 bit), AES-256 (256 bit).

Các mật mã khối này được đặt tên do khóa được sử dụng cho quá trình mã hóa và giải mã.

Chúng tôi có thể mã hóa văn bản của mình bằng mã hóa AES và chọn độ dài khóa theo yêu cầu (128, 192 và 256 bit).

Vì vậy, chúng ta hãy xem cách sử dụng mã hóa và giải mã AES trong PHP.AES encryption and decryption in PHP.

//Define cipher 
$cipher = "aes-256-cbc";

//Generate a 256-bit encryption key
$encryption_key = openssl_random_pseudo_bytes(32);

// Generate an initialization vector
$iv_size = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($iv_size);

//Data to encrypt
$data = "Sample Text";
$encrypted_data = openssl_encrypt($data, $cipher, $encryption_key, 0, $iv);

echo "Encrypted Text: " . $encrypted_data;
?>

AES giải mã trong PHP

Lưu ý: Nếu bạn sẽ cố gắng in biến $ IV hoặc $ IV_SIZE thì nó sẽ hiển thị cho bạn các ký tự không xác định theo mặc định. Vì vậy, để xem giá trị của nó ở dạng có thể đọc được, bạn phải sử dụng hàm bin2hex (). If you will try to print $iv or $iv_size variable then it will show you unknown characters by default. So to see its value in readable form, you have to use the bin2hex() function.

Một điều quan trọng khác bạn phải ghi nhớ rằng giải mã luôn yêu cầu cùng một khóa mã hóa và vectơ khởi tạo (IV) được sử dụng để mã hóa.

Sự kết luận

Trong hướng dẫn này, bạn đã học được cách mã hóa và giải mã dữ liệu bằng AES.

Nếu bạn có bất kỳ câu hỏi nào, hãy bình luận bên dưới.

Đây là những phương pháp nhỏ gọn để mã hóa / giải mã các chuỗi bằng PHP bằng cách sử dụng AES256 CBC:AES256 CBC:

function encryptString($plaintext, $password, $encoding = null) {
    $iv = openssl_random_pseudo_bytes(16);
    $ciphertext = openssl_encrypt($plaintext, "AES-256-CBC", hash('sha256', $password, true), OPENSSL_RAW_DATA, $iv);
    $hmac = hash_hmac('sha256', $ciphertext.$iv, hash('sha256', $password, true), true);
    return $encoding == "hex" ? bin2hex($iv.$hmac.$ciphertext) : ($encoding == "base64" ? base64_encode($iv.$hmac.$ciphertext) : $iv.$hmac.$ciphertext);
}

function decryptString($ciphertext, $password, $encoding = null) {
    $ciphertext = $encoding == "hex" ? hex2bin($ciphertext) : ($encoding == "base64" ? base64_decode($ciphertext) : $ciphertext);
    if (!hash_equals(hash_hmac('sha256', substr($ciphertext, 48).substr($ciphertext, 0, 16), hash('sha256', $password, true), true), substr($ciphertext, 16, 32))) return null;
    return openssl_decrypt(substr($ciphertext, 48), "AES-256-CBC", hash('sha256', $password, true), OPENSSL_RAW_DATA, substr($ciphertext, 0, 16));
}

Usage:

$enc = encryptString("mysecretText", "myPassword");
$dec = decryptString($enc, "myPassword");

EDIT: Đây là phiên bản mới của các chức năng sử dụng AES256 GCM và PBKDF2 làm dẫn xuất khóa, an toàn hơn.AES256 GCM and PBKDF2 as key derivation, more secure.

function str_encryptaesgcm($plaintext, $password, $encoding = null) {
    if ($plaintext != null && $password != null) {
        $keysalt = openssl_random_pseudo_bytes(16);
        $key = hash_pbkdf2("sha512", $password, $keysalt, 20000, 32, true);
        $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length("aes-256-gcm"));
        $tag = "";
        $encryptedstring = openssl_encrypt($plaintext, "aes-256-gcm", $key, OPENSSL_RAW_DATA, $iv, $tag, "", 16);
        return $encoding == "hex" ? bin2hex($keysalt.$iv.$encryptedstring.$tag) : ($encoding == "base64" ? base64_encode($keysalt.$iv.$encryptedstring.$tag) : $keysalt.$iv.$encryptedstring.$tag);
    }
}

function str_decryptaesgcm($encryptedstring, $password, $encoding = null) {
    if ($encryptedstring != null && $password != null) {
        $encryptedstring = $encoding == "hex" ? hex2bin($encryptedstring) : ($encoding == "base64" ? base64_decode($encryptedstring) : $encryptedstring);
        $keysalt = substr($encryptedstring, 0, 16);
        $key = hash_pbkdf2("sha512", $password, $keysalt, 20000, 32, true);
        $ivlength = openssl_cipher_iv_length("aes-256-gcm");
        $iv = substr($encryptedstring, 16, $ivlength);
        $tag = substr($encryptedstring, -16);
        return openssl_decrypt(substr($encryptedstring, 16 + $ivlength, -16), "aes-256-gcm", $key, OPENSSL_RAW_DATA, $iv, $tag);
    }
}

Usage:

$enc = str_encryptaesgcm("mysecretText", "myPassword", "base64"); // return a base64 encrypted string, you can also choose hex or null as encoding.
$dec = str_decryptaesgcm($enc, "myPassword", "base64");

AES PHP là gì?

AES là viết tắt của tiêu chuẩn mã hóa nâng cao. Mã hóa AES có ba mật mã khối là AES-128 (128 bit), AES-192 (192 bit), AES-256 (256 bit). Các mật mã khối này được đặt tên do khóa được sử dụng cho quá trình mã hóa và giải mã.Advanced Encryption Standard. AES encryption has three block ciphers which are AES-128 (128 bit), AES-192 (192 bit), AES-256 (256 bit). These block ciphers are named due to the key used for the encryption and decryption process.

Mã hóa AES có nghĩa là gì?

AES là gì? Tiêu chuẩn mã hóa nâng cao (AES) là một mật mã khối đối xứng được chính phủ Hoa Kỳ lựa chọn để bảo vệ thông tin được phân loại. AES được triển khai trong phần mềm và phần cứng trên toàn thế giới để mã hóa dữ liệu nhạy cảm.a symmetric block cipher chosen by the U.S. government to protect classified information. AES is implemented in software and hardware throughout the world to encrypt sensitive data.

Mã hóa nào là tốt nhất cho PHP?

Mã hóa khóa bí mật (hoặc mã hóa đối xứng như cũng đã biết) sử dụng một khóa duy nhất để cả dữ liệu mã hóa và giải mã.Trong quá khứ PHP đã dựa vào McRypt và OpenSSL cho mã hóa khóa bí mật.Php 7.2 đã giới thiệu natri, hiện đại hơn và được coi là an toàn hơn. (or symmetric encryption as it's also known) uses a single key to both encrypt and decrypt data. In the past PHP relied on mcrypt and openssl for secret key encryption. PHP 7.2 introduced Sodium, which is more modern and widely considered more secure.

Mã hóa nào được sử dụng trong PHP?

Mã hóa khóa bí mật còn được gọi là mã hóa đối xứng, mã hóa khóa bí mật của PHP chỉ sử dụng một khóa, được gọi là bí mật chia sẻ, cho cả mã hóa và giải mã.Để mã hóa dữ liệu, ở đây một khóa tương tự được người gửi sử dụng (để mã hóa) và máy thu (để giải mã). is also called Symmetric encryption, The Secret Key Encryption of the PHP uses just one key, called a shared secret, for both encrypting and decrypting. To encrypt the data, Here one same key is used by the sender (for encryption) and the receiver (for decryption).