<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>CFGReader</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Vigenere encoding </b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is a quite simple demo of what I read from book &lt;battle of wits&gt; which described the code-breaking war in </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>world war II. </b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>Encoding can be as early as 
in Roman times, it said in &quot;discrete mathematics&quot; text book that Caesar encoded 
his</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>message to his general with 
a very simple &quot;shift&quot;. The idea is simple but I think it is the essential of </b>
</font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>encoding. The real problem 
is how to shift. No matter how complicated calculating formula you might use for 
each</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>character to shift. 
Nevertheless, the rule for each character is fixed. A simple statistic frequency 
of each </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>character in English 
language will reveal the secret. Therefore, encoded character MUST show 
different &quot;face&quot;</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>in same message. For 
example, the message &quot;I am here&quot; contains two &quot;e&quot;'s. In ciphered text, two &quot;e&quot; 
should be</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>different letters. 
Otherwise, no matter what kind of permutation or combination you make for the 
message. The </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>frequency of &quot;showing-up&quot; 
will reveal that cipher character is &quot;e&quot;.</b></font></span></p>
<div align="left">
  <p ALIGN="LEFT">　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>Look at the following table of Vigenere, 
  the top row is the plain text and from that row below, each line is </b>
  </font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>a key letter. And each of them shifts by 
  one character. </b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>The essence of this kind of encoding is 
  just use a &quot;key&quot; which is arbitrary. Then align the key to plain text so</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>that you will use following formula to 
  find ciphered character: coded = table[cipher, plain] where &quot;plain&quot; is </b>
  </font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>the character to be encoded. The cipher 
  is the &quot;key&quot; letter when you align the key one after another to plain </b>
  </font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>text.</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>For example, key=&quot;CIPHER&quot;; plain text =&quot;IAMHEREWHEREAREYOU&quot;;</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>To align the key:</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>CIPHERCIPHERCIPHER</b></font></span></div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>IAMHEREWHEREAREYOU</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  　</div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca"><font size="2"><b>&nbsp; A B C D E F G H I J K L M N O P Q 
  R S T U V W X Y Z<br>
  A A B C D E F G H I J K L M N O P Q R S T U V W X Y Z<br>
  <br>
  B B C D E F G H I J K L M N O P Q R S T U V W X Y Z A<br>
  <br>
  C C D E F G H I J K L M N O P Q R S T U V W X Y Z A B<br>
  <br>
  D D E F G H I J K L M N O P Q R S T U V W X Y Z A B C<br>
  <br>
  E E F G H I J K L M N O P Q R S T U V W X Y Z A B C D<br>
  <br>
  F F G H I J K L M N O P Q R S T U V W X Y Z A B C D E<br>
  <br>
  G G H I J K L M N O P Q R S T U V W X Y Z A B C D E F<br>
  <br>
  H H I J K L M N O P Q R S T U V W X Y Z A B C D E F G<br>
  <br>
  I I J K L M N O P Q R S T U V W X Y Z A B C D E F G H<br>
  <br>
  J J K L M N O P Q R S T U V W X Y Z A B C D E F G H I<br>
  <br>
  K K L M N O P Q R S T U V W X Y Z A B C D E F G H I J<br>
  <br>
  L L M N O P Q R S T U V W X Y Z A B C D E F G H I J K<br>
  <br>
  M M N O P Q R S T U V W X Y Z A B C D E F G H I J K L<br>
  <br>
  N N O P Q R S T U V W X Y Z A B C D E F G H I J K L M<br>
  <br>
  O O P Q R S T U V W X Y Z A B C D E F G H I J K L M N<br>
  <br>
  P P Q R S T U V W X Y Z A B C D E F G H I J K L M N O<br>
  <br>
  Q Q R S T U V W X Y Z A B C D E F G H I J K L M N O P<br>
  <br>
  R R S T U V W X Y Z A B C D E F G H I J K L M N O P Q<br>
  <br>
  S S T U V W X Y Z A B C D E F G H I J K L M N O P Q R<br>
  <br>
  T T U V W X Y Z A B C D E F G H I J K L M N O P Q R S<br>
  <br>
  U U V W X Y Z A B C D E F G H I J K L M N O P Q R S T<br>
  <br>
  V V W X Y Z A B C D E F G H I J K L M N O P Q R S T U<br>
  <br>
  W W X Y Z A B C D E F G H I J K L M N O P Q R S T U V<br>
  <br>
  X X Y Z A B C D E F G H I J K L M N O P Q R S T U V W<br>
  <br>
  Y Y Z A B C D E F G H I J K L M N O P Q R S T U V W X<br>
  <br>
  Z Z A B C D E F G H I J K L M N O P Q R S T U V W X Y<br>
