Ricerca…


introduzione

Il cifrario di cifratura a più lettere più noto è il Playfair, che tratta i digram in testo semplice come unità singole e traduce queste unità in digram di testo cifrato. L'algoritmo di Playfair si basa sull'uso di una matrice di lettere 5 x 5 costruita usando una parola chiave.

Esempio di Crittografia con crittografia di Playfair insieme a Crittografia e regola di decrittografia

Una parola chiave è "MONARCHY", quindi la matrice sarà simile
Matrice per parole chiave MONARCHY

La matrice viene costruita compilando le lettere della parola chiave (meno duplicati) da sinistra a destra e dall'alto verso il basso e quindi riempiendo il resto della matrice con le lettere rimanenti in ordine alfabetico. Il testo in chiaro è crittografato due lettere alla volta, in base alle seguenti regole:

  1. Prendi i caratteri nel testo (semplice / cifrata) e crea un gruppo di due personaggi. Se il numero di caratteri nel testo è dispari, aggiungi una lettera di riempimento (solitamente usiamo "x").
    Text = "HELLO" quindi sarebbe group as
    LUI | LL | BUE

  2. Due lettere in chiaro che cadono nella stessa riga della matrice sono sostituite ciascuna dalla lettera a destra, con il primo elemento della riga che segue circolarmente l'ultimo. Ad esempio, ar è crittografato come RM.

  3. Due lettere di testo in chiaro che cadono nella stessa colonna sono sostituite ciascuna dalla lettera sottostante, con l'elemento superiore della colonna che segue circolarmente l'ultimo. Ad esempio, mu è crittografato come CM.

  4. Altrimenti, ogni lettera di testo in chiaro in una coppia viene sostituita dalla lettera che si trova nella sua stessa riga e dalla colonna occupata dall'altra lettera in chiaro. Quindi, hs diventa BP e ea diventa IM (o JM, come desidera la cifratura).

Di seguito è riportato un semplice programma Java che implementa il codice di gioco di Playfair:

import java.util.Scanner;


public class Playfair {
public static void main(String[] args) {
    Scanner in=new Scanner(System.in);

    System.out.print("Enter keyword: ");
    String key=in.nextLine();
    System.out.print("Enter message to encrypt: ");
    String msg=in.nextLine();

    PFEncryption pfEncryption=new PFEncryption();
    pfEncryption.makeArray(key);
    msg=pfEncryption.manageMessage(msg);
    pfEncryption.doPlayFair(msg, "Encrypt");
    String en=pfEncryption.getEncrypted();
    System.out.println("Encrypting....\n\nThe encrypted text is: " + en);
    System.out.println("=======================================");
    pfEncryption.doPlayFair(en, "Decrypt");
    System.out.print("\nDecrypting....\n\nThe encrypted text is: " + pfEncryption.getDecrypted());
}
}

class PFEncryption{

private char [][] alphabets= new char[5][5];
private char[] uniqueChar= new char[26];
private String ch="ABCDEFGHIKLMNOPQRSTUVWXYZ";
private String encrypted="";
private String decrypted="";

void makeArray(String keyword){
    keyword=keyword.toUpperCase().replace("J","I");
    boolean present, terminate=false;
    int val=0;
    int uniqueLen;
    for (int i=0; i<keyword.length(); i++){
        present=false;
        uniqueLen=0;
        if (keyword.charAt(i)!= ' '){
            for (int k=0; k<uniqueChar.length; k++){
                if (Character.toString(uniqueChar[k])==null){
                    break;
                }
                uniqueLen++;
            }
            for (int j=0; j<uniqueChar.length; j++){
                if (keyword.charAt(i)==uniqueChar[j]){
                    present=true;
                }
            }
            if (!present){
                uniqueChar[val]=keyword.charAt(i);
                val++;
            }
        }
        ch=ch.replaceAll(Character.toString(keyword.charAt(i)), "");
    }

    for (int i=0; i<ch.length(); i++){
        uniqueChar[val]=ch.charAt(i);
        val++;
    }
    val=0;

    for (int i=0; i<5; i++){
        for (int j=0; j<5; j++){
            alphabets[i][j]=uniqueChar[val];
            val++;
            System.out.print(alphabets[i][j] + "\t");
        }
        System.out.println();
    }
    }

    String manageMessage(String msg){
    int val=0;
    int len=msg.length()-2;
    String newTxt="";
    String intermediate="";
    while (len>=0){
        intermediate=msg.substring(val, val+2);
        if (intermediate.charAt(0)==intermediate.charAt(1)){
            newTxt=intermediate.charAt(0) + "x" + intermediate.charAt(1);
            msg=msg.replaceFirst(intermediate, newTxt);
            len++;
        }
        len-=2;
        val+=2;
    }

    if (msg.length()%2!=0){
        msg=msg+'x';
        }
        return msg.toUpperCase().replaceAll("J","I").replaceAll(" ","");
    }

void doPlayFair(String msg, String tag){
    int val=0;
    while (val<msg.length()){
        searchAndEncryptOrDecrypt(msg.substring(val, val + 2), tag);
        val+=2;
    }
}

void searchAndEncryptOrDecrypt(String doubblyCh, String tag){
    char ch1=doubblyCh.charAt(0);
    char ch2=doubblyCh.charAt(1);
    int row1=0, col1=0, row2=0, col2=0;
    for (int i=0; i<5; i++){
        for (int j=0; j<5; j++){
            if (alphabets[i][j]==ch1){
                row1=i;
                col1=j;
            }else if (alphabets[i][j]==ch2){
                row2=i;
                col2=j;
            }
        }
    }
    if (tag=="Encrypt")
        encrypt(row1, col1, row2, col2);
    else if(tag=="Decrypt")
        decrypt(row1, col1, row2, col2);
}

void encrypt(int row1, int col1, int row2, int col2){
    if (row1==row2){
        col1=col1+1;
        col2=col2+1;
        if (col1>4)
            col1=0;
        if (col2>4)
            col2=0;
        encrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row1][col2]));
    }else if(col1==col2){
        row1=row1+1;
        row2=row2+1;
        if (row1>4)
            row1=0;
        if (row2>4)
            row2=0;
        encrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row2][col1]));
    }else{
        encrypted+=(Character.toString(alphabets[row1][col2])+Character.toString(alphabets[row2][col1]));
    }
}

void decrypt(int row1, int col1, int row2, int col2){
    if (row1==row2){
        col1=col1-1;
        col2=col2-1;
        if (col1<0)
            col1=4;
        if (col2<0)
            col2=4;
        decrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row1][col2]));
    }else if(col1==col2){
        row1=row1-1;
        row2=row2-1;
        if (row1<0)
            row1=4;
        if (row2<0)
            row2=4;
        decrypted+=(Character.toString(alphabets[row1][col1])+Character.toString(alphabets[row2][col1]));
    }else{
        decrypted+=(Character.toString(alphabets[row1][col2])+Character.toString(alphabets[row2][col1]));
    }
}

String getEncrypted(){
    return encrypted;
}
String getDecrypted(){
    return decrypted;
}



}


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow