Playfair. h
# Ifndef playfair_h
# Define playfair_h
# Include <string>
# Include <utility>
Using namespace STD;
Class Playfair
{
PRIVATE:
Char m_ch;
Int m_position;
String m_key;
String m_letter;
Void tolower (string & Str );
Bool emptykey () const;
Void keysameletter ();
Void deleteletter ();
Void addletter ();
Void initialize ();
Void changemessage (string & Str );
Bool samerow (char LCH, char RCH, pair <int, int> & P) const;
Bool samecolumn (char LCH, char RCH, pair <int, int> & P) const;
Bool isright (INT po );
Bool isbottom (INT po );
Void samerowinsert (pair <int, int> & P, string & Str );
Void samecolumninsert (pair <int, int> & P, string & Str );
Void nonsameinsert (pair <int, int> & P, string & Str );
Public:
Explicit Playfair (){};
Explicit Playfair (const string & Str );
Explicit Playfair (const char * Ch );
Playfair & operator = (const Playfair & P );
Bool operator = (const Playfair & P );
Bool Operator! = (Const Playfair & P );
String encrypt (string Str );
Void setkey (string & Key );
Void setkey (char * Ch );
};
# Endif
Pffun. cpp
# Include <iostream>
# Include <cctype>
# Include "Playfair. H"
Playfair: Playfair (const string & Str): m_key (STR)
{
Initialize ();
}
Playfair: Playfair (const char * Ch): m_key (CH)
{
Initialize ();
}
Void Playfair: tolower (string & Str)
{
String: iterator ite = Str. Begin ();
For (; ite! = Str. End (); ++ ITE)
{
If (isupper (* ITE ))
* Ite = tolower (* ITE );
}
}
Bool Playfair: emptykey () const
{
Return m_key.empty ();
}
Void Playfair: keysameletter ()
{
If (! Emptykey ())
{
String: iterator Fite, site;
Fite = m_letter.begin ();
For (INT I = 0; I <m_letter.size (); ++ I)
{
Fite = m_letter.begin () + I;
Site = Fite + 1;
For (Int J = I + 1; j <m_letter.size (); ++ J)
{
If (* Fite! = * Site)
++ Site;
Else
{
Site = m_letter.erase (SITE );
Fite = site-1;
}
}
}
}
}
Void Playfair: deleteletter ()
{
String: iterator ite = m_key.begin ();
For (; ite! = M_key.end (); ++ ITE)
{
M_letter [* ite-97] = '';
}
}
Void Playfair: addletter ()
{
If (! Emptykey ())
{
M_position = m_key.size ();
String: iterator ite = m_letter.begin ();
For (; ite! = M_letter.end (); ++ ITE)
{
If (* ite! = '')
M_key.insert (m_key.end (), * ITE );
}
M_ch = m_key [m_position];
M_key.erase (& (m_key [m_position]);
}
}
Void Playfair: Initialize ()
{
M_letter = "abcdefghijklmnopqrstuvwxyz ";
Tolower (m_key );
Keysameletter ();
Deleteletter ();
Addletter ();
}
Bool Playfair: Operator = (const Playfair & P) // The left operand cannot be a constant object.
{
Return (m_key = P. m_key );
}
Bool Playfair: Operator! = (Const Playfair & P)
{
Return! (M_key = P. m_key );
}
Playfair & Playfair: Operator = (const Playfair & P)
{
If (* This = P)
Return * this;
Else
{
String: const_iterator ite = P. m_key.begin ();
For (; ite! = P. m_key.end (); ++ ITE)
M_key.insert (m_key.end (), * ITE );
}
Return * this;
}
Void Playfair: changemessage (string & Str)
{
String: iterator Fite, site;
Fite = Str. Begin ();
Site = Fite + 1;
For (; Fite! = Str. End () & site! = Str. End ();)
{
If (* Fite = * site)
{
If (* site = 'Z ')
Site = Str. insert (site, 'A ');
Else
Site = Str. insert (site, * site + 1 );
Fite = site + 1;
Site = Fite + 1;
}
Else
{
++ Fite;
++ Site;
}
}
If (! Str. Empty () & Str. Size () % 2)
{
If (* (Str. End ()-1) = 'Z ')
Str. insert (Str. End (), 'A ');
Else
Str. insert (Str. End (), * (Str. End ()-1) + 1 );
}
}
String Playfair: encrypt (string Str)
{
If (! Emptykey ())
{
String result;
Tolower (STR );
Changemessage (STR );
Pair <int, int> po;
String: iterator Fite, site;
Fite = Str. Begin ();
For (; Fite! = Str. End (); Fite + = 2)
{
Site = Fite + 1;
If (samerow (* Fite, * site, po ))
Samerowinsert (PO, result );
Else
{
If (samecolumn (* Fite, * site, po ))
Samecolumninsert (PO, result );
Else
Nonsameinsert (PO, result );
}
}
Return result;
}
Else
Return "";
}
Bool Playfair: samerow (char LCH, char RCH, pair <int, int> & P) const
{
P. First = m_key.find (LCH );
If (P. First = string: NPOs)
P. First = m_position;
P. Second = m_key.find (RCH );
If (P. Second = string: NPOs)
P. Second = m_position;
If (P. First/5 = P. Second/5)
Return true;
Else
Return false;
}
Bool Playfair: samecolumn (char LCH, char RCH, pair <int, int> & P) const
{
P. First = m_key.find (LCH );
If (P. First = string: NPOs)
P. First = m_position;
P. Second = m_key.find (RCH );
If (P. Second = string: NPOs)
P. Second = m_position;
If (P. First % 5 = P. Second % 5)
Return true;
Else
Return false;
}
Inline bool Playfair: isright (INT Po)
{
Return (PO % 5 = 4 );
}
Inline bool Playfair: isbottom (INT Po)
{
Return (PO/5 = 4 );
}
Void Playfair: samerowinsert (pair <int, int> & P, string & Str)
{
If (isright (P. First ))
Str. insert (Str. End (), m_key [P. First-4]);
Else
Str. insert (Str. End (), m_key [P. First + 1]);
If (isright (P. Second ))
Str. insert (Str. End (), m_key [P. Second-4]);
Else
Str. insert (Str. End (), m_key [P. Second + 1]);
}
Void Playfair: samecolumninsert (pair <int, int> & P, string & Str)
{
If (isbottom (P. First ))
Str. insert (Str. End (), m_key [P. First-20]);
Else
Str. insert (Str. End (), m_key [P. First + 5]);
If (isbottom (P. Second ))
Str. insert (Str. End (), m_key [P. Second-20]);
Else
Str. insert (Str. End (), m_key [P. Second + 5]);
}
Inline void Playfair: nonsameinsert (pair <int, int> & P, string & Str)
{
Str. insert (Str. End (), m_key [P. First/5*5 + P. Second % 5]);
Str. insert (Str. End (), m_key [P. Second/5*5 + P. First % 5]);
}
Void Playfair: setkey (string & Str)
{
M_key = STR;
Initialize ();
}
Void Playfair: setkey (char * Ch)
{
M_key = CH;
Initialize ();
}