1

I have a long in my database. I like to encrypt it. I could use xor but that is not very secure as a pattern can be seen and the xor value may be revealed. IIRC AES has a key and an initialize vector (IV) which can be thought as a seed (when talking about random numbers). I believe the IV should be randomly made. This is so a different pattern occurs when encrypting the same data with the key.

How might i securely encrypt 64bits? I suspect it may not be truely secure w/o a random IV/seed. But i do not want to store it. At the moment the other option i can think of is having a cryptographic RNG and running it say 10000 times if the value is the 10000th index then xor the value. But performance wise that doesn't scale.

How do i properly and securely encrypt 64bits w/o storing extra bits for each 64bits? I'm thinking maybe feed a crypto RNG with (seed xor index) run it X times and xor that with the value. But i have a feeling someone has a better idea?

2 Answers 2

2

AES has a block size of 128 bits, so if you use that, you'll end up encrypting as much padding as meaningful data.

You could use DES (or, preferably, 3DES), since its block size is 64 bits.

The initialization vector should be difficult for the attacker to predict, but it's not actually secret like the key is, so it can be chosen algorithmically; see, for example, the ESSIV algorithm used to generate per-sector IVs for disk encryption. You could use the same scheme — just replace the "sector number" with something analogous like the record's unique ID number — so you don't actually have to store the IV, just regenerate it using the algorithm at decryption time. (But if you ever modify the record's unique ID, make sure you decrypt and re-encrypt with the new IV!)

You can avoid using an IV entirely if you encrypt using ECB mode, but that's not really a good idea. For a given key, the same plaintext always encrypts to the same ciphertext in ECB mode, so an attacker would be able to see that your secret number is the same in two records even if he can't determine the actual value. Assuming that you're not using different keys for each record, you want to have a different IV for each record.

5
  • But if i am using just one block then wouldnt using ECB or not make no difference? ;)
    – user34537
    Jul 13, 2012 at 3:37
  • Not if you have multiple records in your table and you're using the same key for the whole table. You do have multiple blocks; they're just spread across records.
    – Wyzard
    Jul 13, 2012 at 3:38
  • Then wouldnt i have to grab all the rows in order to decrypt the last row?
    – user34537
    Jul 13, 2012 at 3:39
  • No, because you're not encrypting all the blocks as a single stream with a single IV. You'd encrypt each one as a separate (single-block) stream with its own IV, so they're independent of each other. But that distinction doesn't apply to ECB mode, which has no IV.
    – Wyzard
    Jul 13, 2012 at 3:41
  • ohh! so EBC completely ignores the IV. Thats... kind of bad.
    – user34537
    Jul 13, 2012 at 3:45
1

If you have an index of some kind, you can use CTR mode encryption, with the index as a NONCE. In that case you can safely use AES. Be warned that there is an important caveat: if you use the same NONCE a second time then you leak the plain text (because you can retrieve the key stream). If an attacker can view your system in time, this could also hold for encrypting a new value into the database.

Your Answer

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