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 Sarah Richard · June 13, 2015
Looks complicated,what‘s this.
+1 Alan Johnson · June 14, 2015
Well I've commented what you've done so far:


// Word to encrypt
$data = 'Hello';

// Array storing each letter in the word ['h', 'e', 'l', 'l', 'o']
$dataSplit = str_split($data, 1);

// String used for output
$output = "";

// Go through each character and encrypt it by changing its ASCII value from decimal to octal
foreach($dataSplit as $key => $value){
// ord() - get the ASCII value of a character
// base_convert(10, 8) - convert from decimal(10) to octal(8)
$value   = base_convert(ord($value), 10, 8);

// The character has been "encrypted" now and can added to the output
$output .= $value;
}

// codeer() - this isnt a PHP function so I don't know what it does
echo codeer($data);



If you still need help let me know.
0 Oussama Fahchouch · June 14, 2015
@Sarah 

Trust me this is not even near to complexity, i am just a first year and i didn't even study mathematics. It takes time to learn the syntax, and if you do, it is like a puzzle, every piece haves it's own function in the whole thing.

@Alan

I appreciate your comments :)! But my question actually was: How can i make  functions that encrypts and decrypts the same input data.


I am this far now..:


$data = 'Hello';

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

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

// echo $output;

$dataSplitB = str_split($output, 3);

foreach($dataSplitB AS $keyb => $valueB){  
 $valueB = base_convert(ord($valueB), 8, 10);
 /*$valueB = chr($valueB);
 $outputB .= $valueB; */ 
}
0 Oussama Fahchouch · June 14, 2015
So i know how to encrypt ( don't laugh at me for how weak the encryption is, but i am still a beginner) a file that is being inputted. In this caase it's stored in the variable named $data with the string  'Hello';

I am trying to decrypt it back to a string now.

Sorry for not commenting on my code..
+1 Alan Johnson · June 14, 2015
Ok, I've done this for you, here you go:

// Word to encode
$data = 'Hello';

// Array storing each letter in the word ['h', 'e', 'l', 'l', 'o']
$dataSplit = str_split($data, 1);

// String used for output
$output = "";

// Go through each character and encode it by changing its ASCII value from decimal to octal
foreach($dataSplit as $key => $value){
// ord() - get the ASCII value of a character
// base_convert(10, 8) - convert from decimal(10) to octal(8)
$value   = base_convert(ord($value), 10, 8);

// The character has been "encoded" now and can added to the output
$output .= $value;
}
// Display encoded word
echo 'Encoded: ' . $output . '';

// Decode it now...
echo 'Now attempting to decode...';

// Store decoded word
$decoded = '';

// Split the encoded word up, your previous function is using 3 numbers per letter
$outputSplit = str_split($output, 3);

// Go through each word and decode it, opposite to what you did previously
foreach ($outputSplit as $key=>$value) {
$value = chr(base_convert($value, 8, 10));
$decoded .= $value;
}

// Display decoded word
echo 'Decoded word: ' . $decoded . '';
+1 Oussama Fahchouch · June 14, 2015
I couldnt figure out how to change it back from 8 to 10..

Thank you for helping me out: I came this far on my own:


$data = 'Hello';

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

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

// echo $output;

$dataSplitB = str_split($output, 3);
$outputB = "";

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

var_dump($outputB);
echo $outputB;
0 Oussama Fahchouch · June 14, 2015
What could you advice me if you look to my 'coding style' :)?!
0 Alan Johnson · June 14, 2015
No problem, glad to help
0 Oussama Fahchouch · June 14, 2015
Anyways, thank you, forget my question, how can you see how i code in just that little code :P.
0 Oussama Fahchouch · June 14, 2015
Alan, can you tell me something about how people make huge complex encrypting applications?

How far am i away from those techniques?
  • 1
  • 2

PHP

106,946 followers
About

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

Links
Moderators