views:

330

answers:

5

I have to encrypt the output file in A application, then decrypt it in B application, but I found there are some limitations with MS encryption, if I encrypt a 1000 bytes buffer and then want to decrypt start for different position with different size in B application, the return values are error. Is there any encryption can meet my requirement? Thanks. Here is my sample codes:

clTemp.EncryptDataDirectly(buffer, 1000);
clTemp.DecryptDataDirectly(buffer + 1, 500);
A: 

You want to encrypt something and then only decrypt a portion of it? starting not at the beginning? did I understood ok?

gbianchi
Yes, you are right.
Yigang Wu
Well. You have to use a very basic encryption system. Like CX (Cesar method). Any numeric method will be impossible to decrypt (since it depends on every byte of data).
gbianchi
Thanks for your information, where can I get some sample codes or document to see how to implement the CX(Cesar method)? Thanks.
Yigang Wu
The Cesar algorithm is not a state of the art encryption algorithm. It got its name form its assumed inventor the roman emperor Gaius Julius Caesar (100 years BC) and encrypts a plaintext by simple rotation of an alphabet. So it's not really secure.
Flo
Also, please ask questions as comments to the original question.
Stefan Thyberg
Flo: He didn't ask for a good algorithm, he ask for one that could make it work.Stefan: thanks for the head up, will take in mind from now on.
gbianchi
+1  A: 

You can try to use a block cipher algorithm that can be used in in electronic code book (ECB) mode, that means that the encryption of every block is independent to the encryption of its predecessor. This allows you to start the decryption at any point in your buffer as long as you're starting at the beginning of an code block and no within a code block.

Fo example you chose the DES algorithm for encryption which has a block size of 64 bit. So you have to decrypt your cipher with the same block size as you encrypted the plaintext. But you don't have to start an the beginning of your cipher you can also start at each point which is a multiple of 64.

Perhaps this might help you.

Some more info on block cipher:

Block cipher

Block cipher modes

Caesar cipher (this is not a block chiper)

Flo
+1  A: 
eed3si9n
+3  A: 

Disclaimer: I don't know this Windows API for encryption. So this is just a general outline of the problem. Use your common sense and the documentation to find out more.

There a two possibilities:

  1. Stream-ciphers. They encrypt one byte (or maybe even bit) at the time and you have to start at the beginning of the encrypted stream to decrypt it properly. Some ciphers have synchronization abilities that help you recover after some small error in the transmission.

  2. Block-ciphers. They encrypt fixed sized blocks, 64 or 128 bit are popular block sizes. But they can use different modes for encryption. ECB for example encrypts every block by itself. you could jump into the middle of your file, grab a block and decrypt it. But it leaves you open to known plaintext attacks where an attacker knows some encrypted text and tries to find the key you used. CBC or some other mode with feedback is more secure, but in this case you need again to start decrypting at the beginning of your encrypted file.

Here are some relevant Wikipedia articles:

unbeknown
+1  A: 

One solution is to use a cipher with CTR (Counter) block mode. It allows random access during decryption; the blocks are not dependent on each other, you only need to know the key and the block number.

See wikipedias description for details.

Anders Westrup