Rijndael algorithm (How to create our own Key )

前端 未结 3 567
自闭症患者
自闭症患者 2021-02-10 09:23

All the samples for Rijndael algorithm are defining the key from the Rijndael class itself, can\'t we provide the Key of our own. Any hint on this will help me a lot.

Th

3条回答
  •  春和景丽
    2021-02-10 09:43

    What do you mean by cannot provide the key of our own? Here's an example on how you do it.

    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;
        }
    }
    

提交回复
热议问题