2
$\begingroup$

I have the following piece of .NET code (see below). I know that DES is not quite secure, I saw that MSDN does not recommend using DES, only for compatibility with legacy programs. I also saw that some people say that IV has to be regenerated every time new message is encrypted.

Q: How secure is the code? Assuming attacker has several hundred messages and corresponding ciphers (and can get more), how easy to break this and find key/IV?. How could it be done? But most importantly, how it could be made secure? Will change to AES help? I'm a beginner and any help or references are very appreciated.

internal static class Encryptor
{      
  private static readonly byte[] Key = { *** };
  private static readonly byte[] Iv = { *** };

  internal static String Encrypt(string source)
  {
    var des = new DESCryptoServiceProvider();
    var enc = des.CreateEncryptor(Key, Iv);
    var b = Encoding.ASCII.GetBytes(source);
    var encId = enc.TransformFinalBlock(b, 0, b.Length);
    return Convert.ToBase64String(encId);
  }

  internal static string Decrypt(string encrypted)
  {
    var des = new DESCryptoServiceProvider();
    var dec = des.CreateDecryptor(Key, Iv);
    var b = Convert.FromBase64String(encrypted);
    var decId = dec.TransformFinalBlock(b, 0, b.Length);
    return Encoding.ASCII.GetString(decId);
  }
}
$\endgroup$
2
  • $\begingroup$ If you're a beginner, use jbtule's answer to Encrypt and decrypt a string. It's one of the few secure examples of symmetric encryption using C#. $\endgroup$ Oct 9, 2014 at 13:45
  • $\begingroup$ If you want to secure communication between two computers, you're better off with SSL/TLS than using low level crypto. $\endgroup$ Oct 9, 2014 at 13:46

2 Answers 2

2
$\begingroup$

The key space for DES is far too small (56 bits). Therefore, any use of DES is not secure. It doesn't matter what mode you use. If the attacker has one plaintext, ciphertext pair, they can brute force the key space and recover the key in a feasible amount of time (24 hours using the cloud).

But most importantly, how it could be made secure? Will change to AES help?

Moving to AES will definitely help. What will help the most is first, defining the desired protection model (i.e., what does "secure" mean in your context?). Do you only care about confidentiality? Do you also care about integrity? How about authenticity? Deciding this will help you decide which mode to use. In particular, do you need a MAC or authenticated encryption mode?

Once you have this figured out, choose the cipher and mode to match your needs, not just security needs, but also performance needs.

$\endgroup$
4
  • $\begingroup$ Thanks a lot for the explanation. In my case the only thing I care is that 3rd party should not be able to recover the key or produce its own message and valid cipher (Integrity as I understand). In my case Encryptor also takes static 8-byte key and static 8-byte Init Vector (so total 64+64 bits). Does this make it bruteforce-proof? Any other vulnerabilities apart from keys should not be in source code? $\endgroup$
    – Vadim K.
    Oct 9, 2014 at 14:52
  • 1
    $\begingroup$ @Jk_xp your code, as is, is completely insecure since it uses DES. Now, let's say you changed it to AES. That would be a step up. But, since the key and IV are hard coded into the code, someone could reverse engineer your executable, and recover those values. Then they have both and can create their own messages. $\endgroup$
    – mikeazo
    Oct 9, 2014 at 14:56
  • $\begingroup$ Thanks, assuming that it is sever-side code (and it is not available for 3rd party), will change to AES be sufficient? $\endgroup$
    – Vadim K.
    Oct 9, 2014 at 14:59
  • $\begingroup$ @Jk_xp I think so. Make sure you are using a good mode. In particular, if you choose CBC or another non-authenticated mode, make sure you understand the implications of doing that in the context of your application. The remaining obvious risk is that your server could be hacked and the key stolen. If that is an acceptable risk to you, then you should be fine. $\endgroup$
    – mikeazo
    Oct 9, 2014 at 15:20
1
$\begingroup$

It is not at all secure if you fix the key and IV in the code, no matter what language you use. Ideally you should generate the key from a password based key derivative function like PBKDF2 or SCRYPT or provision the key from an external key management server. You also need to chose a encryption mode along with the scheme. The modes are picked based on the application you develop. the default mode if you don't specify is usually ECB which is not secure again even if it is AES. Google around for cryptographic modes and PBKDF2 etc.

Edit : for .net the default mode seems to be CBC Mode. One may carefully check for each language what the default mode is.

$\endgroup$
4
  • $\begingroup$ It looks like the default mode is CBC. msdn.microsoft.com/en-US/library/… $\endgroup$
    – Nova
    Oct 9, 2014 at 3:48
  • $\begingroup$ @sashank Thanks a log for your answer. Is there any other problems apart from keys stored in the source code? $\endgroup$
    – Vadim K.
    Oct 9, 2014 at 14:56
  • $\begingroup$ Would suggest, you spend some more time on various modes. and find out which suits your application $\endgroup$
    – sashank
    Oct 9, 2014 at 15:09
  • $\begingroup$ if you have client and server and use AES to encrypt data and need to encrypt/decrypt on both sides with the same key, there is no other option than to store key in source... $\endgroup$
    – juFo
    Aug 29, 2018 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.