Đâ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"];
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.
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.