cryptosystem

I need help asap,my assignment is due tomorrow.

#include<iostream>
#include<vector>
#include<stdlib.h>
#include <stdexcept>
#include <fstream>
#include <string>
#include <algorithm>
#include <stddef.h>

using namespace std;

void changeuppercase(vector<char>& word, int size)
{
for (int i = 0; i < size; i++)
{
if (word[i] >= 97 && word[i] <= 122)
word[i] -= 32;
}
}
void printtext(vector<char> word, int size)
{
for (int i = 0; i < size; i++)
{
cout << (char)(word[i] + 65);
}
cout << endl;
return;
}
size_t getinput(vector<char>& sen)
{
char b;
while (1)
{
b = getchar();
if (b == '\n')
break;
sen.push_back(b);
}
return sen.size();
}
int main()
{
vector<char> sen;
vector<char> enc_sen;
vector<char> dec_sen;
int *p;
int j;
size_t size;
cout << "Enter Message to Encrypt:";
size = getinput(sen);
changeuppercase(sen, size);
p = (int*)malloc(sen.size() * sizeof(int));
for (j = 0; j < size; j++)
{
p[j] = rand() % 26;
if (sen[j] >= 65 && sen[j] <= 90)
enc_sen.push_back((char)((sen[j] - 65 + p[j]) % 26));
else if (sen[j] >= 97 && sen[j] <= 122)
enc_sen.push_back((char)((sen[j] - 97 + p[j]) % 26));
else
enc_sen.push_back((char)sen[j]);
}
cout << "\nEncoded Message:";
printtext(enc_sen, size);
cout << "\nKey for decryption:\n";
for (j = 0; j < size; j++)
{
cout << (char)(p[j] + 65);
}
cout << endl;
cout << "\nDecrypted Message:";
for (j = 0; j < size; j++)
{
if ((enc_sen[j] - p[j]) < 0)
dec_sen.push_back((char)(enc_sen[j] - p[j] + 26));
else if ((enc_sen[j] - p[j]) >= 0)
dec_sen.push_back((char)(enc_sen[j] - p[j]));
else
dec_sen.push_back((char)enc_sen[j]);
}
printtext(dec_sen, size);
return 0;
}
Write a text‐based program that can encrypt or decrypt a text file
using the “unbreakable” One‐Time Pad cryptosystem. The program should read
a text file that contains only English characters (upper and lower case) and blank
spaces, and generate an encrypted or decrypted file following these rules:
(1) For encryption: For each character p in the unencrypted file, generate a
random number r between 1 and 53. The encrypted character C will be
computed by the formula C = p + r. To do this you will need to enccrypt
characters and blank spaces following exactly this mapping:
‘a’ ‘b’ …   ‘z’ ‘A’ ‘B’ …   ‘Z’ [blank space]
1   2 … 26 27 28 … 52          53
Example: if your random number r is equal to 7, the unencrypted
character ‘z’ will be encrypted to character C = ‘z’ + 7 =  33 = ‘H’. Note
that this formula should wrap around; for example, for r = 6 and p = ‘Y’, C
= ‘Y’ + 6 = 51 + 6 = 4 = ‘d’.  
The generated sequence of random numbers r should be stored in a
separate file which is the secret key to decrypt the encrypted file.
Also, of course, you need to generate an encrypted file containing the
encrypted characters.
To run the program the user should type otp -e <unencrypted_file>
<output_file> <secret_file>, where unencrypted_file is the name of the file
you want to encrypt, output_file is the name of the file you want to give to
the encrypted file, and secret_file is the name of the file that will contain the
generated random numbers which are the key to decrypt output_file at a
later time.
(2) For decryption: For each character C in the encrypted file, read the
corresponding number r from the secret key file generated during
encryption, and compute the unencrypted character p using the formula       
p = C – r.  
Example: if your secret key number for a particular character C is r = 7,
the unencrypted character ‘H’ will be computed using the formula p = 33
– 7 = 26 = ‘z’. Note again that this formula should wrap around; for
example, for r = 6 and C = ‘d’, p = ‘d’ ‐ 6 = 4 ‐ 6 = 51 = ‘Y’.
To run the program the user should type otp -d <encrypted_file>
<output_file> <secret_file>, where encrypted_file is the name of the file
you want to decrypt, output_file is the name of the file you want to give to
the unencrypted file, and secret_file is the name of the file that contains the
generated random numbers which are the key to decrypt the output_file. 
Please use the code tag to make the program more readable, it looks like a mess now. And try to ask about what doesn't work as intended or doesn't work at all (like compile and run-time errors) rather than just throwing it around.

For start, you haven't put the main function arguments(i.e: argc and argv), you need them in order to task the program via command prompt by passing your arguments(the files)
Last edited on
I recomend you use pastebin if you're going to be posting large quantities of code (or an entire program). It will make it easier for people to read, and make your post more concise.
I recomend you use pastebin if you're going to be posting large quantities of code (or an entire program). It will make it easier for people to read, and make your post more concise.

I wish you would stop going around recommending people use pastebin for code that easily fits into one post. Using code tags will make it more readable. Moving code to another location will mean reviewers have to do more work to see the code and respond to it, so it will be less likely to garner a response.
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
#include<iostream>
#include<vector>
#include<stdlib.h>
#include <stdexcept>
#include <fstream>
#include <string>
#include <algorithm>
#include <stddef.h> 

using namespace std;

void changeuppercase(vector<char>& word, int size)
{
	for (int i = 0; i < size; i++)
	{
		if (word[i] >= 97 && word[i] <= 122)
			word[i] -= 32;
	}
}
void printtext(vector<char> word, int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << (char)(word[i] + 65);
	}
	cout << endl;
	return;
}
size_t getinput(vector<char>& sen)
{
	char b;
	while (1)
	{
		b = getchar();
		if (b == '\n')
			break;
		sen.push_back(b);
	}
	return sen.size();
}
int main((int argc, char *argv[])
{
	vector<char> sen;
	vector<char> enc_sen;
	vector<char> dec_sen;
	int *p;
	int j;
	size_t size;
	cout << "Enter Message to Encrypt:";
	size = getinput(sen);
	changeuppercase(sen, size);
	p = (int*)malloc(sen.size() * sizeof(int));
	for (j = 0; j < size; j++)
	{
		p[j] = rand() % 26;
		if (sen[j] >= 65 && sen[j] <= 90)
			enc_sen.push_back((char)((sen[j] - 65 + p[j]) % 26));
		else if (sen[j] >= 97 && sen[j] <= 122)
			enc_sen.push_back((char)((sen[j] - 97 + p[j]) % 26));
		else
			enc_sen.push_back((char)sen[j]);
	}
	cout << "\nEncoded Message:";
	printtext(enc_sen, size);
	cout << "\nKey for decryption:\n";
	for (j = 0; j < size; j++)
	{
		cout << (char)(p[j] + 65);
	}
	cout << endl;
	cout << "\nDecrypted Message:";
	for (j = 0; j < size; j++)
	{
		if ((enc_sen[j] - p[j]) < 0)
			dec_sen.push_back((char)(enc_sen[j] - p[j] + 26));
		else if ((enc_sen[j] - p[j]) >= 0)
			dec_sen.push_back((char)(enc_sen[j] - p[j]));
		else
			dec_sen.push_back((char)enc_sen[j]);
	}
	printtext(dec_sen, size);
	return 0;
} 
I need help asap,my assignment is due tomorrow.

What, exactly, do you need help with?
Topic archived. No new replies allowed.