Re: Encrypt and Decrypt in C#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



SP wrote:

>>There are several things I am not too happy about in that code:
>
>
> It was one of those things that once it worked and it was being used then I
> did not want to change it. You make some valid points and I do have some
> comments and questions inline.

It wasn't that bad, it was just that when I got started looking at the
code more and more things kept drawing my attention...

>> - It uses DES-encryption
> Can you explain why DES is not the best choice.

DES is not "secure". it's keys are 56-bits, modern CPU's can brute force
DES faster than you can change the keys, custom hardware can do it in
minutes. Rinjdael (now AES) have longer key-lengths and generally
considered much more resistant to attacks.

>> - It is designed with the keys in the code
>
> How can you decrypt without the keys in your code somewhere. I place the
> keys in one place and they are encrypted by an obfuscator. How do you avoid
> placing the keys in your code?

The keys were actually duplicated, as the strings "1234...".

To avoid having the keys in the code, either prompt for them, have them
on disk or use any other means of input. Even if you know them to be
constant, have them in a file or something, so you can provide different
keys to different people.

BTW: an obfuscator will not really help against any, even just slightly,
qualified attacker.

>> - It uses a constant IV
>
> Is it better to use an encrypted and unknown IV or a random but known IV?
> You suggested above to use a random IV and store it in clear text.

The IV isn't secret, it's used to seed the encryption. No secrecy is
lost by transferring it it clear-text, nor is any secrecy gained by
obscuring it or "encrypting" it.

Choosing a random IV gives you non-deterministic cryptography -- that
is, multiple encryptions of the same clear is encrypted to different
cipher-texts.

>> - It doesn't come with a test (I know, that's a cheap-shot ;)
>
> I did not provide the unit tests. They are in a separate class. Of course it
> is easy to test.

Good, cryptographic code should almost always be unit-tested, it's
really hard to debug by inspecting the encrypted data ;)

>> - It converts data, to and from Base64
>
> Originally it was to be a license key so it may of needed to be typed in.

En/De-cryption should be a separate concen from what format the
input-data is in. If you need to Base64 convert somewhere, just do it on
the in/output of the de/en-cryption.

>> - It duplicates the code for creating the en/de-cryption keys
>
> Because I do not provide the Encrypt function in the Release build, only the
> Decrypt.

That doesn't prevent you from having a separate function for creating
the en/de-cryption keys. (http://c2.com/cgi/wiki?OnceAndOnlyOnce)

>> - It only works for strings with a valid ASCII-encoding
>> - It only works for keys with a valid ASCII-encoding
>> - It uses the ascii-encoding of key-strings as keys
>
> I knew that the text going in was ASCII but do not want to change anything
> at this point.

Fine, note that valud ASCII-text is encoded exactly the same in ASCII
and UTF8.

Using the ascii-encoding of key-strings directly as keys is frowned upon
since at dramatically reduces the used keyspace. Hashing the keys
removes this problem.

--
Helge Jensen
mailto:helge.jensen@xxxxxxx
sip:helge.jensen@xxxxxxx
-=> Sebastian cover-music: http://ungdomshus.nu <=-
.



Relevant Pages

  • Re: Encrypting again an already encrypted file increase security ?
    ... > If I encrypt exactly this file again with another encryption algorithm tool ... twice, with different keys. ... because every concatenation of cipher operations is a cipher ... On the other hand, if you do the same with DES, things are different. ...
    (sci.crypt)
  • RE: Email Encryption Between Servers
    ... Secure E-mail, PGP, secure web server, ... Are the doctors going to have separate keys for each provider, doctor, ... desktop e-mail encryption, enterprise e-mail encryption. ... manage key exchange, staff training, ...
    (Security-Basics)
  • Re: Which Drive Encryptor for this?
    ... detained without charge indefinitely. ... demand your access keys. ... The USA is not alone in this - in the UK you can be jailed for refusing to hand over encryption keys to the authorities. ... displayed on his laptop display and the customs officer saf that. ...
    (comp.sys.ibm.pc.hardware.storage)
  • Re: Which Drive Encryptor for this?
    ... detained without charge indefinitely. ... demand your access keys. ... encryption keys, the current state of affairs seems to be that ... displayed on his laptop display and the customs officer saf that. ...
    (comp.sys.ibm.pc.hardware.storage)
  • My response to a message by Dorothy Denning in 1995 - Australia and Encryption Policy
    ... Subject: Australia and Encryption Policy ... interception, which includes the issue of the use of cryptography as: ... keys but may be required to provide them in response to a court order. ...
    (sci.crypt)