R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

Square Cipher

The Question

Implement a classic method for composing secret messages called the square code:

 

1. Normalize the input - remove all spaces and punctuation and made all letters lowercase.

2. Create a square with the letters forming rows  - if the number of letters is a perfect square, use the square root as the number of columns. Else, choose the number of columns that corresponds to the smallest square that is larger than the characters in the message, which will result in a rectangular-like shape. 

3. Read vertically down each column going left to right, adding a space character every time the end of a column is reached. 

Example

"If man was meant to stay on the ground god would have given us roots"

Normalized, the sentence is 54 characters long.

The smallest square root larger than 54 is 64 - so we will work with 8 columns:

  ifmanwas
  meanttos
  tayonthe
  groundgo
  dwouldha
  vegivenu
sroots

Reading vertically down each column, we will come up with this as the encrypted message:

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

 

1. Normalize Text

var Crypto = function(string) {
    this.string = string;
};

Crypto.prototype.normalizePlaintext = function(){
    this.string = this.string.replace(/[,!%?#$^&\s.]/gi,'').toLowerCase();
    return this.string;
}

2. Calculate number of columns


Crypto.prototype.size = function(){
    return Math.ceil(Math.sqrt(this.string.length));
}

3. Divide the normalized text into blocks of letters of the number of columns calculated above

Crypto.prototype.plaintextSegments = function() {
    var normalized = this.normalizePlaintext().split('');
    var length = this.size();

    for (var i = 0; i < normalized.length; i++) {
        if (i % length === (length - 1) && i < normalized.length -1) {
            normalized[i] += " ay";
        }
    }

    return normalized.join('').split(' ay');
}

Placeholder to determine where to split later on, can be anything

4. Cipher the text ('read' vertically down each column)

Crypto.prototype.ciphertext = function(){

    var squared = this.plaintextSegments();
    var length = squared[0].length;
    var result = '';
    var j = 0;

    function cipher(){
        while (j < length) {
            for (var i = 0; i < length; i++) {  
                if (squared[i] !== undefined) {
                    if (squared[i][j] !== undefined){

                        result += squared[i][j];
                    }
                }
            }
            j = j + 1;
            cipher();
        }
    }
    cipher();

    return result;
}

4. Normalize the ciphered text (add spaces in between each column)

Crypto.prototype.normalizeCiphertext = function(){
    var ciphered = this.ciphertext().split('');
    var length = Math.ceil(Math.sqrt(ciphered.length));

    for (var i = 0; i < ciphered.length; i++) {
        if (i % length === (length - 1) && i < ciphered.length - 1) {
            ciphered[i] += " ";
        }
    }

    return ciphered.join('');
}

Reacto: Square Cipher

By kabibster

Reacto: Square Cipher

  • 1,706