Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using System.Text;
- using System.Xml.Serialization;
- namespace System.Security.Cryptography
- {
- public static class RijndaelManagedExtensions
- {
- // Structure to store required Rijndael parameters
- public struct RijndaelParameters
- {
- public byte[] IV;
- public byte[] KEY;
- public RijndaelParameters(byte[] IV, byte[] KEY)
- {
- this.IV = IV;
- this.KEY = KEY;
- }
- }
- // Extract Rijndael Parameters as byte[]
- public static byte[] GetParamBytes(this RijndaelManaged rijndael)
- {
- var sw = new StringWriter();
- new XmlSerializer(typeof(RijndaelParameters)).Serialize(sw, new RijndaelParameters(rijndael.IV, rijndael.Key));
- return Encoding.UTF8.GetBytes(sw.ToString());
- }
- // Construct a new RijndaelManaged via ParamBytes (obtained from GetParamBytes)
- public static RijndaelManaged ConstructRijndaelManaged(this byte[] ParamBytes)
- {
- RijndaelParameters Params = (RijndaelParameters)new XmlSerializer(typeof(RijndaelParameters)).Deserialize(new StringReader(Encoding.UTF8.GetString(ParamBytes)));
- return new RijndaelManaged
- {
- IV = Params.IV,
- Key = Params.KEY
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment