ENCRYPT AND DECRYPT FUNCTION PHP

+3 Oussama Fahchouch · June 12, 2015
The teacher at school asked me to make 2 functions.

function 1 to encrypt a string, every character gets an ascii code of 3 numbers.
                                          So 'Hello'; will look something like this: "123 433 151 151 234";

function 2 to decrypt function 1 back to a string
                                          So "123 433 151 151 234"; will look like 'Hello'; again.

$data = 'Hello';


$dataSplit = str_split($data, 1);
$output = "";

foreach($dataSplit AS $key => $value){
$value   = base_convert(ord($value), 10, 8);
$output .= $value;

echo codeer($data);




So i made it just this far, can someone help me plz. Give some explenation and comments also, because i wont learn much if its just copy and paste.

Thank you, and sorry.

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Oussama Fahchouch · June 15, 2015
Alan,

This is how my teacher today coded this:

de andere mogelijkheid


BeantwoordenAllen beantwoordenDoorsturen
Markeren als gelezen

MB


Marcel Berkien

ma 15-6-2015 08:35

Aan:

Oussama Fahchouch;




<?php
    $str = 'Oussama Fahchouch komt dagelijks naar Gouda!';

    $aaa = Geheimschrift::codeer($str);
    echo $aaa."<hr>";
    
    echo Geheimschrift::decodeer($aaa);
    
    // ----------------------------------------------------
    
    class Geheimschrift {
        public static function codeer($bron) {
            $s = "";
            for($i = 0; $i < strlen($bron); $i++)
                $s .= sprintf("%03d", ord($bron[$i]));
            
            return $s;
        }
        
        public static function decodeer($bron) {
            $s = "";
            for($i = 0; $i < strlen($bron); $i += 3) 
                $s .= chr(substr($bron, $i, 3));
                
            return $s;
        }
    }

?>
0 Oussama Fahchouch · June 14, 2015
Thank you for helping me out with this simple functions. They make sense now :)!

The Teacher also questioned me if i could upgrade this class with those 2 functions if possible. The class must be able to encrypt and decrypt files.

Do you know what he could mean? How can i encrypt or decrypt files? I don't understand what the teacher asked..
I want to complete the task for tomorrow though..
+1 Alan Johnson · June 14, 2015
Because the function doesn't return anything.

function codeer($data){
$dataSplit = str_split($data, 1);
$encrypt   = "";

foreach($dataSplit AS $key => $value){
$value    = base_convert(ord($value), 10, 8);
$encrypt .= $value;
}

return $encrypt;
}
echo codeer('Hello');


Now that will echo it out.
0 Oussama Fahchouch · June 14, 2015
It works only if i make the function echo at the end the results.. if i call the function:


function codeer($data){
$dataSplit = str_split($data, 1);
$encrypt   = "";

foreach($dataSplit AS $key => $value){
$value    = base_convert(ord($value), 10, 8);
$encrypt .= $value;
}
echo $encrypt;
}
codeer('hello');

Sorry for asking so much questions, i need to wait until morning to ask my teacher at school :)
0 Oussama Fahchouch · June 14, 2015
Ah thank you, but why can't i echo a function containing this function we made?



function codeer($data){
$dataSplit = str_split($data, 1);
$encrypt   = "";

foreach($dataSplit AS $key => $value){
$value    = base_convert(ord($value), 10, 8);
$encrypt .= $value;
}

}
echo codeer('hello');
0 Alan Johnson · June 14, 2015
Variables have something known as scope. Look into if you don't know what it is.

If you made the variable inside the foreach loop, you wouldn't be able to print it out afterwards due to scope.

Also, if you made the variable inside the loop, a new variable would be created every time it looped.
0 Alan Johnson · June 14, 2015
Yes, ord() converts a character to an ASCII value (number).

Your word gets encoded using 3 numbers, so you don't need to convert it when decoding. 
You use chr() for the decoding as it converts a number to a character like 'h' or 'e' etc.. so you can get the word back.
0 Oussama Fahchouch · June 14, 2015
Anywas, why did we need to make a empty variable to store the foreach in? Why doesn't it work without making an empty variable before the foreach loop?
0 Oussama Fahchouch · June 14, 2015
Btw i know why my code wasn't working

I saw u didn't put any ord in your foreach:


foreach ($outputSplit as $key=>$value) {
$value = chr(base_convert($value, 8, 10));
$decoded .= $value;
}


Mine contained an ord..:


foreach($dataSplitB AS $keyb => $valueB){  
  $valueB = chr(base_convert(ord($valueB), 8, 10));
  $outputB .= $valueB;
}

That's why it didn't work..
0 Oussama Fahchouch · June 14, 2015
Techniques that NSA uses or probably could use in the future.
  • 1
  • 2

PHP

106,965 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators