TizzyT

RijndaelManagedExtensions -TizzyT

Jun 21st, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System.IO;
  2. using System.Text;
  3. using System.Xml.Serialization;
  4.  
  5. namespace System.Security.Cryptography
  6. {
  7.     public static class RijndaelManagedExtensions
  8.     {
  9.         // Structure to store required Rijndael parameters
  10.         public struct RijndaelParameters
  11.         {
  12.             public byte[] IV;
  13.             public byte[] KEY;
  14.  
  15.             public RijndaelParameters(byte[] IV, byte[] KEY)
  16.             {
  17.                 this.IV = IV;
  18.                 this.KEY = KEY;
  19.             }
  20.         }
  21.  
  22.         // Extract Rijndael Parameters as byte[]
  23.         public static byte[] GetParamBytes(this RijndaelManaged rijndael)
  24.         {
  25.             var sw = new StringWriter();
  26.             new XmlSerializer(typeof(RijndaelParameters)).Serialize(sw, new RijndaelParameters(rijndael.IV, rijndael.Key));
  27.             return Encoding.UTF8.GetBytes(sw.ToString());
  28.         }
  29.  
  30.         // Construct a new RijndaelManaged via ParamBytes (obtained from GetParamBytes)
  31.         public static RijndaelManaged ConstructRijndaelManaged(this byte[] ParamBytes)
  32.         {
  33.             RijndaelParameters Params = (RijndaelParameters)new XmlSerializer(typeof(RijndaelParameters)).Deserialize(new StringReader(Encoding.UTF8.GetString(ParamBytes)));
  34.             return new RijndaelManaged
  35.             {
  36.                 IV = Params.IV,
  37.                 Key = Params.KEY
  38.             };
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment