/// <summary> /// Load the properties of this instance, from the properties of another instance of the class <see cref="SecureGram"/>. /// </summary> /// <param name="obj">The existing object instance source for the property values.</param> public void Load(SecureGram obj) { this._sender = obj.Sender; this._created = obj.Created; this._subject = obj.Subject; this._messageLength = obj.MessageLength; this._isCompressed = obj.IsCompressed; this._message = obj.Message; }
/// <summary> /// Creates the encrypted Datagram. /// Client provides the encryption method to use as <typeparam name="T">SymmetricAlgorithm</typeparam>. /// </summary> /// <param name="key"></param> /// <param name="salt"></param> /// <returns></returns> public string CreateGramContent <T>(string key, string salt) where T : SymmetricAlgorithm, new() { DateTime Now = DateTime.Now.ToUniversalTime(); this._created = string.Format("{0:yyyy-MM-dd-TZ-HH:mm:ss.fff}", Now); this._messageLength = this._message.Length; StringBuilder SaltedMessage = new StringBuilder(this.Message); Random r = new Random((int)DateTime.Now.Ticks); int charCount = r.Next(3, 25); for (int charIndex = 0; charIndex < charCount; charIndex++) { SaltedMessage.Append((char)r.Next(32, 90)); } SecureGram work = new SecureGram(this); work.IsCompressed = this._messageLength >= 10000; if (work.IsCompressed) { MemoryStream messageCompressed = new MemoryStream(); using (var outGZipStream = new GZipStream(messageCompressed, CompressionLevel.Optimal)) { using (StreamWriter sw = new StreamWriter(outGZipStream)) { sw.Write(SaltedMessage.ToString()); sw.Close(); } } work.Message = Convert.ToBase64String(messageCompressed.ToArray()); } CipherUtility cipher = new CipherUtility(new T()); return (cipher.Encrypt( ObjectJsonSerializer <SecureGram> .CreateDocumentFormat(work), key, salt, Base64FormattingOptions.InsertLineBreaks)); }
/// <summary> /// Initializes a new instance of the class <see cref="SecureGram"/>, using an existing instance as the parameter. /// </summary> /// <param name="obj">The existing instance source for the property values.</param> public SecureGram(SecureGram obj) { this.Load(obj); }