Click here to Skip to main content
15,918,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I don't quite understand what some of these lines do based on the previous code. Could someone explain to me ?

PHP
if ($c != " ") {
    $new_c = chr(((ord($c) - 97 + $clefChiffrage) % 26) + 97);

    return $alphabetFinal[ord($new_c) - 97];
}
return $c;


Context of this code : the goal of the code is to cypher a message based on the Caesar Code. It works by looking for the longest word in a string.

Based on this code, we get an encryption key. This is the number by which the translation to the right will be done. There is also an initial alphabet (abc....) and a final alphabet (azerty...).

What I have tried:

Here's the previous code :
PHP
$clefChiffrage = 0;
        $alphabetFinal = str_split("azertyuiopqsdfghjklmwxcvbn");

        function transformer($message) {
            $key_transform = array(    'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
                            'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
                            'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
                            'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
                            'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y');
            $clean = strtr( $message, $key_transform);

            return strtolower($clean);
        }

        function cmp($str1, $str2) {
            $length1 = strlen($str1);
            $length2 = strlen($str2);

            if ($length1 == $length2)
                return 0;

            return ($length1 < $length2) ? -1 : 1;
        }

        function clefChiffrement($message) {
            $arr = explode(' ', $message);

            usort($arr, "cmp");

            return strlen(end($arr));
        }

        function _crypterMessage($c) {
            global $clefChiffrage;
            global $alphabetFinal;

            if ($c != " ") {
                $new_c = chr(((ord($c) - 97 + $clefChiffrage) % 26) + 97);

                return $alphabetFinal[ord($new_c) - 97]; 
            }
            return $c;
        }

        function crypter($message) {

            $arr = str_split($message);

            $arr_chiffre = array_map("_crypterMessage", $arr);

            return join("", $arr_chiffre);
        }
Posted
Updated 21-Aug-22 6:41am

1 solution

PHP
$new_c = chr(((ord($c) - 97 + $clefChiffrage) % 26) + 97);
               <--- 1 ----> <------ 2 ------>
             <-------------- 3 ------------------->
            <-------------------- 4 -------------------->
<---- 5 ---->

1. get the numeric value of c and subtract the numeric value of 'a'. This (I think)assumes c is a lower case character.
2. Add the value of $clefChiffrage
3. Take the remainder of the above sum divided by 26
4. Add the value of 'a'
5. Convert the resultant number to its character, and set that to $new_c

But why it does this is not clear.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900