Need help with code reduction please. New and don't know where to start??

Square.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef SQUARE_H
#define SQUARE_H

static char VigSquare[28][27] =
	{
		{'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',' ',},
		{'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','_',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'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',},
		{'_','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',},
	};

#endif 




Messaged.h

1
2
3
4
5
6
7
8
#ifndef MSG_H
#define MSG_H

char Message[];

void TakeMessage();

#endif 



Keyword.h

1
2
3
4
5
6
7
8
9
10
#ifndef KEYWORD_H
#define KEYWORD_H

char Keyword[];

int TakeKeyword();

extern int KeyLength;

#endif 





EncodeDecode.h

1
2
3
4
5
6
7
#ifndef EnD_H
#define EnD_H

int Encode();
int	Decode();

#endif 





VigFinder.h

1
2
3
4
5
6
7
8
#ifndef SEARCH_H
#define SEARCH_H

char VigSearchR(int Row, char Letter);

char VigSearchC(int Col, char Letter);

#endif 



Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include "EncodeDecode.h"
#include "Keyword.h"
#include "Message.h"

using namespace std;

int main()
{
Reset:
	char nChoice;
	cout << "\n\t\t\tPlease pick an option\n\t\t       a.Encode     b.Decode : ";
	cin >> nChoice;
	if(nChoice == 'a')
		{
			Encode();
		}
		else if(nChoice == 'b')
		{
			Decode();
		}
		else
		{
			cout << "\n\n\t\tERROR : INVALID CHOICE\n";
			goto Reset;
	}
	cout << endl << endl << Keyword << endl << endl;
	
	system("PAUSE");
	return 0;
}




Encode.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include "Keyword.h"
#include "Square.h"
#include "Message.h"
#include "VigFinder.h"

using namespace std;

int Encode()
{
	TakeKeyword();
	
Retake:
	cout << "\n\tPlease enter your message up to 4000 charcters:\n\n";
	TakeMessage();
	int MsgLength = 0;
	
	while (Message[MsgLength] != NULL)
	{
		++MsgLength;
	}
	
	int VigChk = 0;
	int MsgPos = 0;

	do
	{
		if ((Message[MsgPos] != VigSquare[0][VigChk]) && VigChk <= 25)
		{
			++VigChk;
		}
		if (Message[MsgPos] == VigSquare[0][VigChk])
		{
			++MsgPos;
			VigChk = 0;
		}
		if (VigChk > 25 || MsgPos == 3999)
		{
			cout << "\n\t\t\t  ERROR: BAD MESSAGE\n\t\t\t(lower case only)\n\t\t\t(no punctuation)\n\t\t\t(hyphon instead of space)\n";
			goto Retake;
		}
	}while(MsgPos != (MsgLength));
	
	int MsgEcd=0;
	int KeyPos=0;
	int Col;
	int Row;

	do
	{
		Col = VigSearchR(0,Message[MsgEcd]);
		Row = VigSearchC(0,Keyword[KeyPos]);
		cout << VigSquare[Row][Col];
		++MsgEcd;
		++KeyPos;
		if (KeyPos > (KeyLength-1))
		{
			KeyPos = 0;
		}

	}while(Message[MsgEcd] != NULL);

	cout << endl << endl << MsgLength;

	return 0;
	
}





Decode.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include "Keyword.h"
#include "Square.h"
#include "VigFinder.h"
#include "Message.h"

using namespace std;

int Decode()
{
	TakeKeyword();
	
Retake:
	cout << "\n\tPlease enter your message up to 4000 charcters:\n\n";
	TakeMessage();
	int MsgLength = 0;
	
	while (Message[MsgLength] != NULL)
	{
		++MsgLength;
	}
	
	int VigChk = 0;
	int MsgPos = 0;

	do
	{
		if ((Message[MsgPos] != VigSquare[1][VigChk]) && VigChk <= 25)
		{
			++VigChk;
		}
		if (Message[MsgPos] == VigSquare[1][VigChk])
		{
			++MsgPos;
			VigChk = 0;
		}
		if (VigChk > 25 || MsgPos == 3999)
		{
			cout << "\n\t\tERROR : BAD MESSAGE\n\t\t\t(upper case only)\n\t\t\t(no punctuation)\n\t\t\t(underscore instead of space)\n";
			goto Retake;
		}
	}while(MsgPos != (MsgLength));
	
	int MsgEcd=0;
	int KeyPos=0;
	int Col;
	int Row;

	do
	{
		Row = VigSearchC(0,Keyword[KeyPos]);
		Col = VigSearchR(Row,Message[MsgEcd]);
		cout << VigSquare[0][Col];
		++MsgEcd;
		++KeyPos;
		if (KeyPos > (KeyLength-1))
		{
			KeyPos = 0;
		}

	}while(Message[MsgEcd] != NULL);

	cout << endl << endl << MsgLength;
	
	return 0;
	
}





Keyword.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include "Keyword.h"
#include "Square.h"


using namespace std;

char Keyword[30]={0};
int KeyLength = 0;



	

int TakeKeyword()
{
Retake:

	if (Keyword[0] != NULL)
	{
		KeyLength = 0;
	}
	
	cout << "\n\t\tPlease enter you keyword in capitals: ";
	cin.ignore();
	cin.get (Keyword, 30);
	
	while (Keyword[KeyLength] != NULL)
	{
		++KeyLength;
	}

	int VigComp=1;
	
	int KeyPos=KeyLength;
	do
	{
		if ((Keyword[KeyPos] != VigSquare[VigComp][0]) && (VigComp <= 27))
		{
			++VigComp;
		}
		
		if (Keyword[KeyPos] == VigSquare[VigComp][0])
		{
			VigComp=1;
			--KeyPos;
		}

		if (VigComp > 27)
		{
			cout << "\n\t\t\t  ERROR: BAD KEYWORD\n\t\t\t  (upper case only)\n\t\t    (may only contain underscores)\n\t\t\t   (no punctuation)\n\n";
			goto Retake;
		}

	}while (KeyPos >= 0);
	return 0;
}




TakeMessage.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include "Message.h"

using namespace std;

char Message [4000];
void TakeMessage()
{
Retry:
	cin.ignore();
	cin.get (Message, 4000);
	if (Message[0] == NULL)
	{
		cout << "\n\t\tERROR : NO ENTRY\n";
		cout << "\n\tPlease enter at least 1 chracter:\n\n";
		goto Retry;
	}
	else
	{
		return;
	}
}






VigFinder.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include "Square.h"


using namespace std;

char VigSearchR(int Row,char Letter)
{
	int Col = 0;
	do
	{
		if (VigSquare[Row][Col] != Letter)
		{
			++Col;
		}
	}while(VigSquare[Row][Col] != Letter);
	return Col;
}

char VigSearchC(int Col,char Letter)
{
	int Row = 0;
	do
	{
		if(VigSquare[Row][Col] != Letter)
		{
			++Row;
		}
	}while (VigSquare[Row][Col] != Letter);
	return Row;
}
Last edited on
I am a beginner interested in cryptography and I wrote this automated version of a vignere cypher I am sure that there is plenty of code saving that can be done just do not know as a beginner where to start. The whole thing runs and works fine with only one non-terminal compiler error that I cannot figure out.

Any help with trimming the code and the error quuoted below would be much appreciated.

LINK : C:\Users\Cobby\Documents\Visual Studio 2010\Projects\Vignere\Debug\Vignere.exe not found or not built by the last incremental link; performing full link

Thanks.
Cobby
any feedback would be awesome

Thanks Cobby
Help please, my tutor said i had to much code and could simplify greatly; just do not know where to start??
Someone, anyone, please??
I understand most of the program but have not learnt this statements yet;

1
2
3
4
5
6
7
8
9
10
11
 
Vigenere(string key)
  {
    for(int i = 0; i < key.size(); ++i)
    {
      if(key[i] >= 'A' && key[i] <= 'Z')
        this->key += key[i];
      else if(key[i] >= 'a' && key[i] <= 'z')
        this->key += key[i] + 'A' - 'a';
    }
  }


Also I can understand that % 26 is modular maths to find the line position but I cannot understand how the maths represents a Vignere Square highlighted bold in the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  string encrypt(string text)
  {
    string out;
 
    for(int i = 0, j = 0; i < text.length(); ++i)
    {
      char c = text[i];
 
      if(c >= 'a' && c <= 'z')
        c += 'A' - 'a';
      else if(c < 'A' || c > 'Z')
        continue;
 
      out += (c + key[j] - 2*'A') % 26 + 'A'; 
      j = (j + 1) % key.length();
    } 


One more annoying question finally. Although I do not understand the math above I do realise how it relates to ASCII code; part of my assignment is to include ' ' or '_'
(my choice) as a charachter added to the end of the square but in ASCII the ' ' and '_' are seperated from the regular alphabet; I cannot think of a simple addition to the maths in the RosettaCode Vignere encrypter/decrypter to represent ' ' or '_'??

Sorry for being a complete noob only started to learn a week ago
Cobby
Last edited on
> I understand most of the program but have not learnt this statements yet ...

Perhaps you are unfamiliar with std::string.
See http://www.cprogramming.com/tutorial/string.html


> Also I can understand that % 26 is modular maths to find the line position but I cannot understand how the maths represents a Vignere Square highlighted bold in the code below.

This might help: http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher#Algebraic_description


> Although I do not understand the math above I do realise how it relates to ASCII code; part of my assignment is to include ' ' or '_'

It is always a bad idea to assume that characters are encoded the same way on all implementations. What we require is that the characters in our alphabet are mapped to consecutive numbers starting at zero: 0, 1, 2, .... etc.

For example if the characters in a message to be encrypted is restricted to the alphabet:
{ A, B, C, D, E, F, G, H, _, #, % }, we cam map A => 0, B => 1 , ... by:
1
2
3
4
5
6
7
8
#include <string>

const std::string alphabet = "ABCDEFGH_#%" ;
const std::size_t N_LETTERS = alphabet.size() ;

// given char 'c', return its position in the alphabet
// this does a linear search; can be made faster by using a lookup table
inline std::size_t look_up( char c ) { return alphabet.find(c) ; }


And now, given a key CF_#:
1
2
const std::string key = "CF_#" ;
const std::size_t KEY_SIZE = key.size() ;


With plain_text being the message to be encrypted giving us cipher_text, the algebra reduces to:

1
2
3
4
5
// encyipt
ciphetr_text[i] = alphabet[ ( look_up( key[i%KEY_SIZE] ) + look_up( plain_text[i] ) ) % N_LETTERS ] ;

// decrypt
plain_text[i] = alphabet[ ( look_up( ciphetr_text[i] ) + look_up( key[i%KEY_SIZE] ) ) % N_LETTERS ] ;


Note: error checks for invalid characters have been elided.
closed account (N36fSL3A)
I'm sure that JLBorges helped you, I'm too tired to read it right now, I have just come home from a busy day. But next time when continuing on to a post, use the 'Edit' tag at the bottom right of the message.
Well, this... Only one thing, as I'm not very good:
You can make char arrays like this:
char array[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
Topic archived. No new replies allowed.