Text Padding

Pages: 12
lol, you up to the challenge? ill start testing it now... i was being lazy before and haven't tested it yet.
/facepunch ... i can't believe it. i didn't try to compile it, now that i did. it doesn't work. hmm, so ive gotta get it into a char array... ugh, it sucks cause i never use arrays therefore don't know to put something into them. lol
Iterate over an array of type char, using getchar to place values into an array thus:
1
2
3
4
5
6
7
8
9
10
11
12
    char* myCharArray = new char[256];
    int* myIntArray = new int[256];

    for (int i = 0; i < 256; ++i) {
        
myCharArray[i] = getchar();
        
myIntArray[i] = int(myCharArray[i];
    }
    delete myCharArray; 
    // Play with the ints and write to file
delete myIntArray;

Or use fgets(myCharArray[i], 1, myFile)
to get it from a file.

Remember that as you're iterating you only want one char at a time. Hence getchar and fgets. With fgets if you use stdin as the file you can get terminal input.
Last edited on
hmm, thanks. that must've been hard to type on a phone. or atleast tedious. so i should put this in a function then... well i think deleting the character array is a bad idea, but basically that middle part the for statement is the check, right? so its gonna keep repeating converting a to 1, b to 2, c to 3, etc for all of the places? Then its gonna delete the character array, and i use the int array for encryption? but how to a turn the int array into like... well say it contains 01, 02, 03. how do i convert that over to 010203? or how do i use it in a formula in that position? and what's the code to undo this?
For why I used new and delete:
You don't know how much memory you need. The file size is arbitrary. I think
int x = sizeof(myFile);
char* y = new char[y];
will work. Then you need to free the memory you used to prevent a memory leak.

A will be 97, b 98, etc. x = y + 97 where y is the position of x in the alphabet given that the position of a is 0. Numbers are + 48.

Google 'ASCII table' for more.

To build a string, add
myStr += myIntArray[i];
to the for loop. Then you can delete both arrays, write the string and destroy the string object by executing.

Just delete the arrays as soon as you're done with them to prevent a memory leak.

On tediousness:
Nah, its fun.
int x = sizeof(myFile);
What?

char* y = new char[y]; What?
okay so it should look something like this :

char* myCharArray = new char[256];
int* myIntArray = new int[256];
int x = sizeof(myFile);
char* y = new char[y];
for (int i = 0; i < 256; ++i) {

myCharArray[i] = getchar();

myIntArray[i] = int(myCharArray[i];
myStr += myIntArray[i];
}
delete myCharArray;
// Play with the ints and write to file
delete myIntArray;

...? so that'll build the int array? the cool part is this is kinda like a .txt editor but encrypted, so all of the values will be ASCII and thus everything will be totally encrypted. So basically : i need to add that in, then convert the string to an int or a long or something, then i can use that to encrypt/decrypt? and does this piece of code work for both padding and unpadding or is it for just one of them?


[EDIT] helios, maybe you can make it a bit more clear?
Last edited on
Yeah my mistake, I wasn't sure how to get the size of a file. And I meant
int y = new int[x]
Typo is maek me sad :(

I was sure there was something to do with sizeof and files. But actually that may have been something in python...

Elven, you can reverse it by delimiting each number - If something like ! is found you skip it and read the characters around it.
Last edited on
Firstly, you're talking about 'padding'

http://en.wikipedia.org/wiki/Padding_%28cryptography%29


but what you probably want to do is (since you arent doing anything)

http://en.wikipedia.org/wiki/Substitution_cipher


Secondly: altho I am happy to see someone that is learning by testing, i'd recomend you to read the cpp tutorial on this site.

Thirdly:
and this goes to helios: it was prolly a typo, he ment char* y = new char[x];
Anyway thats not the way to do it...


Let me help you out a bit, here is a psevdocode to what you want to do:

function encrypt:
open file
read a block of data from it (lets say 1024chars) and store it into an tmp var
for every char in the block:
char XOR value
write it back or add to an array of blocks (create a vector<char[1024]*> blocks)

function decrypt:
same as encrypt :)




You can later save back all blocks in the vector with your save function, but first have a look at

http://www.cplusplus.com/reference/iostream/fstream/

Notice that I've changed the algorithm to an simpler, xor algorithm, which is better- just use long strings to xor with, or even files.
About the 'value', you can read a string as a password than xor the file with that string (you xor a char at a time, you will have to calc which position in the string youre using)

You can always just set it to a hardcoded value, but a single char is eazily breakable with brute force .


Hope I helped.

EDIT: Had this site opened for a bit so I ditnt see chris posting:) btw chris, you're a long way from britain! Since you're posting here I imagine you're bored, go swimming or something
Last edited on
Lol. I was swimming earlier. Yesterday I went into a walled city.
It's awesome here. The people have nice 'personalities' :P

And it's not that far, <= 3 hours on a jet, which I slept for virtually all of. Had to get up at 5am :o

Gregor, where are you from?
telling you this the second time; I'm slovenian, eg from Slovenia, that is about 500km from the farest sea acessible point in Croatia :)
hmm... perhaps this was in the wrong section. im not that much of a beginner. i know how to use fstream. i read through the entire tutorial already. and yeah, sorry. the padding vs substitution thing was my bad. i blame wikipedia.org : http://en.wikipedia.org/wiki/RSA
mentions a thing on padding, and i didnt read through that article again, and its been awhile sense ive done anything cryptography related, so i saw padding and thought that was the term. lol anyway, what you said made no sense to me. im used to just code. not an explanation of what the code would do, go find the code myself. :P
Eastern Europe owns. I wanted to go to Russia or Serbia, or Ukraine, but we went to Croatia instead. Can't say I'm dissapointed though.
Topic archived. No new replies allowed.
Pages: 12