开发者

How can i set key and vector when doing encryption?

I 开发者_如何学Cam using the following code. The key and vector are copied from the web. No secrets there.

    private static byte[] key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
    private static byte[] vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 221, 112, 79, 32, 114, 156 };
    private ICryptoTransform encryptor, decryptor;
    private UTF8Encoding encoder;

    public SimplerAES()
    {
        RijndaelManaged rm = new RijndaelManaged();
        encryptor = rm.CreateEncryptor(key, vector);
        decryptor = rm.CreateDecryptor(key, vector);
        encoder = new UTF8Encoding();
    }

Can someone explain what's the meaning of the settings for key and vector. I would like to privately code up the way my data is encrypted but I am not sure where to start.

Thanks,


The key is simply your encryption key, the passphrase that someone needs to decrypt the data. AES is a symmetric cipher, so the same key will be used both for encryption and decryption.

The initialization vector (IV) is a technical detail: when using a block cipher in CBC mode, the ciphertext for each input block depends on the ciphertext for the previous input block (and also on the current input block and the key, of course). However, when trying to encrypt the first block there is no "previous" data to take into account; the IV takes the place of the ciphertext for the "0th" input block in this case.

In practice, this means that when decrypting, it's also necessary to know the IV in addition to the key. However the IV is not sensitive information, and you can simply store it along with the ciphertext (thus making the key the only information you need to decrypt that you do not already have).

Note: It's good practice to not use the same IV multiple times; you can simply generate a random one each time you need to encrypt something.


Check out the following sites should help.

Encryption 1

Encryption 2

Encryption 3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