Skip to main content
Code Review

Return to Question

deleted 1 character in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

beginner written class Basic cryptography algorithm

I wrote 2 classes in order to implement some basic cryptography algorithm. I tried to use some object-oriented concepts of C++. I'm a beginner, so any comment or suggestion is welcome :).

First class: Message the

The header:

the cppThe .cpp file:

theThe header:

theThe .cpp file:

beginner written class

I wrote 2 classes in order to implement some basic cryptography algorithm. I tried to use some object-oriented concepts of C++. I'm a beginner, so any comment or suggestion is welcome :)

First class: Message the header:

the cpp file:

the header:

the .cpp file:

Basic cryptography algorithm

I wrote 2 classes in order to implement some basic cryptography algorithm. I tried to use some object-oriented concepts of C++. I'm a beginner, so any comment or suggestion is welcome.

First class: Message

The header:

The .cpp file:

The header:

The .cpp file:

Source Link
youyou
  • 177
  • 8

beginner written class

I wrote 2 classes in order to implement some basic cryptography algorithm. I tried to use some object-oriented concepts of C++. I'm a beginner, so any comment or suggestion is welcome :)

First class: Message the header:

#ifndef MESSAGE_H
#define MESSAGE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class Message
{
 public:
 friend class Chiffre;
 typedef std::vector<char> cryptoContainer;
 // constructors
 Message();
 Message(const std::string&);
 Message(const cryptoContainer&);
 Message(const std::string&, const int&);
 // templates for writing original and crypted messages
 template<typename T>
 int writeOriginalMessage(T& stream) const // return original message in an output stream
 {
 for (auto it = m_originalContent.begin(); it != m_originalContent.end(); it++) stream << *it;
 stream.flush();
 return 0;
 }
 template<typename T>
 int writeCryptedMessage(T& stream) const // return crypted message in an output stream
 {
 if (m_cryptedContent.empty()) return 1;
 for (auto it = m_cryptedContent.begin(); it != m_cryptedContent.end(); it++) stream << *it;
 stream.flush();
 return 0;
 }
 // getters
 cryptoContainer getOriginalMessage(void) const;
 cryptoContainer getCryptedMessage(void) const;
 // destructor
 virtual ~Message();
 protected:
 //protected setter
 void setOriginalMessage(cryptoContainer&);
 void setCryptedMessage(cryptoContainer&);
 //attributes
 int m_key;
 cryptoContainer m_originalContent;
 cryptoContainer m_cryptedContent;
};
#endif // MESSAGE_H

the cpp file:

#include "Message.h"
Message::Message(const std::string& texte):
 m_key(0),
 m_originalContent(texte.begin(),texte.end())
{
}
Message::Message(const Message::cryptoContainer& texte):
 m_key(0), // default key is 0
 m_originalContent(texte)
{
}
Message::Message(const std::string& texte, const int& clef):
 m_key(clef),
 m_originalContent(texte.begin(),texte.end())
{
}
Message::~Message()
{
}
Message::cryptoContainer Message::getOriginalMessage(void) const
{
 return m_originalContent;
}
Message::cryptoContainer Message::getCryptedMessage(void) const
{
 return m_cryptedContent;
}
void Message::setOriginalMessage(Message::cryptoContainer& msg)
{
 m_originalContent = msg;
 return;
}
void Message::setCryptedMessage(Message::cryptoContainer& msg)
{
 m_cryptedContent = msg;
 return;
}

second class: Chiffre (will implement crypto algorithms)

the header:

#ifndef CHIFFRE_H
#define CHIFFRE_H
#include <vector>
#include "Message.h"
class Chiffre
{
public:
 Chiffre();
 virtual ~Chiffre() = 0;
 static void cesar(Message&, const int&); // décale un caractère à partir d'une clef (algorithme de César)
};
#endif // CHIFFRE_H

the .cpp file:

#include "Chiffre.h"
Chiffre::Chiffre()
{
}
Chiffre::~Chiffre()
{
}
void Chiffre::cesar(Message& msg, const int& clef)
{
 Message::cryptoContainer res;
 Message::cryptoContainer orig = msg.getOriginalMessage();
 for (auto it = orig.begin(); it != orig.end(); it++) res.push_back(*it + clef);
 msg.Message::setCryptedMessage(res);
 return;
}
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /