이 방식에서는 단순 치환 암호의 경우와 같이 한 글자가 아닌 한 쌍의 글자를 암호화한다.
playfair cipher에서는 처음에 키 테이블이 생성됩니다. 키 테이블은 일반 텍스트를 암호화하는 키 역할을 하는 5×5 알파벳 그리드입니다. 25개의 알파벳 각각은 고유해야 하며 26개 대신 25개의 알파벳만 필요하므로 테이블에서 알파벳의 한 글자(보통 J)가 생략됩니다. 일반 텍스트에 J가 포함되어 있으면 I로 대체됩니다.
보낸 사람과 받는 사람이 특정 키에 대해 '자습서'라고 말합니다. 키 테이블에서 테이블의 첫 번째 문자(왼쪽에서 오른쪽으로)는 중복 문자를 제외한 구입니다. 나머지 테이블은 자연 순서대로 나머지 알파벳 문자로 채워집니다. 키 테이블은 다음과 같이 작동합니다. -

Playfair 암호 프로세스
먼저 일반 텍스트 메시지를 두 글자(이중 문자) 쌍으로 분할합니다. 홀수 문자가 있는 경우 마지막 문자에 Z가 추가됩니다. "돈 숨기기" 메시지를 암호화하고 싶다는 점을 고려해 보겠습니다. 다음과 같이 작성됩니다 -
HI DE MO NE YZ
암호화 규칙은 -
-
두 글자가 같은 열에 있는 경우 각 글자 아래에 있는 글자를 취합니다(아래에 있으면 맨 위로 이동) 'H'와 'I'가 같은 열에 있으므로 그 아래에 있는 글자로 대체합니다. 안녕 → 품질 관리

-
두 문자가 같은 행에 있으면 각 문자의 오른쪽에 있는 문자를 가져옵니다(가장 오른쪽에 있으면 왼쪽으로 돌아감) 'D'와 'E'가 같은 행에 있으므로 오른쪽에 있는 문자를 가져옵니다. 교체. 드 → EF

-
앞의 두 규칙 중 어느 것도 참이 아닌 경우 두 글자로 사각형을 만들고 사각형의 반대쪽 수평 모서리에 있는 글자를 가져옵니다.

이 규칙을 사용하여 'tutorials' 키를 사용하여 'hide money'를 암호화한 결과는 -
QC EF NU MF ZV
Playfair 암호 해독은 동일한 프로세스를 역순으로 수행하는 것처럼 간단합니다. 수신자는 동일한 키를 가지며 동일한 키 테이블을 생성한 다음 해당 키를 사용하여 만든 메시지를 해독할 수 있습니다.
다음은 Playfair Cipher를 사용하여 메시지를 인코딩하는 C++ 프로그램입니다.
알고리즘
Begin Function void play( int dir ) For it = msg.begin() to it != msg.end() If ( getPos( *it++, j, k ) ) If ( getPos( *it, p, q) ) If ( j == p ) nmsg+= getChar( j, k + dir ) nmsg += getChar( p, q + dir ) else if( k == q ) nmsg += getChar( j + dir, k ) nmsg += getChar( p + dir, q ) else nmsg += getChar( p, k ) nmsg += getChar( j, q ) done done done msg = nmsg done End
예시
#include <iostream>
#include <string>
using namespace std;
class playfair {
public:
string msg; char n[5][5];
void play( string k, string t, bool m, bool e ) {
createEncoder( k, m );
getText( t, m, e );
if( e )
play( 1 );
else
play( -1 );
print();
}
private:
void play( int dir ) {
int j,k,p,q;
string nmsg;
for( string::const_iterator it = msg.begin(); it != msg.end(); it++ ) {
if( getPos( *it++, j, k ) )
if( getPos( *it, p, q) ) {
//for same row
if( j == p ) {
nmsg+= getChar( j, k + dir );
nmsg += getChar( p, q + dir );
}
//for same column
else if( k == q ) {
nmsg += getChar( j + dir, k );
nmsg += getChar( p + dir, q );
} else {
nmsg += getChar( p, k );
nmsg += getChar( j, q );
}
}
}
msg = nmsg;
}
void print() //print the solution {
cout << "\n\n Solution:" << endl;
string::iterator it = msg.begin(); int count = 0;
while( it != msg.end() ) {
cout << *it;
it++;
cout << *it << " ";
it++;
if( ++count >= 26 )
cout << endl;
count = 0;
}
cout << endl << endl;
}
char getChar( int a, int b ) { //get the characters
return n[ (b + 5) % 5 ][ (a + 5) % 5 ];
}
bool getPos( char l, int &c, int &d ) { //get the position
for( int y = 0; y < 5; y++ )
for( int x = 0; x < 5; x++ )
if( n[y][x] == l ) {
c = x;
d= y;
return true;
}
return false;
}
void getText( string t, bool m, bool e ) { //get the original message
for( string::iterator it = t.begin(); it != t.end(); it++ ) {
//to choose J = I or no Q in the alphabet.
*it = toupper( *it );
if( *it < 65 || *it > 90 )
continue;
if( *it == 'J' && m )
*it = 'I';
else if( *it == 'Q' && !m )
continue;
msg += *it;
} if( e ) {
string nmsg = ""; size_t len = msg.length();
for( size_t x = 0; x < len; x += 2 ) {
nmsg += msg[x];
if( x + 1 < len ) {
if( msg[x] == msg[x + 1] ) nmsg += 'X';
nmsg += msg[x + 1];
}
}
msg = nmsg;
}
if( msg.length() & 1 )
msg += 'X';
}
void createEncoder( string key, bool m ) { //creation of the key table
if( key.length() < 1 )
key= "KEYWORD";
key += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string s= "";
for( string::iterator it = key.begin(); it != key.end(); it++ ) {
*it = toupper( *it );
if( *it < 65 || *it > 90 )
continue;
if( ( *it == 'J' && m ) || ( *it == 'Q' && !m ) )
continue;
if( s.find( *it ) == -1 )
s += *it;
}
copy( s.begin(), s.end(), &n[0][0] );
}
};
int main( int argc, char* argv[] ) {
string k, i, msg;
bool m, c;
cout << "Encrpty or Decypt? ";
getline( cin, i );
c = ( i[0] == 'e' || i[0] == 'E' );
cout << "Enter a key: ";
getline( cin, k);
cout << "I <-> J (Y/N): ";
getline( cin, i );
m = ( i[0] == 'y' || i[0] == 'Y' );
cout << "Enter the message: ";
getline( cin, msg );
playfair pf;
pf.play( k, msg,m, c );
return system( "pause" );
} 출력
Encrpty or Decypt? d Enter a key: players I <-> J (Y/N): y Enter the message: OK GC GC MZ MQ CF YA RL QH OM Solution: TH IS IS TU TO RI AL SP OI NT