Skip to content

Instantly share code, notes, and snippets.

@ninsuo
Created June 24, 2019 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninsuo/fe63e35c7e79b5e2246291364c9dea3f to your computer and use it in GitHub Desktop.
Save ninsuo/fe63e35c7e79b5e2246291364c9dea3f to your computer and use it in GitHub Desktop.
Cornichonize
<?php
class Cornicode
{
static public function cornichonize(string $text) {
// Reverse
$cypher = strrev($text);
// Rotate
$cypher = str_rot13($cypher);
// Hex
$cypher = bin2hex($cypher);
// Base64
$cypher = base64_encode($cypher);
return $cypher;
}
static public function decornichonize(string $cypher) {
// Base64
$cypher = base64_decode($cypher);
// Hex
$cypher = hex2bin($cypher);
// Rotate
$cypher = str_rot13($cypher);
// Reverse
$text = strrev($cypher);
return $text;
}
static public function xorify(string $text, string $key)
{
$out = '';
for ($i = 0; $i < strlen($text); ) {
for ($j = 0; $j < strlen($key) && $i < strlen($text); $j++, $i++) {
$out .= $text[$i] ^ $key[$j];
}
}
return $out;
}
}
// encode
$message = 'The first one to decrypt this wins a tree. Send a :deciduous_tree: emoji to #coding_night';
$key = 'broccoli';
$xorified = base64_encode(Cornicode::xorify($message, $key));
$cornichonized = Cornicode::cornichonize("Ahahah ! C'est beaucoup trop facile, mais qu'est ce que tu croyais ?
En fait, le cypher, c'est ça :
{$xorified}
");
echo $cornichonized, PHP_EOL;
// decode
$text = Cornicode::decornichonize($cornichonized);
echo $text, PHP_EOL;
$text = Cornicode::xorify(base64_decode($xorified), $key);
echo $text, PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment