kl

closed account (E0p9LyTq)
I need help starting this


Write some code you believe would solve this homework problem. Post the source here using code tags, along with any errors you might get from your compiler or problems you have with possible incorrect output.

We will help you when you put some effort, we won't do your work for you.
Oh please, who the hell should read this code. Try to indent it and use code-tags.
closed account (E0p9LyTq)
@dark nephthys,

Read this: How to Use Code tags, http://www.cplusplus.com/articles/jEywvCM9/

And what is your problem? You've posted your source without telling us what is not working correctly.
Last edited on
That code isn't close to solving the problem set.

To run the program the user should type otp -e <unencrypted_file>
<output_file> <secret_file>, ...
You haven't bothered to implement this.

For each character p in the unencrypted file, ...
This doesn't mean allocate an array of p (that you don't delete).

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
I can't see where you implement this mapping.

This list goes on, but you have to respond first for anyone to help.
Do you still need help with this?
closed account (48T7M4Gy)
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<vector>
#include<stdlib.h>
using namespace std;
void to_upper_case(vector<char>& text, int len)
{
	for (int i = 0; i < len; i++)
	{
		if (text[i] >= 97 && text[i] <= 122)
			text[i] -= 32;
	}
}
void print_string(vector<char> text, int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << (char)(text[i] + 65);
	}
	cout << endl;
	return;
}
size_t get_input(vector<char>& msg)
{
	char a;
	while (1)
	{
		a = getchar();
		if (a == '\n')
			break;
		msg.push_back(a);
	}
	return msg.size();
}
int main()
{
	vector<char> msg;
	vector<char> enc_msg;
	vector<char> dec_msg;
	int *p;
	int i;
	size_t len;
	cout << "Enter Message to Encrypt:";
	len = get_input(msg);
	to_upper_case(msg, len);
	p = (int*)malloc(msg.size() * sizeof(int));
	for (i = 0; i < len; i++)
	{
		p[i] = rand() % 26;
		if (msg[i] >= 65 && msg[i] <= 90)
			enc_msg.push_back((char)((msg[i] - 65 + p[i]) % 26));
		else if (msg[i] >= 97 && msg[i] <= 122)
			enc_msg.push_back((char)((msg[i] - 97 + p[i]) % 26));
		else
			enc_msg.push_back((char)msg[i]);
	}
	cout << "\nEncoded Message:";
	print_string(enc_msg, len);
	cout << "\nKey for decryption:\n";
	for (i = 0; i < len; i++)
	{
		cout << (char)(p[i] + 65);
	}
	cout << endl;
	cout << "\nDecrypted Message:";
	for (i = 0; i < len; i++)
	{
		if ((enc_msg[i] - p[i]) < 0)
			dec_msg.push_back((char)(enc_msg[i] - p[i] + 26));
		else if ((enc_msg[i] - p[i]) >= 0)
			dec_msg.push_back((char)(enc_msg[i] - p[i]));
		else
			dec_msg.push_back((char)enc_msg[i]);
	}
	print_string(dec_msg, len);
	return 0;
}

Ah that's where we like to start.
Last edited on
closed account (48T7M4Gy)
I suppose cut and paste is a start. :-]

http://www.sanfoundry.com/category/c-algorithms-set-string/page/2/
It looks like you had a choice of projects, why choose one that you don't understand the question?

first thing to do there is to turn that big chunk of text into a list of things to do!
thats where i would start!
closed account (48T7M4Gy)
dark nepthys
"Your" code has been cut and paste from the sanfoundry site I quoted above.

If it is genuinely your work then please prove it. If it is not then please note this is not a homework site and not a vehicle for your plagiarism and cheating.

[BTW If it is your code I don't think too many people would believe you can't make a start.]
Last edited on
Let's implement one part at a time:
(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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const char chmap[] = 
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    " ";

char encrypt(char c, int r)
{
    // find the position of the char in the char map
    char *p = std::find(chmap, chmap + sizeof(chmap), p);
    if (p == chmap + sizeof(chmap))
        throw std::domain_error("Input character out of range");

    // find the index of the char
    ptrdiff_t idx = p - chmap;

    // add on the random offset
    idx += r;
    idx = idx % sizeof(chmap);

    return chmap + idx;
}


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.
This means you need to save the random numbers in a container somewhere so you can save them when you're done.

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.
This means you need to handle the command line args. We need to have exactly these args:
0: program name
1: -e (or -d to be added later)
2: plain text file name
3: cipher text file name
4: random numbers file used to generate cipher text
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
int main(int argc, char* argv[])
{
    if (argc != 5)
        throw std::domain_error("Program expects 4 parameters");

    if (strcmp(argv[1], "-e") == 0)
    {
        std::ifstream plaintext(argv[2]);
        std::ofstream ciphertext(argv[3]);
        std::ofstream secretfile(argv[4]);

        std::string line;
        while (std::getline(plaintext, line))
        {
            for (size_t i = 0, mx = line.size(); i != mx; ++i)
            {
                int r = 1 + rand() % sizeof(chmap);
                char c = encrypt(line[i], r);
                ciphertext << c;
                secretfile << r << '\n';
            }
            ciphertext << '\n';
        }
    }
}

And of course, you'll need to prepend:
1
2
3
4
5
#include <stdexcept>
#include <fstream>
#include <string>
#include <algorithm>
#include <stddef.h> 

Do you understand the breakdown and code?

BTW, I just wrote this in the browser, so expect to fix the odd error here or there.
Last edited on
very helpful Kbw,interested to see how you would decrypt
The op has removed the post. I can imagine what the command line would be for decrypt and of course the decrypt is just the opposite of the encrypt.

If you want help, ask for it. Don't try to bait someone who spending their own valuable time to help for free.

This is the original text:
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 encrypt characters and blank spaces following exactly this mapping:
‘a’ ‘b’ … ‘z’ ‘A’ ‘B’ … ‘Z’ [blank space]
1 2…26 27…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.
Last edited on
closed account (48T7M4Gy)
acmilan
FWIW, The OP and I cut and pasted exactly the same from the sanfoundry site I referred to above.
OK. so how exactly would you change encrypt to make it decrypt?
why is every comment at start reported?
@jasonwynn10
Because we didn't do his homework for him :)
i also need help with the same program
Awfully suspicious remone...
Topic archived. No new replies allowed.