Caesar box / square script help urgently needed

Discussion in 'PHP' started by robokoder, Jan 1, 2006.

  1. #1
    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
     
    robokoder, Jan 1, 2006 IP
  2. n0other

    n0other Peon

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?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):
     
    n0other, Jan 1, 2006 IP
  3. robokoder

    robokoder Peon

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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!
     
    robokoder, Jan 3, 2006 IP
  4. n0other

    n0other Peon

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    n0other, Jan 3, 2006 IP