Monday, September 8, 2008

Encrypt vs Decrypt using PHP

md5() - Message Digest Algorithm:

md5 -- Calculate the md5 hash of a string

Calculates the MD5 hash of str using the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number.

$msg = "Evergreenphp123";
$encrypted_text = md5($msg);
echo "
Plain Text : ";
echo $msg;
echo "
Encrypted Text : ";
echo $encrypted_text;
?>

Note :

Once we encrypted the string using MD5() then we can't able to decrypt, we can again use the encrption of
the given string instead of decryption using below example.

Example :

$str = 'apple';

if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
echo "Valid string";
} else {
echo "Invalid string";
}
?>

Something about MD5:
- MD2 is a cryptographic hash function developed by Ronald Rivest in 1989.
- MD4 is a message digest algorithm (the fourth in a series) designed by Professor Ronald Rivest of MIT in 1990.
- MD4 is also used to compute NT-hash password digests on Microsoft Windows NT, XP and Vista.
- MD4 Weaknesses were demonstrated by Den Boer and Bosselaers in a paper published in 1991.
- MD5 was designed by Ron Rivest in 1991 to replace an earlier hash function, MD4.

Applications of MD5:

1. Encrypting the users password at Registration
2. Administrator itself not able to see the users password
3. Encrypting the users Activation code at the time of Registration


URL Encrypt and Decrypt

urlencode() -- URL-encodes string

urldecode() -- Decodes URL-encoded string


String Encrypt & Decrypt

Encryption

base64_encode -- Encodes data with MIME base64

This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.

Example :

/* Encrypt */
$str = 'This is an encoded string';
echo base64_encode($str);
/*
Output :
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
*/
?>

Decryption

base64_decode() -- Decodes data encoded with MIME base64

decodes encoded_data and returns the original data or FALSE on failure. The returned data may be binary.

Example :

/* Decrypt */
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
/*
Output :
This is an encoded string
*/
?>

1 comment:

Forus said...

Good one.... keep ur work

Popular Posts