Re: [RESOLVED] how can i encrypt a file?
hi sorry i have another question about the cryptographic transformations: i know you said that true is encrypt and false is decrypt depending on what the mode is set to. however how does the program know from the if statement which one to do, like is CreateEncryptor(); always equal to 1 so if i say mode = true it will perform encryption? and is CreateDecryptor(); always = to 0... is it possible to reverse it so decryptor is true? i'm just trying to work out why true is encryption and false is decryption
Re: [RESOLVED] how can i encrypt a file?
You probably know already that the "en/decrypt while copying" operation always goes from the input file (first parameter) to the output file (second parameter) via the crypto transformer. The decision whether to encrypt or decrypt is taken in this piece of code:
Code:
if (bMode) // Encrypt
cryptrans = cryptalg->CreateEncryptor();
else // Decrypt
cryptrans = cryptalg->CreateDecryptor();
It creates either an encrypting or decrypting crypto transformer, depending on the value of bMode.
Quote:
Originally Posted by
katy_price
is it possible to reverse it so decryptor is true?
This is really simple: Exchange the if and the else branch. Or, which would require the least code changes, simply write !bMode instead of bMode. (Well, it would be a good idea to change the comments appropriately as well, otherwise it might get a bit confusing... ;))
Quote:
i'm just trying to work out why true is encryption and false is decryption
This is essentially just because I wrote it that way. As shown above, it's no problem to change that.
Re: [RESOLVED] how can i encrypt a file?
ok thanks i just wasn't sure whether the decrypt function had a 0 value as a default and the encyption was always 1 so would equal true. so basically i could do this - if bmode is true decrypt else encrypt. is it whatever is first in the if statment that determines if its true
Re: [RESOLVED] how can i encrypt a file?
Right. :)
And if you'd like it extra fancy, you can define an enum like this:
Code:
public enum class CryptMode {Encrypt, Decrypt};
Then you could call Crypt() like this:
Code:
bStatus = Crypt("input.dat", "output.dat", strPassword, CryptMode::Encrypt);
Of course that would require to change the last parameter of Crypt() to that enum type and to modify the relevant if statement to something like this:
Code:
if (cmMode == CryptMode::Encrypt) // Encrypt
cryptrans = cryptalg->CreateEncryptor();
else // Decrypt
cryptrans = cryptalg->CreateDecryptor();
:cool:
Re: [RESOLVED] how can i encrypt a file?
Re: [RESOLVED] how can i encrypt a file?
you know the block size 512, will it be padded if the data is less than the size of the block. or does that only happen if it is related to the crypto algorithm?
Re: [RESOLVED] how can i encrypt a file?
To be honest, I simply don't know whether, in case of an incomplete read operation, the remaining bytes of the buffer are padded, left in their previous state or simply contain garbage. And it's downright irrelevant: cryptstream->Read() returns the number of bytes that actually have been read and that I store in nBytesRead. And this is the value I use to specify the number of bytes to write in the call to fOutput->Write(), not the buffer size. This simple mechanism ensures that any excess bytes in the buffer are ignored in the write operation, so it doesn't matter what they contain.
Re: [RESOLVED] how can i encrypt a file?
Quote:
Originally Posted by
Eri523
To be honest, I simply don't know whether, in case of an incomplete read operation, the remaining bytes of the buffer are padded, left in their previous state or simply contain garbage. And it's downright irrelevant: cryptstream->Read() returns the number of bytes that actually have been read and that I store in nBytesRead. And this is the value I use to specify the number of bytes to write in the call to fOutput->Write(), not the buffer size. This simple mechanism ensures that any excess bytes in the buffer are ignored in the write operation, so it doesn't matter what they contain.
so is it safe to say 512 bytes of data is encrypted at a time whilst the block size equals the data in the input file. When the data is smaller than the block size the last convertion and copy is made to the output file?
Re: [RESOLVED] how can i encrypt a file?
Quote:
Originally Posted by
katy_price
so is it safe to say 512 bytes of data is encrypted at a time whilst the block size equals the data in the input file. When the data is smaller than the block size the last convertion and copy is made to the output file?
My code takes care of incomplete blocks at the end (or, maybe to put it simpler, doesn't need to care about that the way it's written) and the crypto transformer essentially does the same in its code we both don't know, as I've already explained in an earlier post.
So why worry about that at all? :cool:
Re: [RESOLVED] how can i encrypt a file?
Quote:
Originally Posted by
Eri523
My code tekes care of incomplete blocks at the end (or, maybe to put it simpler, doesn't need to care about that the way it's written) and the crypto transformer essentially does the same in its code we both don't know, as I've already explained in an earlier post.
So why worry about that at all? :cool:
its because i have to explain in my write up whats happening and how its happening and i don't want to get it wrong. i just needed to know the copy process so i can write it. so it reads, encrypts/ decrypts data in the input file and copies data 512 bytes at a time to the output file, when the data is less than the buffersize thats essentially the last read and copy right?
Re: [RESOLVED] how can i encrypt a file?
Quote:
Originally Posted by
katy_price
so it reads, encrypts/ decrypts data in the input file and copies data 512 bytes at a time to the output file, when the data is less than the buffersize thats essentially the last read and copy right?
Right.
Note that the read operation in the copy-and-en/decrypt loop is done on the crypto stream that is "stacked on top" of the input file stream. The crypto stream then does read operations as needed on the input file stream itself. Not sure whether that was what you meant in your sentence I quoted.
Re: [RESOLVED] how can i encrypt a file?
sorry i don't get it.
Code:
// Process (i.e. encrypt or decrypt) data while copying from input file to output file:
try {
const int nBufferSize = 512;
array<Byte> ^abBuffer = gcnew array<Byte>(nBufferSize);
int nBytesRead;
do {
nBytesRead = cryptstream->Read(abBuffer, 0, nBufferSize);
if (!nBytesRead) break;
fOutput->Write(abBuffer, 0, nBytesRead);
} while (nBytesRead == nBufferSize);
}
catch (...) {
return false;
}
so the cryptstream encrytpts/decrypts the input file. the read() reads the file 512 bytes at a time, write() writes the 512 bytes to the output file.
Re: [RESOLVED] how can i encrypt a file?
Quote:
Originally Posted by
katy_price
so the cryptstream encrytpts/decrypts the input file. the read() reads the file 512 bytes at a time, write() writes the 512 bytes to the output file.
More or less, yes. The only tricky part is that the read actually reads from the cryptstream that in turn reads from the input file. (And it does that on its own, so we don't have code for that.)
Re: [RESOLVED] how can i encrypt a file?
Quote:
Originally Posted by
Eri523
More or less, yes. The only tricky part is that the read actually reads from the cryptstream that in turn reads from the input file. (And it does that on its own, so we don't have code for that.)
so the cryptostream reads and moves data through the cryto process in the input file, then the read() reads from the Cryptostream 512 bytes....
sorry how does the buffer work - sorry i no you used a do while loop but what is the buffer for
Re: [RESOLVED] how can i encrypt a file?
Quote:
Originally Posted by
katy_price
sorry how does the buffer work
You mean this one?
Code:
array<Byte> ^abBuffer = gcnew array<Byte>(nBufferSize);
It's a plain array of bytes where bytes get read into and written back out from there. It doesn't do anything on its own.
Re: [RESOLVED] how can i encrypt a file?