coding ciphers

Pages: 12
ciphermagi (450) Feb 17, 2012 at 4:01pm
You still don't have anything to encrypt or decrypt. You're not taking any input


i figured as much which is why im missing the connector between the output from the text file and strings,for the lack of a better term. do i have to declare it as a variable or something else entirely?
I have no idea what you're asking. You have to provide something to be changed, whether you read it from a file or have the user input it manually. Otherwise, you won't get any results.
i get that I have to put what comes through the file to inevitably change it to a decrypted text, im not sure how to actually do it.
you can use successive:

inputstream.getline (char* s, streamsize n );

To get a line of text. It will read up to a newline or the end of file depending on which comes first.

Also:

inputstream.get(char* s, streamsize n );

Or

inputstream.get (char c);

To read character by character.

My preference always leans toward line by line for text files that are structured. If there is no structure (Blender's lightwave .obj export is one) then it would have to be character by character.
@kakashi, file IO is not your first problem. Approach the task step by step.
The first step is to write a function that encrypts a single character.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

char encrypt(char ch) {
    //your code goes here.
    //it should only take one line.
}

int main() {
   std::cout << "'x' encrypted using affine cipher becomes " << encrypt('x');
   std::cin.get();
}


Only once you have that figured out, you can start thinking about files.
Dude,the files include the encrypted text.
You're missing the point. A key concept of programming (or anything complex, I guess) is to beak a task into smaller pieces. You clearly can't wrap your head around all of it at once, so at least figure out that one line you need to make my code work.
ok, I made some overhaul to my program and did what I should have done from the get go and just printed out the long strings. Now I can see what I figured from the start that my functions suck.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
using std::string;
string message1("TKTCBDGTSPBCXCVGTHIGXRITSPAVDGXIWBHPAADLCDFJPAXINRDCIGDADGHIPCSP\n"
					 "GSXOPIXDCTKTGNVGDJEDUJHTGHBJHIWPKTIWTXGDLCJCXFJTPAVDGXIWBHJRWPVG\n"
					 "DJERPCCDIJHTDUUIWTHWTAUWPGSLPGTDGHDUILPGTEGDSJRIHPCTPKTHSGDEETGR\n"
					 "PCQJNIWTHPBTEGDSJRIPCSATPGCIWTPAVDGXIWBIWTNWPKTIDLGXITIWTXGDLCPA\n"
					 "VDGXIWBHPCSXBEATBTCIPIXDCHXUCDDCTXCIWTVGDJEXHPVDDSRGNEIDVGPEWTGI\n"
					 "WTCIWTNLXAACDIZCDLXUIWTNWPKTPHTRJGTPAVDGXIWB");
string message2 ("NIFONAICUMNISWTLUVETVXSHKNISSXWSSTBHUSLECHSNVYTSROCDSOYNISGXLNONACHVCHGNONW\n"
					 "YNDLHSFRNHNPBLRYOWGFUNOCACOSSUOUOLLIUVEFISSOEXGOSACPBEWUORMHLFTAFCMWAKBBBDV\n"
					 "CQVEKMUVILQBGNHNTIRILJGIGATWNVYUVEVIORIMCPBSBHXVIVBUVETVXSHKUORIMMJBDBPJRUT\n"
					 "FBUEGNTGOFYUWMXMIODMIPDEKUUSWXLFJEKSEWFYYSSNMZSCMMBPGEBHUVEZYSAAGUSAEWMFFVB\n"
					 "WFGIMQPILWBBJEUYFBEFVBFRTMTWNZUORIGWPBVXHJSNMZPFAGUHSNMNPGLBJBQRHMTTRHHUWEK\n"
					 "MPFAKLJJENHBBNHOOQEWVZDAKUDVUMYUCBXYOQUFVFFEWVZONXHJUMTLFGEFVMWNZUXSIZBUMAG\n"
					 "XBBTBKVOTXXUMPXQSWTXL");
string message3("JGRMQOYGHMVBJWRWQFPWHGFFDQGFPFZRKBEEBJIZQQOCIBZKLFAFGQVFZFWWE\n"
					"OGWOPFGFHWOLPHLRLOLFDMFGQWBLWBWQOLKFWBYLBLYLFSFLJGRMQBOLWJVFP\n"
					"FWQVHQWFFPQOQVFPQOCFPOGFWFJIGFQVHLHLROQVFGWJVFPFOLFHGQVQVFILE\n"
					"OGQILHQFQGIQVVOSFAFGBWQVHQWIJVWJVFPFWHGFIWIHZZRQGBABHZQOCGFHX");
string plaintext1;
string plaintext2;
string plaintext3;

string affineA(string);
string vingere(string);
string affineB(string);
int main()
{
	
	int choice;
	cout<< "Encrypted messages found. Enter 1-3 to decrypt the message:"<<endl;
	cin>>choice;
while(choice!=0)
{
	if (choice== 1)
	{
		affineA(message1);
		cout<<plaintext1<<endl;
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else if(choice==2)
	{
		vingere(message2);
		cout<<plaintext2<<endl;
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else if (choice==3)
	{
		affineB(message3);
		cout<<plaintext3<<endl;
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else
	{
		cout<<"Program terminated";
	}
}
	return 0;
}

string affineA(string message1)
{
	
	int a=1;
	int b=15;
	int i;
	char c;

	for(i=0;i<=message1.length();i++)
	{
	c=((a*i+b)%26);
	cout<<char(c);

	}
	
	return plaintext1;
}
string vingere(string message2)
{
	

	int keyword[]={21,2,15,1,20};
	int n=0;
	int i;
	char c;

	for(i=0;i<=message2.length();i++)
	{
		n++;
		c=int((message2[i]) - keyword[n]);
		cout<<c;   

	}

   return plaintext2;
}


string affineB(string message3)
{
	
	int a=17;
	int b=3;
	int i;
	char c;

	for(i=0;i<=message3.length();i++)
	{
		
		c=((a*i+b)%26);
		cout<<c;
	}	

	
	return plaintext3;
}

Anyone with a opinion on what Im doing wrong is more than welcome to assist. Truly appreciate the help.
This is just a once over on your code, I didn't really look in depth on it, but there are a few problems that just jump out at me right from the start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string affineA(string message1) // Should be a void function
{
	
	int a=1;
	int b=15;
	int i;
	char c; // Not a pointer, only holds one character

	for(i=0;i<=message1.length();i++)
	{
	c=((a*i+b)%26);
	cout<<char(c); // This line doesn't do anything. Try cout << c;
        // plaintext1 never does get changed, so you never have the up to date text in a variable
	}
	
	return plaintext1; // plaintext1 does not need to be returned - it's global (bad idea)
}


1
2
3
4
	else
	{
		cout<<"Program terminated"; // This doesn't do anything except say terminated. Program still runs.
	}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string affineA(string message1)
{
	
	int a=1;
	int b=15;
	int i;
	char c;

	for(i=0;i<=message1.length();i++)
	{
	c=((a*i+b)%26);
	cout<<char(c);

	}
	
	return plaintext1;
}


Since these three functions are very similar let me just dissect one. First, valid array indexes are from 0 to array_size-1. This is true for strings as well, so you might want to adjust your for continuation expression.

In all of these functions you return a string you never modify. Returning a string looks like the right thing to do, but you probably want it to be one that you built in the function. I don't see any reason for a global one.

You don't actually read from the message passed to the functions, either. In this one, it appears you're manipulating the value of the loop counter instead.

Name your functions after what they do.

The two affine functions (almost) look like affine encryption algorithms. I'm at a loss as to why you're trying to decrypt messages with them.
Last edited on
I'm at a loss as to why you're trying to decrypt messages with them.


Lol,that makes two fo us. So im presuming c=((a*i-b)%26); would be a better equation?
That would be closer, but it wouldn't be right. Once again, i is your loop counter. You don't need to decode your loop counter.

In the affine cipher the letters of an alphabet of size m are first mapped to the integers in the range 0..m − 1

1
2
3
4
5
6
7
8
9
10
11
#include <cctype>

inline char encode_affine(char to_encode, int a, int b)
{
	if (!std::isalpha(to_encode)) // not for encoding non-letters.
		return to_encode ;

	to_encode = toupper(to_encode) ;  // also can't preserve case.

	return 'A' + (a*(to_encode-'A'))%26 ;
}
Topic archived. No new replies allowed.
Pages: 12