Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) MultiEncrangerVcl

 

MultiEncranger (VCL) is a tool and a multi-line version of Encranger.

 

MultiEncranger (VCL) is the predecessor of MultiEncranger.

 

 

 

 

 

Downloads

 

Technical facts

 

 

 

 

 

 

./ToolMultiEncrangerVcl/ProjectMultiEncranger.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("UnitFormMain.cpp", FormMain);
USEFORM("UnitFormAbout.cpp", FormAbout);
USEFORM("UnitFormWhatsNew.cpp", FormWhatsNew);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 Application->Initialize();
                 Application->Title = "MultiEncranger";
                 Application->CreateForm(__classid(TFormMain), &FormMain);
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        catch (...)
        {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
        }
        return 0;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitEncranger.h

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitEncrangerH
#define UnitEncrangerH
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
//Encranger stands for 'ENCryption by RAndom Number GEneratoR'
struct Encranger
{
  Encranger(const int key);
  const std::string Encrypt(std::string s) const;
  const std::string Deencrypt(std::string s) const;

  const std::vector<char> characters;
  const std::vector<int>  table;

  private:

  const char Encrypt(const char c, const int d) const;
  const char Deencrypt(const char c, const int d) const;
  const int GetIndex(const char c) const;

  const std::vector<int> CreateTable(const int key, const unsigned int sz) const;
  const std::vector<char> CreateCharacters() const;

};
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl
struct Increase : public std::unary_function<void,int>
{
  explicit Increase(const int initValue = 0) : mValue(initValue) {}
  void operator()(int& anything)
  {
    anything = mValue;
    ++mValue;
  }

  private:
  int mValue;
};
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitEncranger.cpp

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#pragma hdrstop

#include <cassert>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <algorithm>
#include "UnitLoopReader.h"
#include "UnitEncranger.h"

