Rijndael algorithm (how to create your own key)

All samples of the Rijndael algorithm determine the key to the Rijndael class itself, can we provide our own key. Any hint of this will help me a lot.

The sample application we are building is for Windows Mobile, and it does not support PasswordDeriveBytes

Thanks in advance Gita

Update on this topic: In accordance with the code example below, we tried it and it seems to work, but there is a slight hiccup in this. when we decrypt the data, on the right side of the value for the example there is an 8-bit addition, we encrypt a unique key for the transaction and look like this:

Before encryption: MI03112009044625000000000000008024754008

After decryption: MI03112009044625000000000000008024754008 揞 ⑁㋬ 㓠 ⥳ 空 ⠜ 資

can anyone help with this correct addition occurring in the original value.

thanks gita

+4
source share
3 answers

You can try something like this based on the RDN article for the RijndaelManaged Class , which I also recommend you read.

var plainText = "This will be encrypted."; var aesAlg = new RijndaelManaged(); aesAlg.Key = new byte[32] { 118, 123, 23, 17, 161, 152, 35, 68, 126, 213, 16, 115, 68, 217, 58, 108, 56, 218, 5, 78, 28, 128, 113, 208, 61, 56, 10, 87, 187, 162, 233, 38 }; aesAlg.IV = new byte[16] { 33, 241, 14, 16, 103, 18, 14, 248, 4, 54, 18, 5, 60, 76, 16, 191}; ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); msEncrypt = new MemoryStream(); using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } } return msEncrypt.ToArray(); 
+6
source

The key property of an Rijndael instance takes byte [] as the key. Make sure you install it in an array with a valid size for the algorithm.

Link to msdn: http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.key.aspx

+2
source

What do you mean, cannot provide your own key? Here is an example of how you do this.

 public static string Encrypt(string Text, byte[] key, byte[] VectorBytes){ try{ byte[] TextBytes = Encoding.UTF8.GetBytes(Text); RijndaelManaged rijKey = new RijndaelManaged(); rijKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = rijKey.CreateEncryptor(key,VectorBytes); MemoryStream memoryStream = new MemoryStream(); cryptoStream.Write(TextBytes, 0, TextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = memoryStream.ToArray(); memoryStream.Close(); cryptoStream.Close(); string cipherText = Convert.ToBase64String(cipherTextBytes); return cipherText; } catch (Exception e){ MessageBox.Show("Falsches Passwort "+ e.Message.ToString()); string t = ""; return t; } } 
+2
source

All Articles