Text Padding

Pages: 12
I'm totally stumped on this one, no idea how to get it to work.
I need to get a large string of data to become a number to be worked with for encryption, then another piece of code to be able to unpad it later. This is going to be used for a program to encrypt and decrypt text files for the sake of a simple file extension i decided to make with a C++ program for reading/writing it. The extension is XED which is : X-Encrypted Data. X being Terminal and things of that sort. Here's what I've got :

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

string data, datae;

int save ();
int load ();
int edit ();
int decrypt (); 
int encrypt ();

int main () {
        // Erm... Im a retard xD all of my >> and << operators are backwards. 
        // Remember kiddies, dont code at 5 am, everything'll be messed up.
	// This asks what you want to do, pretty basic
	string wtd;
	cout >> "Welcome To XED Reader\n";
	cout >> "What Would You like To Do? (new, save, or load)\n";
	:quick // yada yada yada, goto is bad, whatever. it's quick and easy.
	cout >> "> ";
	cin << wtd; // this takes input
	if (wtd == "new") {
		edit ();
	} else if (wtd == "save") {
		save ();
	} else if (wtd == "load") {
		load ();
	} else {
		cout >> "Invalid Action\n";
		goto quick;
	}
}

	// Dummy Main to skip over the welcome message when we need to go back to this.
int dummy () {
	cout >> "What Would You like To Do? (new, save, or load)\n";
	:dquick // yada yada yada, goto is bad, whatever. it's quick and easy.
	cout >> "> ";
	cin << wtd; // this takes input
	if (wtd == "new") {
		edit ();
	} else if (wtd == "save") {
		save ();
	} else if (wtd == "load") {
		load ();
	} else {
		cout >> "Invalid Action\n";
		goto dquick;
	}
}

int edit () {
	cout >> "\nNote : XED Is Written All On One Line."
	cout >> "\n> ";
	cin << data;
	dummy ();
}

int load () { 
	string fname;
	:filename
	cout >> "\nFilename > ";
	cin << fname;
	ifstream load ( "Documents/" fname ".xed" );
	if ( ! load ) {
		cout << "File Not Found\n";
		goto filename;
	} else {
		load >> datae;
		decrypt ();
	}
	dummy ();
}

int save () {
	string fname;
	:saving	
	cout >> "\nFilename > ";
	cin << fname;
	ofstream save ( "Documents/" fname ".xed" );
	if ( ! save ) {
		cout << "An unknown error has occured.\n"
		goto saving;
	} else {
		encrypt ();
		save << datae;
	}
	dummy ();
}

int encrypt () {

}

int decrypt () {

}


As you can see, the encrypt and decrypt functions are blank, this is because i still need to pad it, and im working on a nice, decently-powerful yet simple algorithm for encrypting it. Obviously not industrial grade, but i hope to get it to be pretty good. Any help with the padding is much appreciated, being as I can only figure out how to pad it if it's 1 letter. I'm fine with converting it to an array temporarily, but it'll need to be a massive array because i need the program to be able to handle large strings of data. NOTE : Im still modifying it as we speak, so the code won't be perfect. I might add a key file besides it to be included with it as a check... The encryption itself isn't for the person to work with by the way. It's so that it's not just a normal .txt file that's written to by a C++ program with a new extension. Any other ideas for that besides padding and encryption would be appreciated to, but the encryption and padding is just simpler.
Last edited on
Hmm... I think i may have got it :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int encrypt () {
	// This Pads The Data... I Hope...
	int x;
	x << data;
	/*
		algorithm goes here to work with X.
	*/	
}

int decrypt () {
	int x;
	/*
		an algorithm goes here to work with X.
	*/
	// This Un-Pads The Data... I Hope...	
	data << x;
}



Anyone see any problems with that? It should turn all of the letters into its ASCII value, right?
(EDIT) Still need help with the padding, my previous idea didn't work. anyone? ideas?
Last edited on
Make it a char array and cast each value, like
myIntArray[i] = int(myCharArray[i]);
Maybe?
That will convert to ASCII values.
ASCII values is just simpler, ive got it to convert the string to int now with atoi(), but i need a way to get it back into the ASCII string when i unpad it. I looked all over google but couldn't find squat for that, its all like "use atoi" when you cant use atoi for that... or can you? from what i know you cant o.o

{EDIT} also, thanks for not commenting on my sloppy coding at the top, ugh... i can't believe how much i screwed that up, i fixed it all though... seriously, dont code at 5 am. :P oh, and also not commenting on my use of goto.
Last edited on
itoa is the opposite of atoi, but I'm unsure of its usage.
And I didn't read your code, I'm on my phone, in Croatia.

My bill will be huge but there you go. Blame WH Smith in Gatwick airport, they didn't have any C++ or programming books. I have to keep myself with C++, lest the sudden huge improvements to my knowlege disappear. I learned more last week then I learned in any previous week. This is my 6th month :)
heh, good. its for the better anyway. its horrible, backwards operators, for goto's i defined it as :name, not name:, and all of these other simple flaws. but ill go ahead and try itoa, hopefully itll work. if it doesn't im gonna re-post the whole code for other people too, ive just been commenting on it while waiting for responses, so might as well.
What do you mean by 'padding'? Have a random set of characters before the actual encrypted text?
erm padding is a cryptography term, sorry if that wasn't clear. It's giving a text string a numerical value to be used with encrypting/decrypting formulas, for example : a = 01, b = 02, c =03 etc. 010203 would be read as abc.
also, i tried itoa and it says it wasn't declared in this scope, what library does it need?? oh and this is like... my 3rd week or so.
updated source code coming in a few.
I just thought of hashing - but that's one-way. You'd have to develop a rainbow table to read the files.
That is definately not what you want to do, lol.
And NGEN, that's called 'salting'. I have no idea what padding means.
Last edited on
[NOT SOLVED] alright, so here is my most up-to-date source, just finished it a sec ago, there is a problem though. undeclared itoa, what's the header, and im also not sure if itoa will work, so any details on it would be great, and a way to unpad it if itoa can't is even better.
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

string data, datae, datal;

int save ();
int load ();
int edit ();
int decrypt (); 
int encrypt ();
int fnew ();

int main () {
	datal = "";
	// This asks what you want to do, pretty basic
	string wtd;
	cout << "Welcome To XED Reader\n";
	cout << "What Would You like To Do? (new, edit, save, load, view, help or exit)\n";
	quick: 	// yada yada yada, goto is bad, whatever. it's quick and easy.
	cout << "> ";
	cin >> wtd; // this takes input
	if (wtd == "new") { // And now we begin handling the input...
		fnew (); // yup, new file
	} else if (wtd == "edit") { 
		edit (); // edit
	} else if (wtd == "save") {
		save (); // save
	} else if (wtd == "load") {
		load (); // load
	} else if (wtd == "view") {
		cout << datal; // view the loaded data!
		goto quick;
	} else if (wtd == "help") { // get some help :)
		cout << "Command Help :\n";
		cout << "New -> Go To Edit A New File\n";
		cout << "Edit -> Edit The Loaded File\n";
		cout << "Save -> Save The File, This Includes Loaded & New Data\n";
		cout << "Load -> Load A File, From Any Directory\n";
		cout << "View -> View The Contents Of The Loaded File\n";
		cout << "Help -> View This Screen\n";
		cout << "Exit -> Exit The Program\n";
	} else if (wtd == "exit") { // NOOOOOOO DON'T LEAVE ME!!!!!!!!!!!!!!! D:
		return 0;
	} else { // Aww... You Were THIIIIIIS Close To Being A Real Command
		cout << "Invalid Action\n";
		goto quick;
	}
}

	// Dummy Main to skip over the welcome message when we need to go back to this.
int dummy () {
	string wtd;
	cout << "What Would You like To Do? (new, edit, save, load, view, help or exit)\n";
	quick: 	// yada yada yada, goto is bad, whatever. it's quick and easy.
	cout << "> ";
	cin >> wtd; // this takes input
	if (wtd == "new") { // See The Comments On Main :O
		fnew ();
	} else if (wtd == "edit") { 
		edit ();
	} else if (wtd == "save") {
		save ();
	} else if (wtd == "load") {
		load ();
	} else if (wtd == "view") {
		cout << datal;
		goto quick;
	} else if (wtd == "help") {
		cout << "Command Help :\n";
		cout << "New -> Go To Edit A New File\n";
		cout << "Edit -> Edit The Loaded File\n";
		cout << "Save -> Save The File, This Includes Loaded & New Data\n";
		cout << "Load -> Load A File, From Any Directory\n";
		cout << "View -> View The Contents Of The Loaded File\n";
		cout << "Help -> View This Screen\n";
		cout << "Exit -> Exit The Program\n";
	} else if (wtd == "exit") {
		return 0;
	} else {
		cout << "Invalid Action\n";
		goto quick;
	}
}

int fnew () {
	// This Function Allows You To Input Text Onto The Open Document, it outputs datal incase you loaded a file.
	cout << "\nNote : XED Is Written All On One Line.";
	cout << "\n> "; // Yup, This Is Where We Start Writing...
	cin >> data;
	dummy ();
}

int edit () {
	// This Function Allows You To Input Text Onto The Open Document, it outputs datal incase you loaded a file.
	cout << "\nNote : XED Is Written All On One Line.";
	cout << "\n> "; // Yup, This Is Where We Start Writing...
	cout << datal; // Yeah, the difference is an output... :P
	cin >> data;
	dummy ();
}

int load () { 
	string fname, dir;
	filename:
	cout << "\nDirectory (Case-Sensitive) >"; // ZOMFG, YOU CAN SELECT THE DIRECTORY
	cin >> dir;
	cout << "\nFilename > ";
	cin >> fname;
	ifstream load;
	load.open((dir + fname + ".xed").c_str(), ios::app); // Yeah, it's gotta be .xed
	if ( ! load ) {
		cout << "File Not Found\n"; // oh wow, you tried to load a nonexistant file, nice.
		goto filename;
	} else {
		load >> datae; // mhmm, yup : loading the data.
		decrypt (); // ZOMFG, WE'RE GONNA GO DECRYPT :O
	}
	dummy (); // somehow... just.... just if you somehow beat the if statement... :P
}

int save () {
	int sig;
	string fname, sign, dir;
	saving:	
	cout << "\nDirectory > "; // See comments on loading :P
	cin >> dir;
	cout << "\nFilename > ";
	cin >> fname;
	ofstream save;
	save.open((dir + fname + ".xed").c_str());
	if ( ! save ) {
		cout << "An unknown error has occured.\n"; // unknown? well, i couldn't think of something that might happen to make it fail at saving...
		goto saving; // ... a goto?!?!?! NOT AGAIN!!!!!!!!!!!!11oneone!!!1!1!11oneone1!1one!111!111!11!!!!!11!1one
	} else {
		encrypt (); // yup, encrypting it real fast.
		save << datae; // saving, assuming that it knows to come back here after encrypting... if it does't im gonna need another file for saving.
		cout << "Sign (y/n) > "; // oh man, you can sign it!
		cin >> sign; // yerr input?
		if (sign == "y") { // if its a yes
			save << "        Signature : "; // dividing between signature and normal.
			save << sig; // yup, its the signature
			// How it works : the data cannot be modified by the program without placing it after signature. the reciever will know easily if it's been modified, use this for cryptographic purposes, or in places where you'll need to toss around the data in new files every time.
		}
	}
	dummy ();
}

int encrypt () {
	// This Pads The Data... I Hope...
	int x;
	x = atoi(data.c_str()); // yup, it actually pads it... i think...?
	// This is where the algorithm would go, but its hidden for security purposes
	datae = /* variable would be here, its secret though :P */;
}

int decrypt () {
	int x;
	x = atoi(datae.c_str());
	// This is where the algorithm would go, but its hidden for security purposes
	// This Un-Pads The Data... I Hope...	
	datal = itoa(x);
}
yeah, chris i thought about hashing originally, i was like "I SHOULD USE SHA1" then i remembered i can't cause i need to read it. any idea what the header is for itoa? [EDIT] "This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers." not part of G++ :(
Last edited on
No =(
Do the usual standby. Go to your include folder and include *.h

Nah, I think its in string, not cstring mind.

GCC supports it.
Last edited on
i tried both ways, it didnt work either way. like i said, its compiler specific, most compilers dont have it. G++ didnt. so i need some other way to do it... and i explained what padding was before my giant source. hmm, i think i might go ahead and try using netbeans to compile this... nope netbeans doesn't have it either.
Last edited on
ATOI doesn't work either. /sigh is there really no hope for string > ASCII-value > string? Is there some way to quickly change text into specific numbers? if someone would write a function for it, that'd be great. the values aren't of any importance to me too, they could be 1-26 for all i care. but it needs to be able to format it like this : 010203 as abc, Capitalization is important, punctuation should stay the same. my problem with that is that im not sure how to get it to work like 010203 and work with more than 1 letter... a for statement should do it i guess. any help would be much appreciated...
Char array. You can take the input as a c string or c string array, iterate over it and add it to a string with +=
Thus my original suggestion to typecast a char array!
Well, i woulda. but then i went "you know what... REPLACE FUNCTION!" i just finished 2 over-stuffed functions that replace every instance of a with a number etc, and one that undoes it. but i just saw a problem coming with padding it. which ill have to fix. ill have to change the ones that use 4, 5, and 6 as the ends. 44, 45, 46 , 54, 55, 56, 64, 65, 66, cuz unpadding that causes some issues, you know? for example : 455445 would read as 45-5-44-5, because of the ordering. D: besides that it should be all working, but thanks for all the help.
Ok. That's going to be one hefty function and I still really recommend the char array typecasting as the easiest, simplest and most effective method, but if you can do it another way then good job, I guess.
well i already finished both of them, t'was quite repetative, but it works i think. I've still gotta do some testing, and i also replaced cin with getline, now we can use spaces!!! lol i cant believe all the trouble im going through because last night i decided 'im gonna make my own file extension!" lol
Send me a file in it, please! It will be fun to try and figure out how to unencrypt it. Bare in mind I have no computer until Saturday. I forgot my laptop. And they have a supposedly fast wired connection. It's torture looking at all the enet cables.
Pages: 12