On Wed, 19 Jul 2006 01:54:01 -0700, den 2005
<(E-Mail Removed)> wrote:
>Hi everybody,
>
> I am not sure where to put this in this forum. So, I posted this at
>several topics. I created a class library that has two public methods
>Encrypt() and Decrypt(). I reference this dll to a window application. I used
>DESCryptoServiceProvider Algorithm to encrypt and decrypt then with same Key
>and IV. But unable to decrypt it back to original text. This project I plan
>to use all algorithm and Hash. This is Phase One. There is no problem
>ingenerating the Key and IV and at both encrypt and decrypt they are the
>same. Can anyone spot the mistake and know how to correct this? Thanks.
>
>
1 Why are you using DES rather than AES? DES is now obsolete.
Triple-DES is acceptable if you have to link with an existing
application. Use AES for anything else.
2 Your application returns strData even if it finds an exception, this
is a potential security leak. Whenever an exception is thrown you
need to destroy all information for the current message, both
cyphertext and plaintext. Dispose as much as you can, set every
element of encryptedData[] and decryptedData[] to zero. Set strData
to string.Empty. Ideally you should wipe the previous contents of
strData first:
unsafe void OverwriteString(string text) {
const char overwriteChar = 'X';
fixed (char* cp = text) {
for (int i = 0; i < text.Length; ++i) {
cp[i] = overwriteChar;
} // end for
} // end fixed
} // end OverwriteString()
3 Your code is very complex, to me at least it looks more complex then
it needs to be. For security stuff keeeping it simple with very few
options is better; if your users do not have to option to select
keySize then they cannot make the mistake of selecting one that is too
small - keySize = 1? Take out as much of the complexity as you can.
3 To find the problem simplify and see if the problem persists,
something like:
[pseudocode]
byte[] Key = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
byte[] IV = {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}
string plaintext1 = "Hello World!"
byte[] cyphertext = Encrypt(plaintext1, Key, IV)
string plaintext2 = Decrypt(cyphertext, Key, IV).ToString()
if (plaintext != plaintext2) {
WriteLine("Failed")
} else {
WriteLine("OK")
}
[/pseudocode]
It will be much easier for you to see problems in the simpler code.
Once the simple code is working you can add back the required
complications one at a time, retesting after each new complication.
4 You are using Unicode and ASCII encodings at different places. It
might be worth checking that the mixed character codings are not
causing the problem rather than the encryption/decryption.
rossum