Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm writing a demo application in .NET 4.0 which allows user to encrypt a file using DES encryption. This part I have completed.

Now, I want to read that same file in blocks of 8 bytes and display the output to the user as each block is deciphered. I intentionally don't want to decipher the whole file.

I'm using ECB for encryption mode hence no salt value is used. Also the Block size is 64 bits and padding schema is PKCS7.
Posted

1 solution

This is not how encryption works. If you want to do that, you should encrypt those 8-byte blocks separately, which hardly can be practical, in case of such a small block size. I would first think if this is what you really want. After all, if a user can get any of 8-byte of decrypted block, someone can pretty easily obtain the whole file by doing some programming work. I would personally be able to do that. So, what's the point?

I think the right solution should lies in some different direction: you need to analyze your security scenarios based on holistic approach, the whole thing, review possible exploits theoretically and then address the problem as a whole. If you want some of us to help you, you probably need to describe your working scenarios and requirements in detail.

—SA
 
Share this answer
 
v3
Comments
i_syed2000 10-Jul-13 17:28pm    
Thanks Sergey. When using the ECB mode with DES, each block is encrypted standalone using the same key. So you can read each block and decrypt it independently of all the other blocks. In practice, I would really be reading one 'log line' which may span over mutiple encryption blocks.

For example consider the following log lines.
Line 1: Entering Method ABC\r\n
Line 2: Saving Customer\r\n
Line 3: Exiting Method ABC\r\n

Line 1 may span block 0 to N
Line 2 may span block N+1 to M
Line 3 may span block M+1 to ..
Sergey Alexandrovich Kryukov 10-Jul-13 17:41pm    
Well, you can, but what's the problem? I thought you encrypted the whole file and wanted to decrypt the fragment...
I guess, you spend a lot of encrypted bytes on that...
Anyway, it's not apparent to me that it makes your application more secure, I tried to explain why.
—SA
i_syed2000 10-Jul-13 17:49pm    
Okay. So lets say my encrypted log file is 200 MB Long after months of log writing. I can load the whole file in Memory, decrypt it and show all at once to the user. However, I'm trying avoid hogging user's memory. If I decide to move forward with it, I will probably switch to AES. But first, I need to solve the problem mentioned above.

The problem part is the padding. Padding is only applied when the data block size is less than 64 bits. For some reason, .net code is not updating the 'decrypted' memory stream.
See code below.

Sample String Encrypted: ABCDEFGHI
Total Encrypted Bytes: 16 (because of padding).
Goal: Decrypt and read the first block which contains 'ABCDEFGH'.

string DecryptData(string encryptedData)
{
byte[] dataToDecrypt = Convert.FromBase64String(encryptedData);
byte[] decryptedData = new byte[8];
DESCryptoServiceProvider secProvider = new DESCryptoServiceProvider();
secProvider.Mode = CipherMode.ECB;
secProvider.Padding = PaddingMode.PKCS7;
secProvider.Key = ASCIIEncoding.ASCII.GetBytes(encryptionKey);
ICryptoTransform transform = secProvider.CreateDecryptor();
MemoryStream encryptedDataStream = new MemoryStream(dataToDecrypt);
MemoryStream decryptedDataStream = new MemoryStream();

CryptoStream stream = new
CryptoStream(decryptedDataStream,transform,CryptoStreamMode.Write);
stream.Write(dataToDecrypt, 0, 8);
stream.Flush();
//PROBLEM: doesn't flush bytes to decryptedDataStream unless I change padding to 'None'.
}
Sergey Alexandrovich Kryukov 10-Jul-13 18:20pm    
Yes, yes, I see now... I guess you should have shown this code in first place...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900