Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to reverse this bitwise funtion?

I was asked to encrypt a password by creating a new procedure and what I was thinking was to work with bits to change each character of my input key with apparently unrelated characters, and so I wrote this function:

(I'm working with PHP code):

function CBS($digits, $n_times) {
    $mask = 0x7FFFFFFF;
    $digits = intval($digits);
    if($n_times > 0) {
        $digits = ($digits<<$n_times%32) & (($digits>>(32-$n_times%32)) & ($mask>>(31-$n_times%32)));
    }elseif($n_times < 0) {
        $n_times = abs($n_times);
        $digits = (($digits>>$n_times%32) & ($mask >> (-1+$n_times%32))) | ($digits<<(32-$n_times%32));
    }
    return decbin($digits);
}

Of course after I encrypted my password I should be able to decrypt it.

Is there any way to do that?

You don't need to write me the code to do it, it would be great if you could explain it to me with words, too.

like image 701
Baffo rasta Avatar asked Nov 10 '22 03:11

Baffo rasta


1 Answers

"Of course after I encrypted my password I should be able to decrypt it." - fundamentally wrong!. Right encrypt function (i.e. hash-function) should not have reverse function. Very simple identification algorithm:
1. User enters password
2. Get hash from password by using encrypt function (entered_hash=f(password))
3. Compare entered_hash with right_hash_stored
NEVER store passwords, only hashes !

I think, that if you want your encrypt function has reverse, it should consist of function having reverse, so AND and OR are not such, but ROT and XOR are. So all you need - the squense of ROT/XOR (for XOR mask you can use encrypted value of previos squense step, in this case it must aslo be saved)

like image 187
Val K Avatar answered Nov 14 '22 22:11

Val K