Reputation: 7348
in function need key to encrypt string without mcrypt libraly in php
function encrypt($str, $pass){
$str_arr = str_split($str);
$pass_arr = str_split($pass);
$add = 0;
$div = strlen($str) / strlen($pass);
while ($add <= $div) {
$newpass .= $pass;
$add++;
}
$pass_arr = str_split($newpass);
foreach($str_arr as $key =>$asc) {
$pass_int = ord($pass_arr[$key]);
$str_int = ord($asc);
$int_add = $str_int + $pass_int;
$ascii .= chr($int_add);
}
return $ascii;
}
function decrypt($enc, $pass){
$enc_arr = str_split($enc);
$pass_arr = str_split($pass);
$add = 0;
$div = strlen($enc) / strlen($pass);
while ($add <= $div) {
$newpass .= $pass;
$add++;
}
$pass_arr = str_split($newpass);
foreach($enc_arr as $key =>$asc) {
$pass_int = ord($pass_arr[$key]);
$enc_int = ord($asc);
$str_int = $enc_int - $pass_int;
$ascii .= chr($str_int);
}
return $ascii;
}
in this not work for i character i test it
Upvotes: 5
Views: 10119
Reputation: 291
The question is old but, as PHP 7 don't have mcrypt, I had the same problem. The best solution I found is using OpenSSL. It is built into PHP and you don't need any external library.
To encrypt:
function encrypt($key, $payload) {
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($payload, 'aes-256-cbc', $key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
To decrypt:
function decrypt($key, $garble) {
list($encrypted_data, $iv) = explode('::', base64_decode($garble), 2);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}
Reference link: https://www.shift8web.ca/2017/04/how-to-encrypt-and-execute-your-php-code-with-mcrypt/
Upvotes: 0
Reputation: 668
I used jlogsdon enc_encrypt
and enc_decrypt
in prestashop 1.4.4.1 to wrap encrypt
and decrypt
methods from Rijndael class, witch are using mcrypt.
I didn'e check if enc_encrypt
and enc_decrypt
give exacly the same resulst as mcrypt_encrypt
and mcrypt_decrypt
, but they seem to work in prestashop 1.4.4.1. Thanks a lot.
Upvotes: 1
Reputation:
Here's another method:
$key = 'the quick brown fox jumps over the lazy ';
$string = 'Hey we are testing encryption';
echo enc_encrypt($string, $key)."\n";
echo enc_decrypt(enc_encrypt($string, $key), $key)."\n";
function enc_encrypt($string, $key) {
$result = '';
for($i = 0; $i < strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char) + ord($keychar));
$result .= $char;
}
return base64_encode($result);
}
function enc_decrypt($string, $key) {
$result = '';
$string = base64_decode($string);
for($i = 0; $i < strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char) - ord($keychar));
$result .= $char;
}
return $result;
}
Again, don't expect this to be very secure.
Upvotes: 6
Reputation: 6824
This code is rather inefficient, I'm not surprised it doesn't give intended results.
echo RotEncrypt('cheese', 'pie') . "\n<br>\n"
.RotDecrypt(RotEncrypt('cheese', 'pie'), 'pie');
// ÓÑÊÕÜÊ
// cheese
function RotEncrypt($str, $pass){
$pass = str_split(str_pad('', strlen($str), $pass, STR_PAD_RIGHT));
$stra = str_split($str);
foreach($stra as $k=>$v){
$tmp = ord($v)+ord($pass[$k]);
$stra[$k] = chr( $tmp > 255 ?($tmp-256):$tmp);
}
return join('', $stra);
}
function RotDecrypt($str, $pass){
$pass = str_split(str_pad('', strlen($str), $pass, STR_PAD_RIGHT));
$stra = str_split($str);
foreach($stra as $k=>$v){
$tmp = ord($v)-ord($pass[$k]);
$stra[$k] = chr( $tmp < 0 ?($tmp+256):$tmp);
}
return join('', $stra);
}
Tested and it appears to work fine for me.
Either way I feel I should warn you that this is a really insecure form of encryption, most passwords use the same sets of characters and a fairly small range of lengths, which makes this system very vulnerable to people being able to work out with a fairly good level of accuracy the password.
Upvotes: 5