Total members 11894 |It is currently Thu Nov 21, 2024 1:00 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Encrypts/Decrypts (Encryption- Decryption using Vigenere ) a piece of text using vigenere algorithm.
cpp code
//Encrypt.cpp
#include <iostream>

#include "Message.h"
#include "Table.h"
using namespace std;

#define MAXLENGTH 256

void getMessage(char& amessage, char& akey);

int main(int argc, char* argv[])
{
Message myMessage;

if(argc != 2)
{
cout << "usage: " << argv[0] << ".exe -d or -e" << endl;
return 0;
}

if( !strcmp(argv[1], "-d"))
{
myMessage.getMessage();
myMessage.getKey();
myMessage.decryptMessage();
}

if( !strcmp(argv[1], "-e"))
{
myMessage.getMessage();
myMessage.getKey();
myMessage.encryptMessage();
}

return 0;
}


Message.cpp
cpp code
//Message.cpp
#include <iostream>
#include <string>
#include <stdio.h>

#include "Message.h"
#include "table.h"

using namespace std;

//Default constructor
Message::Message()
{}

//Constructor
Message::Message(const char* pStr, const char* key)
{
int i, j;

pUnMessage = new char[ strlen(pStr) + 1 ];
strcpy(pUnMessage, pStr);

pEnMessage = new char[ strlen(pStr) + 1 ];
strcpy(pEnMessage, pStr);

pKey = new char[ strlen(pStr) + 1 ];
strcpy(pKey, pStr);

for(i = 0, j = 0; i < strlen(pStr), j < strlen(pStr); i++, j++)
{
if(key[j] && pKey[i])
{
pKey[i] = key[j];
}
else if(key[j] == '\0')
{
j = -1;
i--;
}
else
break;
}
}

//Constructor for only the message, not the key
Message::Message(const char* pStr)
{
pUnMessage = new char[ strlen(pStr) + 1 ];
strcpy(pUnMessage, pStr);

pEnMessage = new char[ strlen(pStr) + 1 ];
strcpy(pEnMessage, pStr);
}

//Encrypts message and returns encrypted message
void Message::encryptMessage()
{
int across, down;
int i, j;


cout << "Encrypting message..." << endl;
cout << endl << pKey << endl << pUnMessage << endl << endl;
for(j = 0; j < strlen(pEnMessage); j++)
{
for(i = 0; i < 26; i++)
{
if( !(isalpha(pUnMessage[j])))
{
across = 30;
break;
}

if( ( toupper(pUnMessage[j]) == table[0][i]))
{
across = i;
break;
}
}

for(i = 0; i < 26; i++)
{
if( !(isalpha(pUnMessage[j])))
{
down = 30;
break;
}

if( ( toupper(pKey[j]) == table[i][0]))
{
down = i;
break;
}
}

if(across != 30 && down != 30)
pEnMessage[j] = table[down][across];
}
cout << pEnMessage << endl << endl;
}

void Message::decryptMessage()
{
int down, across, i, j;

cout << "Decrypting message..." << endl;
cout << endl << pKey << endl << pEnMessage << endl << endl;

for(j = 0; j < strlen(pEnMessage); j++)
{
for(i = 0; i < 26; i++)
{
if( !(isalpha(pKey[j])))
{
across = 30;
break;
}

if( ( toupper(pKey[j]) == table[0][i]))
{
across = i;
break;
}
}

for(i = 0; i < 26; i++)
{
if( !(isalpha(pEnMessage[j])))
{
down = 30;
break;
}

if( ( toupper(pEnMessage[j]) == table[i][across]))
{
down = i;
break;
}
}

if(down != 30)
pUnMessage[j] = table[down][0];
}

cout << pUnMessage << endl;
}

//Returns unencrypted message
const char* Message::getUnMessage() const
{
if(pUnMessage)
return pUnMessage;
else
return "null pointer (should never happen, default constructor is private";
}

//Returns encrypted message
const char* Message::getEnMessage() const
{
if( strcmp(pEnMessage, pUnMessage))
return pEnMessage;
else
return "Message not yet encrypted";
}


//Gets message to encrypt
void Message::getMessage()
{
char* pTemp = new char[256];

cout << "Enter your message (less than 256 characters, end with \"ENTER\": " << endl;
fflush(stdin);
cin.getline(pTemp, 256, '\n');
cout << endl;

pUnMessage = new char[ strlen(pTemp) + 1 ];
strcpy(pUnMessage, pTemp);

pEnMessage = new char[ strlen(pTemp) + 1 ];
strcpy(pEnMessage, pTemp);

delete[] pTemp;
}

void Message::getKey()
{
if(!pUnMessage)
{
cout << "You have to enter the message before you enter the key." << endl;
return;
}

char* pTemp = new char[32];

cout << "Enter your key (less than 32 characters, one word) that ends with \"ENTER\": " << endl;
fflush(stdin);
cin.getline(pTemp, 32, '\n');
cout << endl;

pKey = new char[ strlen(pUnMessage) + 1 ];
strcpy(pKey, pUnMessage);

int i, j;

for(i = 0, j = 0; i < strlen(pUnMessage), j < strlen(pUnMessage); i++, j++)
{
if(pTemp[j] && pKey[i])
{
pKey[i] = pTemp[j];
}
else if(pTemp[j] == '\0')
{
j = -1;
i--;
}
else
break;
}

delete[] pTemp;
}


//Destructor
Message::~Message()
{
delete[] pUnMessage;
delete[] pEnMessage;
delete[] pKey;
}





Attachments:
File comment: the source code.
encrypt.rar [52.24 KiB]
Downloaded 1904 times

_________________
Please recommend my post if you found it helpful
Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Encrypts using vigenere algorithm
 id3 algorithm     -  
 Rijndael Algorithm     -  
 CPU priority algorithm...     -  
 Dijkstra Algorithm     -  
 Generic Algorithm     -  
 genetic algorithm example     -  
 Data set for ID3 algorithm     -  
 Prime Generator Algorithm     -  
 expectation-maximization (EM) algorithm     -  
 Mid-point Algorithm using j2me     -  



Topic Tags

C++ Algorithms






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com