//---------------------------------------------------------------------------
Encranger::Encranger(const int key)
  : characters(CreateCharacters()),
    table(CreateTable(key,characters.size()))
{

}
//---------------------------------------------------------------------------
const std::string Encranger::Encrypt(std::string s) const
{
  typedef std::string::iterator StringIterator;
  LoopReader<std::vector<int>::const_iterator> table_reader(table.begin(), table.end());

  const StringIterator j = s.end();
  for (StringIterator i = s.begin(); i!=j; ++i, table_reader.Next())
  {
    *i = Encrypt(*i,*table_reader.Read());
  }
  return s;
}
//---------------------------------------------------------------------------
const std::string Encranger::Deencrypt(std::string s) const
{
  typedef std::string::iterator StringIterator;
  LoopReader<std::vector<int>::const_iterator> table_reader(table.begin(), table.end());

  const StringIterator j = s.end();
  for (StringIterator i = s.begin(); i!=j; ++i, table_reader.Next())
  {
    *i = Deencrypt(*i,*table_reader.Read());
  }
  return s;
}
//---------------------------------------------------------------------------
const char Encranger::Encrypt(const char c, const int d) const
{
  const int i = GetIndex(c);
  const int n_chars = static_cast<int>(characters.size());
  const int i_new = (i + d) % n_chars;
  assert(i_new >= 0);
  assert(i_new < static_cast<int>(characters.size()));
  return characters[i_new];
}
//---------------------------------------------------------------------------
const char Encranger::Deencrypt(const char c, const int d) const
{
  const int i = GetIndex(c);
  const int n_chars = static_cast<int>(characters.size());
  const int i_new = (i - d + n_chars) % n_chars;
  assert(i_new >= 0);
  assert(i_new < static_cast<int>(characters.size()));
  return characters[i_new];
}
//---------------------------------------------------------------------------
const int Encranger::GetIndex(const char c) const
{
  const std::vector<char>::const_iterator i
    = std::find(characters.begin(), characters.end(), c);
  assert(i!=characters.end());
  return i - characters.begin();
}
//---------------------------------------------------------------------------
const std::vector<int> Encranger::CreateTable(const int key, const unsigned int sz) const
{
  assert(sz!=0);

  std::vector<int> v(sz);
  std::for_each(v.begin(), v.end(), Increase() );

  assert(sz > 5); //Only for these tests below
  assert(v[0] == 0);
  assert(v[1] == 1);
  assert(v[2] == 2);
  assert(v[3] == 3);
  assert(v[4] == 4);

  //The key is the seed
  std::srand(key);

  //Shuffle
  std::random_shuffle(v.begin(),v.end());

  return v;
}
//---------------------------------------------------------------------------
const std::vector<char> Encranger::CreateCharacters() const
{
  std::vector<char> v;
  //Uppercase
  v.push_back('A');
  v.push_back('B');
  v.push_back('C');
  v.push_back('D');
  v.push_back('E');
  v.push_back('F');
  v.push_back('G');
  v.push_back('H');
  v.push_back('I');
  v.push_back('J');
  v.push_back('K');
  v.push_back('L');
  v.push_back('M');
  v.push_back('N');
  v.push_back('O');
  v.push_back('P');
  v.push_back('Q');
  v.push_back('R');
  v.push_back('S');
  v.push_back('T');
  v.push_back('U');
  v.push_back('V');
  v.push_back('W');
  v.push_back('X');
  v.push_back('Y');
  v.push_back('Z');
  //Lowercase
  v.push_back('a');
  v.push_back('b');
  v.push_back('c');
  v.push_back('d');
  v.push_back('e');
  v.push_back('f');
  v.push_back('g');
  v.push_back('h');
  v.push_back('i');
  v.push_back('j');
  v.push_back('k');
  v.push_back('l');
  v.push_back('m');
  v.push_back('n');
  v.push_back('o');
  v.push_back('p');
  v.push_back('q');
  v.push_back('r');
  v.push_back('s');
  v.push_back('t');
  v.push_back('u');
  v.push_back('v');
  v.push_back('w');
  v.push_back('x');
  v.push_back('y');
  v.push_back('z');
  //Digits
  v.push_back('0');
  v.push_back('1');
  v.push_back('2');
  v.push_back('3');
  v.push_back('4');
  v.push_back('5');
  v.push_back('6');
  v.push_back('7');
  v.push_back('8');
  v.push_back('9');
  //Above digits
  v.push_back('!');
  v.push_back('@');
  v.push_back('#');
  v.push_back('$');
  v.push_back('%');
  v.push_back('^');
  v.push_back('&');
  v.push_back('*');
  v.push_back('(');
  v.push_back(')');
  //Other interpunction
  v.push_back('~');
  v.push_back('`');
  v.push_back('-');
  v.push_back('_');
  v.push_back('=');
  v.push_back('+');
  v.push_back('[');
  v.push_back(']');
  v.push_back('{');
  v.push_back('}');
  v.push_back(';');
  v.push_back(':');
  v.push_back('\'');
  v.push_back('\"');
  v.push_back('<');
  v.push_back('>');
  v.push_back(',');
  v.push_back('.');
  v.push_back('/');
  v.push_back('?');
  v.push_back('\\');
  v.push_back('|');
  v.push_back(' ');
  return v;
}
//---------------------------------------------------------------------------
#pragma package(smart_init)

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitFormAbout.h

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormAboutH
#define UnitFormAboutH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TFormAbout : public TForm
{
__published: // IDE-managed Components
        TRichEdit *RichEdit;
        TImage *ImageAuthor;
        TPanel *PanelTop;
        TPanel *PanelTopRight;
        TPanel *PanelWebsite;
        TPanel *PanelLicence;
        TPanel *PanelDate;
        TPanel *PanelAuthor;
        TPanel *PanelCopyright;
        TPanel *PanelTitle;
        TPanel *PanelVersion;
        TButton *ButtonWhatsNew;
        TPanel *PanelExplain;
        void __fastcall ButtonWhatsNewClick(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TFormAbout(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormAbout *FormAbout;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitFormAbout.cpp

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <boost/scoped_ptr.hpp>
#include "UnitFormAbout.h"
#include "UnitFormWhatsNew.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormAbout *FormAbout;
//---------------------------------------------------------------------------
__fastcall TFormAbout::TFormAbout(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormAbout::ButtonWhatsNewClick(TObject *Sender)
{
  boost::scoped_ptr<TFormWhatsNew> f(new TFormWhatsNew(0));
  f->ShowModal();
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitFormMain.h

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormMainH
#define UnitFormMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Chart.hpp>
#include <ExtCtrls.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
#include <Graphics.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
#include <vector>
//---------------------------------------------------------------------------
class TFormMain : public TForm
{
__published: // IDE-managed Components
        TPanel *PanelPlainText;
        TPanel *PanelMiddle;
        TPanel *PanelCodedText;
        TButton *ButtonEncrypt;
        TButton *ButtonDeencrypt;
        TImage *ImageDown;
        TImage *ImageUp;
        TEdit *EditKey;
        TLabel *LabelKey;
        TButton *ButtonAbout;
        TRichEdit *EditPlainText;
        TRichEdit *EditCodedText;
        void __fastcall ButtonEncryptClick(TObject *Sender);
        void __fastcall ButtonDeencryptClick(TObject *Sender);
        void __fastcall ButtonAboutClick(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TFormMain(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormMain *FormMain;
//---------------------------------------------------------------------------
const std::vector<int> Evaluate();
//---------------------------------------------------------------------------


#endif

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitFormMain.cpp

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <cassert>
#include <vector>
#include <boost/scoped_ptr.hpp>
#include "UnitEncranger.h"
#include "UnitFormMain.h"
#include "UnitFormAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonEncryptClick(TObject *Sender)
{
  const int key = EditKey->Text.ToInt();
  Encranger e(key);

  EditCodedText->Lines->Clear();
  const int n_lines = EditPlainText->Lines->Count;
  for (int i=0; i!=n_lines; ++i)
  {
    const std::string s = EditPlainText->Lines->operator[](i).c_str();
    const std::string s_encrypted = e.Encrypt(s);
    EditCodedText->Lines->Add(s_encrypted.c_str());
  }
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonDeencryptClick(TObject *Sender)
{
  const int key = EditKey->Text.ToInt();
  Encranger e(key);

  EditPlainText->Lines->Clear();
  const int n_lines = EditCodedText->Lines->Count;
  for (int i=0; i!=n_lines; ++i)
  {
    const std::string s = EditCodedText->Lines->operator[](i).c_str();
    const std::string s_deencrypted = e.Deencrypt(s);
    EditPlainText->Lines->Add(s_deencrypted.c_str());
  }

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::ButtonAboutClick(TObject *Sender)
{
  boost::scoped_ptr<TFormAbout> f(new TFormAbout(0));
  f->ShowModal();
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitFormWhatsNew.h

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormWhatsNewH
#define UnitFormWhatsNewH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TFormWhatsNew : public TForm
{
__published: // IDE-managed Components
        TRichEdit *RichEdit;
private: // User declarations
public: // User declarations
        __fastcall TFormWhatsNew(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormWhatsNew *FormWhatsNew;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitFormWhatsNew.cpp

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormWhatsNew.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormWhatsNew *FormWhatsNew;
//---------------------------------------------------------------------------
__fastcall TFormWhatsNew::TFormWhatsNew(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitLoopReader.h

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitLoopReaderH
#define UnitLoopReaderH
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl/CppLoopReader.htm
//Reads a container in a loop:
//  when the end is reached, it starts
//  reading from the beginning again
template <class Iterator>
struct LoopReader
{
  LoopReader(const Iterator begin, const Iterator end)
    : begin_(begin), read_(begin), end_(end)
  {

  }
  const Iterator Read() const { return read_; }

  void Next()
  {
    ++read_;
    if (read_ == end_) read_ = begin_;
  }

  private:
  const Iterator begin_;
  const Iterator end_;
  Iterator read_;

};
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

./ToolMultiEncrangerVcl/UnitLoopReader.cpp

 

//---------------------------------------------------------------------------
/*
  MultiEncranger, encryption and de-encryption tool
  Copyright (C) 2010  Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#pragma hdrstop

#include "UnitLoopReader.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml