/    Sign up×
Community /Pin to ProfileBookmark

Encryption with AES

I got this code from php.net [url]http://php.net/manual/fr/book.mcrypt.php[/url]

[code=php]
<?php
function encrypt($decrypted, $password, $salt=’!kQm*fF3pXe1Kbm%9′) {
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash(‘SHA256’, $salt . $password, true);
// Build $iv and $iv_base64. We use a block size of 128 bits (AES compliant) and CBC mode. (Note: ECB mode is inadequate as IV is not used.)
srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
if (strlen($iv_base64 = rtrim(base64_encode($iv), ‘=’)) != 22) return false;
// Encrypt $decrypted and an MD5 of $decrypted using $key. MD5 is fine to use here because it’s just to verify successful decryption.
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
// We’re done!
return $iv_base64 . $encrypted;
}

function decrypt($encrypted, $password, $salt=’!kQm*fF3pXe1Kbm%9′) {
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash(‘SHA256’, $salt . $password, true);
// Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
$iv = base64_decode(substr($encrypted, 0, 22) . ‘==’);
// Remove $iv from $encrypted.
$encrypted = substr($encrypted, 22);
// Decrypt the data. rtrim won’t corrupt the data because the last 32 characters are the md5 hash; thus any character has to be padding.
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), “4”);
// Retrieve $hash which is the last 32 characters of $decrypted.
$hash = substr($decrypted, -32);
// Remove the last 32 characters from $decrypted.
$decrypted = substr($decrypted, 0, -32);
// Integrity check. If this fails, either the data is corrupted, or the password/salt was incorrect.
if (md5($decrypted) != $hash) return false;
// Yay!
return $decrypted;
}
?>
[/code]

I removed the . md5($decrypted) from line 10 of this code. I just wanted to know that if this is Ok to do this way, the md5 was just adding unnecessary 32 characters to the encrypted text. And I dont want to increase the database size by these characters.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @saud spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...