samedi 14 septembre 2019

How to fix Playfair cipher encryption pairs not fully 'splitting'

Playfair cipher requires the separation of 2 repeating words and append a filler word in between them, for example: 1) balloon will become ba lx lo on, 2) hall will become ha lx lx. The code is able to achieve example number 1 but not number 2. What would be the logic error here?

length = (int) msg.length() / 2 + msg.length() % 2; 
    for (int i = 0; i < (length - 1); i++)
    {
        if (msg.charAt(2 * i) == msg.charAt(2 * i + 1))
        {
            msg = new StringBuffer(msg).insert(2 * i + 1, 'X').toString();
            length = (int) msg.length() / 2 + msg.length() % 2;
        }
    }  

    String ciphertext = "";
    String[] encDigraphs = new String[length];
    encDigraphs = encodeDigraph(digraph);
    for (int k = 0; k < length; k++)
        ciphertext = ciphertext + encDigraphs[k];
    return ciphertext;

Aucun commentaire:

Enregistrer un commentaire