Transposition Cipher

Background

This cipher seeks to mix up the clear text by initially dividing it up into blocks of characters (key long), stacking these blocks and then reading the cipher text down columns to obscure the meaning.

Code

Let's try it out:

Key:
Message:
'; //plate this grid up (for display purposes ONLY) echo '

Original message: '.$neworiginal.'

'; echo ''; for ($a=0; $a < count($grid) ; $a++) { echo ''; for ($b=0; $b < $key ; $b++) { echo ''; } echo ''; } echo '
'.$grid[$a][$b].'
'; //accumulate crypttext $result = ''; for ($col=0; $col < $key ; $col++) { //for each column for ($row=0; $row < count($grid); $row++) { //for each row in that column $result .= $grid[$row][$col]; } } echo '


Ciphertext: '.$result.'

'; } elseif (isset($_POST['decrypt'])) { //reverse the above process //go get user input $original = $_POST['message'] ; $key = $_POST['key']; //hard-code spaces into something you can see (I chose hyphen"-") $neworiginal = str_replace(' ','-',$original); //pad the message with trailing "spaces" so it completely fills the grid while ((strlen($neworiginal)%$key) !=0) { $neworiginal .='-'; } //split the string into grid rows $grid = str_split($neworiginal,strlen($neworiginal)/$key); //it makes count($grid) rows, each contains $key columns echo '
'; //plate this grid up (for display purposes ONLY) echo '

Original message: '.$neworiginal.'

'; echo ''; for ($a=0; $a < count($grid) ; $a++) { echo ''; for ($b=0; $b < strlen($neworiginal)/$key ; $b++) { echo ''; } echo ''; } echo '
'.$grid[$a][$b].'
'; //accumulate crypttext $result = ''; for ($col=0; $col < strlen($neworiginal)/$key ; $col++) { //for each column for ($row=0; $row < count($grid); $row++) { //for each row in that column $result .= $grid[$row][$col]; } } echo '


Ciphertext: '.$result.'

'; } ?>


Algorithm

BEGIN
  INPUT message
  FOR each character in message
     IF character is space
        character = '-'
     ENDIF
  ENDFOR

  INPUT key

  WHILE length(message) mod key != 0
     message = message + '-'

  INPUT conversiontype

  IF conversiontype = 'encrypt'
     INIT result = ''
     grid = message split into key sized substrings
     FOR col = 0 to key-1
        FOR row = 0 to count(grid elements) - 1
           result = result + grid[row][col]
        ENDFOR
     ENDFOR   
  	 OUTPUT result
  ELSEIF conversiontype = 'decrypt'
     INIT result = ''
     grid = message split into (length(message)/key) sized substrings
     FOR col = 0 to length(message) div key - 1
        FOR row = 0 to count(grid elements) - 1
           result = result + grid[col][row]
        ENDFOR
     ENDFOR   
  	 OUTPUT result

  ENDIF
END