coding ciphers

Pages: 12
This is probably going to be a stupid question,but im going to ask anyway.For a homework,im supposed to write a program to decrypt ciphers. One such cipher is an affine cipher. Is there someplace where I can fine code for this so I dont have to reinvent the damn wheel or do I have to write this all from scratch?
I'm sure you could find it, but then this is your homework. You're kind of meant to reinvent the wheel. After all, the algorithm is going to be about two lines long. How lazy can one be?
It's not a question of laziness. It's has to do with the fact that I have never done anything like this before and I don't know where I would start.
well, what kind of cipher do you want to implement? there are different type of it.
Wikipedia often has some code that could get you started.
Ok,I actually have a program now that I need help with. Basically whats going on is that I have three long messages that need to be decoded. What my functions are supposed to do are for the entrie length of the message decrypt the outcome and print the plain text. I know my functions are the problem. All that Im asking is for help making them right.

the code is as follows:

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
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<fstream>
using namespace std;
using std::fstream;

int main()
{
	int choice;
	char message;
	cout<< "Encrypted messages found. Enter 1-3 to decrypt the message";
	cin>>choice;
	if (choice== 1)
	{
		char affineA(message);
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else if(choice==2)
	{
		char vingere(message);
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else if (choice==3)
	{
		char affineB(message);
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else
	{
		cout<<"Program terminated";
	}
	return 0;
}

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

	fstream ciphertext("f:ciphertext1.txt",ios::in);

	for (int i=0;i<=ciphertext.strlength;i++)
	{
		message1=((a*i+b)%26);
	}
	cout<<message1;
}
char vingere(int i,char keyword,char message2)
{
	char keyword="uboat";
	fstream ciphertext("f:ciphertext2.txt",ios::in);
		for (int i=0;i<=ciphertext.strlength;i++)
	{
		message2= int(ciphertext[i]) + int(keyword);
	}
		   cout << char(message2);
}


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

	fstream ciphertext("f:ciphertext3.txt",ios::in);

	for (int i=0;i<=ciphertext.strlength;i++)
	{
		message3=((a*i+b)%26);
	}
	cout<<message3;
}
Several problems:

for (int i=0;i<=ciphertext.strlength;i++)

File streams don't have a member named strlength. You do this twice.

1
2
3
4
char affineA(int a,int b, int i,char message1)
{
	int a=1;
	int b=15;


You declare affineA and affineB to accept three params, which you don't pass in the call to the function, then you redefine them as a local.

char keyword="uboat";

You're trying to assign a string to a single character. That's one byte trying to hold five.

char vingere(int i,char keyword,char message2)

You declare vingere as accepting three params, but pass it one.
Last edited on
ok, but for the for (int i=0;i<=ciphertext.strlength;i++)

im trying to have it stop when it hits the length of the text.what would I replace to make it work?
Last edited on
anyone?
The first loop would become
1
2
3
4
char c;
while(ciphertext.get(c)){
   //ciphertext[i] becomes c
}

The second loop hardly makes any sense.

File IO is not your main concern. Follow a C++ tutorial to get the basics first.
It's not that I don't know the basics,I just stink at programming. None the less, the way I believe the functions should go.....using affine as an example...

While(cipher text.get(c))
{
i++
Ciphertext[i]=the respective function
Ciphertext[i]=c
}
Cout<<c

So am I in the right ball park or still not close?

It's not that I don't know the basics,I just stink at programming.


Some tough love is needed here, I think.

Maybe computer programming isn't the path for you, because it doesn't look like you picked up on the basics either.
Last edited on
So am I in the right ball park or still not close?
That depends on whether you actually have an array called Chipertext. Any way, though, it isn't good.
Look at this code:
1
2
3
4
5
6
7
char arr[100]= {0};//fills the array with 0s
char c = 'x';//this is the same as a file that has x written 100 times in it
while(i < 100) {
   i++;
   arr[i] = some_function(c);
   arr[i] = c;
}
What does arr contain now? Is that what you needed? I'd love to hear your thoughts on the purpose of each line (of the code in your last post).
Last edited on
Maybe computer programming isn't the path for you


oddly enough I agree with you. Believe me if I didnt have to program I would avoid it with a passion,but the problem is that my last class of my degree involves programming so im SOL in that regards.

Moving on, I have my program running. but im having an issue with displaying a decrypted message. The issue being that it doesnt display at all.

the code is as follows:
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
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<fstream>
using namespace std;
using std::fstream;
char message;

int main()
{
	int choice;
	cout<< "Encrypted messages found. Enter 1-3 to decrypt the message:"<<endl;
	cin>>choice;
	if (choice== 1)
	{
		char affineA(message);
		cout<<message<<endl;
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else if(choice==2)
	{
		char vingere(message);
		cout<<message<<endl;
		cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
		cin>>choice;
	}
	else if (choice==3)
	{
		char affineB(message);
		cout<<message<<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;
}
char affineA(char message1)
{
	int a=1;
	int b=15;
	int i=0;
	char c;

	fstream ciphertext("f:ciphertext1.txt",ios::in);

	while(ciphertext.get(message1))
	{
		i++;
		message1=((a*i+b)%26);
		c=message1;	
	}	
	cout<<c;
	return c;
}
char vingere(char message2)
{
	int keyword[]={21,2,15,1,20};
	int n=0;
	char c;
	fstream ciphertext("f:ciphertext2.txt",ios::in);
   while(ciphertext.get(message2))
	{
		message2= int((message2) - keyword[n]);
		c=message2;
   }
   cout<<c;
   return c;
}


char affineB(char message3)
{
	int a=17;
	int b=3;
	int i=0;
	char c;
	fstream ciphertext("f:ciphertext3.txt",ios::in);

	while (ciphertext.get(message3))
	{
		i++;
		message3=((a*i+b)%26);
		c=message3;
	}
	cout<<c;
	return c;

}


My ineptude with programming aside,which is why i joined this forum in the first place to get some help,id like to know what im doing wrong.
Last edited on
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<stdlib.h> //this should be <cstdlib>
#include<string.h> //this should be either <cstring> or <string>, I'm not sure which you meant
//you could definitely use <string> though
#include<fstream>
using namespace std;
using std::fstream;
char message;//char is a type that holds one character. What you need is std::string (from <string> header)

int main()
{
    int choice;
    cout<< "Encrypted messages found. Enter 1-3 to decrypt the message:"<<endl;
    cin>>choice;
    if (choice== 1)
    {
        char affineA(message);//what do you think this is? I understand you want to call a function, but this
        //line declares a variable affineA which has the same value as message.
        //To make this a function call, remove the "char".
        //But that's not all. To call a function you must first declare it. affineA is declared (defined) bellow, so
        //the compiler hasn't seen it yet. Put a declaration above main(). Declaration is the first line of your
        //function but instead of { it ends with ;.
        //But that's not all. What do you thing message contains now? You never gave it any value.
        cout<<message<<endl;
        cout<<"Message decrypted. Enter 1-3 to decrypt the remaining messages or 0 to exit"<<endl;
        cin>>choice;//you say that the user can try another message, but he really can't. To allow that you
        //have to wrap most of main() in a loop.
    }
    //other cases have the same problems, so I'll skip them
    return 0;
}

char affineA(char message1)//again, char is just 'H', not "Hello".
{
    int a=1;
    int b=15;
    int i=0;
    char c;

    fstream ciphertext("f:ciphertext1.txt",ios::in);

    while(ciphertext.get(message1))
    {
        i++;
        message1=((a*i+b)%26);//you read a char from file and now immediately overwrite it. 
        c=message1;//here you overwrite previous values of c.
        //this whole loop is equivalent to c = (a*lentgh_of_that_file+b)%26;
        //I don't really get what you're trying to do..
        //I guess what you want is cout << (a*message1+b)%26; instead of these three lines..	
    }	
    cout<<c;
    return c;
}
char vingere(char message2)
{
    int keyword[]={21,2,15,1,20};
    int n=0;
    char c;
    fstream ciphertext("f:ciphertext2.txt",ios::in);
    while(ciphertext.get(message2))
    {
        message2= int((message2) - keyword[n]);//n is always 0
        c=message2;//again, you overwrite old values of c
   }
   cout<<c;
   return c;
}
//I don't really get what you're trying to do..


essentially,Im trying to write functions that decrypt a message and turn it into plain text.
I understood as much, but how come your de/encryption method ignores the message itself? You need to learn to trace the program. That is, take a line, consider what values do your variables hold before it is executed an after. Then consider what line is going to be executed next and repeat the process.
ok i changed the functions with strings. Im still having trouble displaying them when the program is running.i know it has something to do with getting the strings to match with the output from the text files. If you guys can point me in the right direction i would appreicate it.
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
#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
using namespace std;
using std::string;
string message1;
string message2;
string message3;
string affineA(string message1);
string vingere(string message2);
string affineB(string message3);
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<<message1<<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<<message2<<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<<message3<<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;
	ofstream ciphertext("f:ciphertext1.txt",ios::out);

	for(i=0;i<=message1.length();i++)
	{

		ciphertext<<((a*i+b)%26);
		cout<<message1;
	}	
	
	return message1;
}
string vingere(string message2)
{
	int keyword[]={21,2,15,1,20};
	int n;
	int i;

	ofstream ciphertext("f:ciphertext2.txt",ios::out);
   for(i=0;i<=message2.length();i++)
	{
		n++;
		ciphertext<<int((message2[i]) - keyword[n]);
   }
   return message2;
}


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

	ofstream ciphertext("f:ciphertext3.txt",ios::out);
	for(i=0;i<=message3.length();i++)
	{
		
		ciphertext<<((a*i+b)%26);
		cout<<message3;
	}	
	
	return message3;
}
Last edited on
You still don't have anything to encrypt or decrypt. You're not taking any input.
Pages: 12