Common PHP Encryption Methods: MD5, Crypt, SHA1, URL Encoding, Base64, and Password Hashing
This article explains the main PHP encryption techniques—including one‑way hash functions like MD5 and SHA1, reversible encodings such as URL‑encode and Base64, and secure password hashing APIs—providing code examples and usage notes for each method.
PHP provides several encryption approaches that can be grouped into one‑way hash functions, symmetric (reversible) encodings, and asymmetric techniques, each serving different security needs.
MD5 Hash – MD5 is a non‑reversible hash commonly used for password storage. Example:
<?php
// Example string
$str = "this is string";
// MD5 hash (hexadecimal, default)
$res = md5($str);
// MD5 raw binary (16‑byte) output
$res = md5($str, true);
?>crypt() Function – Another one‑way hash that uses a salt. If no salt is provided, a random one is generated.
<?php
$str = "this is string";
// crypt without explicit salt (randomly generated)
$res = crypt($str);
// crypt with a two‑character salt (e.g., "jm")
$res = crypt($str, 'jm');
?>SHA1 Hash – Similar to MD5, SHA1 returns a 40‑character hexadecimal string unless the raw output flag is set.
<?php
$str = "this is string";
$res = sha1($str); // hex output
$res = sha1($str, true); // raw binary output
?>URL Encoding – A reversible encoding useful for safely transmitting data in URLs.
<?php
$str = "http://www.baidu.com?name=zhangsan&phone=112";
$res = urlencode($str); // encode
$result = urldecode($res); // decode
?>Base64 Encoding – Primarily used for data transport rather than security; it converts binary data to an ASCII string.
base64_encode($data);
base64_decode($data);hash() Function – Provides a flexible one‑way hash where the algorithm (e.g., "md5", "sha256") is specified as the first argument. hash($algorithm, $data); PHP 5.5+ introduces the Password Hashing API for secure password storage:
$hash = password_hash($password, PASSWORD_DEFAULT, $options);
if (password_verify($password, $hash)) {
// password is correct
} else {
// invalid password
}AES Example Class – Demonstrates a simple wrapper for AES‑128‑ECB encryption/decryption using OpenSSL.
class AES {
/**
* Encrypt a string with a secret key.
*/
public static function encrypt($str, $secret) {
return base64_encode(openssl_encrypt($str, "AES-128-ECB", $secret, OPENSSL_RAW_DATA));
}
/**
* Decrypt a string with a secret key.
*/
public static function decrypt($str, $secret) {
return openssl_decrypt(base64_decode($str), "AES-128-ECB", $secret, OPENSSL_RAW_DATA);
}
}These examples illustrate how PHP developers can choose appropriate encryption or hashing mechanisms based on whether they need irreversible protection, reversible encoding, or full cryptographic security.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
