0

I am a newbie in programming and in need of help with PHP. I have a code that is use to decrypt a string. The problem is it only show the decrypted first variable in the function(decriptInfoDetails). What need to be change so it can also shows the 2nd and 3rd variable in the function? Also is there a better way of doing this for example if I have more variable to decrypt say 10 or more? I attach the code below for viewing purpose. Hope somebody can help this newbie, and thanks in advance.

    <?

        $privateKey = "xxxxx";
        $iv = "xxxxx";

        $keyPassword = "xxxxx";
        $ivPassword = "xxxxx";              

        function decriptInfoDetails($ciphertext_base64){
        global $privateKey,$iv;

        $ciphertext_dec = trim(base64_decode($ciphertext_base64));

        $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey,
        $ciphertext_dec, MCRYPT_MODE_CBC, $iv);         

        return trim($plaintext_utf8_dec);
        }

        function encPass($ciphertext_base64){
        global $keyPassword,$ivPassword;

        $ciphertext_dec = trim(base64_decode($ciphertext_base64));

        $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $keyPassword,
        $ciphertext_dec, MCRYPT_MODE_CBC, $ivPassword);

        return trim($plaintext_utf8_dec);
        }           

        function encAndDecPass($data){
        global $keyPassword,$ivPassword;

        $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $keyPassword, $data, MCRYPT_MODE_CBC, $ivPassword);
        $ciphertext_base64 = trim(base64_encode($encrypted));

        return  trim($ciphertext_base64);
        }

        echo decriptInfoDetails("D8D6OsHciT/rBfeNMGrwtQZegzDv02dTnotroNOe+Kk=","ojPlxa16CVhoLq8GP8d0h5l+Z+5GqFXSWXaX7GSXC/Q=1","zrJCOomZ4CJIeBTomAb9OGq2pQK17FBGqVNFdVawPB8=1"); 

    ?>

2 Answers 2

1

Change the parameter of the function to array.

<?
error_reporting(E_ALL);
$privateKey = "xxxxx";
$iv         = "xxxxx";

$keyPassword = "xxxxx";
$ivPassword  = "xxxxx";

function decriptInfoDetails($ciphertext_base64_ar)
{
    global $privateKey, $iv;

    $result = array();

    foreach ($ciphertext_base64_ar as $ciphertext_base64) {
        $ciphertext_dec = trim(base64_decode($ciphertext_base64));

        $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey,
        $ciphertext_dec, MCRYPT_MODE_CBC, $iv);

        $result[]= trim($plaintext_utf8_dec);
    }

    return $result;
}
print_r(decriptInfoDetails(array(
    "D8D6OsHciT/rBfeNMGrwtQZegzDv02dTnotroNOe+Kk=",
    "ojPlxa16CVhoLq8GP8d0h5l+Z+5GqFXSWXaX7GSXC/Q=1",
    "zrJCOomZ4CJIeBTomAb9OGq2pQK17FBGqVNFdVawPB8=1"
)));

?>

NOTE: Removed unused functions.

Sign up to request clarification or add additional context in comments.

3 Comments

@StrikeNeo fixed the example.
this time it only shows the 1st variable, like my original code. How can i display all 3 of it
thanks a lot man, it works. u hv been a great help to this newbie :)
0

Wihtout being an expert on the matter and only for trying to help purposes, i think that at the end of your code below you are giving too many arguments inside decriptInfoDetails function while at the start of your code you stated that it gets only one.You should only give one and not three.

echo decriptInfoDetails("D8D6OsHciT/rBfeNMGrwtQZegzDv02dTnotroNOe+Kk=","ojPlxa16CVhoLq8GP8d0h5l+Z+5GqFXSWXaX7GSXC/Q=1","zrJCOomZ4CJIeBTomAb9OGq2pQK17FBGqVNFdVawPB8=1");

2 Comments

how can i make it so that it gets 3 argument?
When you declare you function you can specify it like this: function decriptInfoDetails($var1,$var2,$var3) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.