I really need some help making a script (in php) that will encode a string with the caesars box / square method. This method takes the string, and adds zeroes before it in order to make the string length square (my own addition) and then scrambles it as follows. Example: hello becomes 0000hello write in a square as 0 0 0 0 h e l l o which is read off as 00l0hl0eo. any ideas? Thanks
<?php $string = 'hello'; # Zeros while (floor(sqrt(strlen($string))) != sqrt(strlen($string))) { $string = '0'.$string; } # Splitting $str_array = explode("\r\n", chunk_split($string, sqrt(strlen($string)))); $array_size = sizeof($str_array) - 1; $new_string = null; # Formatting for ($d = 0; $d < $array_size; $d++) { for ($i = 0; $i < $array_size; $i++) { $new_string .= $str_array[$i][$d]; } } echo $new_string; ?> Code (markup):
Thanks very much! I had a script which worked with smaller strings, but for some reason it got buggy with larger ones. I'll implement your script, it looks perfect!
You could speed it up for large strings by moving those 2 sqrt(strlen($string))'s from while expression into a variable and reusing it. Good luck.