　</b></font></span></div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>When I read more about the book.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1<span lang="en-ca">. vigenere.cpp (main)</span></b></font></pre>
  <pre>　</pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: 24revisited.cpp</b></font></span></pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;


class Vigenere
{
private:
	int* key;
	int keyLen;
	bool validateKey(const char* theKey);
	void run(const char* text, char* target, bool encoding=true);
public:
	void setKey(const char* theKey);
	void printTable();
	
	void encode(char* text, char* coded);
	void decode(const char* text, char* decoded);
	
};


int main()
{
	char buffer[256];
	char message[256];
	char text[256];
	strcpy(text, &quot;I am here&quot;);
	Vigenere V;
	V.setKey(&quot;xyz&quot;);
	//V.printTable();
	V.encode(text, buffer);
	cout&lt;&lt;buffer&lt;&lt;endl;
	V.decode(buffer, message);
	cout&lt;&lt;message&lt;&lt;endl;
	return 0;

}

void Vigenere::encode(char* text, char* coded)
{	
	char* pt=text, *ps=text;
	while (*ps!='\0')
	{
		if (!isalpha(*ps))
		{
			ps++;			
		}
		else
		{
			*pt=toupper(*ps);
			ps++;
			pt++;
		}
	}
	*pt='\0';

	run(text, coded, true);
}

void Vigenere::decode(const char* text, char* decoded)
{
	run(text, decoded, false);
}

//just make sure the length of key won't exceed 26
//make sure the letter in key won't repeat
bool Vigenere::validateKey(const char*theKey)
{
	/*
	bool chUsed[26];
	int theLen;
	theLen=strlen(theKey);
	if (theLen&gt;26)
	{
		cout&lt;&lt;&quot;the key is too long\n&quot;;
		return false;
	}
	for (int i=0; i&lt;26; i++)
	{
		chUsed[i]=false;
	}
	for (i=0; i&lt;theLen; i++)
	{
		char ch=toupper(theKey[i]);
		if (chUsed[ch-'A'])
		{
			return false;
		}
		else
		{
			chUsed[ch-'A']=true;
		}
	}
	*/
	return true;
}

void Vigenere::setKey(const char* theKey)
{
	int i=0;
	if (validateKey(theKey))
	{
		keyLen=strlen(theKey);
		key=new int[keyLen];
		while (theKey[i]!='\0')
		{
			key[i]=toupper(theKey[i])-'A';			
			i++;
		}		
	}
}

void Vigenere::printTable()
{
	for (int i=0; i&lt;26; i++)
	{
		if (i==0)
		{
			cout&lt;&lt;&quot; &quot;;
			for (int j=0; j&lt;26; j++)
			{
				cout&lt;&lt;&quot; &quot;&lt;&lt;(char)('A'+j);
			}
		}
		cout&lt;&lt;&quot;\n&quot;;
		cout&lt;&lt;(char)('A'+i);
		for (int j=0; j&lt;26; j++)
		{
			cout&lt;&lt;&quot; &quot;&lt;&lt;(char)('A'+(j+i)%26);
		}
		cout&lt;&lt;&quot;\n&quot;;
	}
	cout&lt;&lt;&quot;\n&quot;;
}

void Vigenere::run(const char* text, char* target, bool encoding)
{
	int index=0;
	int keyIndex;
	while (text[index]!='\0')
	{
		/*
		if (!isalpha(text[index]))
		{
			if (encoding)
			{
				index++;
				continue;
			}
			else
			{
				cout&lt;&lt;&quot;corrupted coding!\n&quot;;
				exit(1);
			}
		}
		*/
		char ch=text[index];//it must be in capital case
		keyIndex=index%keyLen;

		if (encoding)
		{
			target[index]=(char)('A'+(ch-'A'+key[keyIndex])%26);
		}
		else
		{
			if (ch-'A'-key[keyIndex]&gt;=0)
			{
				target[index]=(char)('A'+ ch-'A'-key[keyIndex]);
			}
			else
			{
				target[index]=(char)('A'+26+ch-'A'-key[keyIndex]);
			}
		}
		index++;		
	}
	target[index]='\0';
}



</pre>
<pre></pre>
<pre><font color="#0000FF"><b><span lang="en-ca">The input is something like following:</span></b></font></pre>
<pre><font color="#0000FF"><b>Here is the <a name="result"></a>result:</b></font></pre>

<pre>
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="game24.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>