patterncppMinor
Playfair Cipher in C++
Viewed 0 times
playfaircipherstackoverflow
Problem
I have been working on a Playfair Cipher in C++ for a final Project in a Cryptography class. I was hoping for some feedback on the project so far. I recently got a job programming before I've even graduated and I feel like I've already learned so much.
I would just like some feedback on my code style, comments, formatting, and structure.
The code below is the PlayfairCipher class. The code is fairly simple.
```
#include "PlayfairCipher.h"
#include "Sanitizer.h"
#include
#include
using namespace std;
PlayfairCipher :: PlayfairCipher(void)
{
playfairMatrix.resize(5);
for (int i = 0; i row][a->column];
}
/////////////////////////////////////////////////////////////////////////////////
/// First, a key matrix is created using the supplied key. ///
/// Then the plaintext is sanitized to remove invalid characters. ///
/// For each pair of characters in plainText, retrieve their coordinates from ///
/// the key Matrix. The coordinates are then transformed. Finally, the newly ///
/// transformed coordinates are used to retrieve the corresponding letter. ///
/////////////////////////////////////////////////////////////////////////////////
string PlayfairCipher :: Encrypt(string plainText, string key)
{
createKeyMatrix(key);
Sanitizer :: sanitizePlainText(plainText);
for(unsigned int i = 0; i column == b->column) {
a->row = (a->row + 1) % 5;
b->row = (b->row + 1) % 5;
}
else if(a->row == b->row) {
a->column = (a->column + 1) % 5;
b->column = (b->column + 1) % 5;
}
else {
int temp = a->column;
a->column = b->column;
b->column = temp;
}
}
void PlayfairCipher :: decryptCoordinates(Point a, Point b) {
if(a->column == b->column) {
a->row = Mod(a->row - 1, 5);
b->row = Mod(b->row - 1,
I would just like some feedback on my code style, comments, formatting, and structure.
The code below is the PlayfairCipher class. The code is fairly simple.
```
#include "PlayfairCipher.h"
#include "Sanitizer.h"
#include
#include
using namespace std;
PlayfairCipher :: PlayfairCipher(void)
{
playfairMatrix.resize(5);
for (int i = 0; i row][a->column];
}
/////////////////////////////////////////////////////////////////////////////////
/// First, a key matrix is created using the supplied key. ///
/// Then the plaintext is sanitized to remove invalid characters. ///
/// For each pair of characters in plainText, retrieve their coordinates from ///
/// the key Matrix. The coordinates are then transformed. Finally, the newly ///
/// transformed coordinates are used to retrieve the corresponding letter. ///
/////////////////////////////////////////////////////////////////////////////////
string PlayfairCipher :: Encrypt(string plainText, string key)
{
createKeyMatrix(key);
Sanitizer :: sanitizePlainText(plainText);
for(unsigned int i = 0; i column == b->column) {
a->row = (a->row + 1) % 5;
b->row = (b->row + 1) % 5;
}
else if(a->row == b->row) {
a->column = (a->column + 1) % 5;
b->column = (b->column + 1) % 5;
}
else {
int temp = a->column;
a->column = b->column;
b->column = temp;
}
}
void PlayfairCipher :: decryptCoordinates(Point a, Point b) {
if(a->column == b->column) {
a->row = Mod(a->row - 1, 5);
b->row = Mod(b->row - 1,
Solution
Code looks clean and readable. 3 quick remarks:
- Magic numbers and string: Move'm aside. What's with that
5? Is that a cosmic constant, or something your boss may change tomorrow? Make it#defineorextern.
- Defend your code. Your
Point*creation method is allowed to returnNULL, but you don't null-check your input.
- Code duplication - encrypt/decrypt coordinates are almost the same. Consider combining.
Context
StackExchange Code Review Q#18407, answer score: 3
Revisions (0)
No revisions yet.