tags:

views:

680

answers:

3

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

+2  A: 

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.

scragar
not work fine why?
monkey_boys
A: 

i test word: insert $act = RotEncrypt("insert", $GLOBALS['config_key_security']); that result:�ำๆูๆู

and i RotDecrypt it result:ฒพรมมฮภว_�fre

not work

monkey_boys
A: 

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.