cryptography
플레이 페어 암호
수색…
소개
가장 잘 알려진 다중 문자 암호화 암호는 일반 텍스트의 digram을 단일 단위로 취급하고이 단위를 암호문 digrams로 변환하는 Playfair입니다. Playfair 알고리즘은 키워드를 사용하여 구성된 5 x 5 행렬의 문자 사용을 기반으로합니다.
암호화 및 복호화 규칙과 함께 플레이 페어 암호 암호화의 예
키워드가 "MONARCHY"이면 매트릭스가 다음과 같이 보입니다.
행렬은 왼쪽에서 오른쪽으로 그리고 위에서 아래로 키워드 (마이너스 중복)의 문자를 채운 다음 남은 문자를 알파벳순으로 채 웁니다. 다음 규칙에 따라 일반 텍스트는 한 번에 두 글자로 암호화됩니다.
텍스트 (일반 / 암호)의 문자를 가져 와서 두 개의 문자 그룹을 만듭니다. 텍스트의 문자 수가 이상한 경우 필러 문자를 추가하십시오 (보통 'x'를 사용합니다).
Text = "HELLO"그러면 그룹으로 표시됩니다.
그는 | LL | 소행렬의 같은 행에 속하는 두 개의 일반 텍스트는 각각 오른쪽 끝에있는 문자로 대체되며 행의 첫 번째 요소는 마지막 행에 이어 순환합니다. 예를 들어, ar은 RM으로 암호화됩니다.
같은 열에있는 두 개의 일반 텍스트는 각각 아래에있는 문자로 바뀌고 마지막 열에는 순환으로 맨 위의 요소가옵니다. 예를 들어, mu는 CM으로 암호화됩니다.
그렇지 않으면 한 쌍의 각 일반 텍스트는 자체 행에있는 문자로 대체되고 다른 일반 텍스트로 사용되는 열로 대체됩니다. 따라서, hs는 BP가되고 ea는 IM (또는 JM, encipherer가 원하는대로)이됩니다.
Playfair Cipher를 구현하는 간단한 Java 프로그램은 다음과 같습니다.
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;
}
}