Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Encryption Key for Encryption Algorithms?

I want to use encryption algorithm available in .Net Security namespace, however I am trying to understand how to generate the key, for example AES algorithm needs 256 bits, that 16 bytes key, and some initialization vector, which is also few bytes.

  1. Can I use any combination of values in my Key and IV? e.g. all zeros in Key and IV are valid or not? I know the detail of algorithm which does lots of xors, so zero wont serve any good, but are there any restrictions by these algorithms?

  2. Or Do I have to generate the key using some program and save it permanently somewhere?

I want to store data in database after encryption, the secure profile data like username, password, phone number etc, and the key will be available to database user mentioned in connection string only, and to the administrator.

like image 455
Akash Kava Avatar asked Mar 23 '10 20:03

Akash Kava


People also ask

How is an encryption key created?

An encryption key is typically a random string of bits generated specifically to scramble and unscramble data. Encryption keys are created with algorithms designed to ensure that each key is unique and unpredictable. The longer the key constructed this way, the harder it is to break the encryption code.


1 Answers

You really ought to do this the correct way :)

1) Use a securely generated random IV
2) Use a securely generated random key
3) Don't use ECB mode - EVER

AesManaged aes = new AesManaged(); aes.GenerateKey(); aes.GenerateIV(); 

The code above will correctly and securely generate a random IV and random key for you.

like image 59
Michael Howard-MSFT Avatar answered Sep 28 '22 01:09

Michael Howard-MSFT