/// <summary>
        /// Writes a single row to a CSV file.
        /// </summary>
        /// <param name="row">The row to be written</param>
        public void WriteRow(CsvRow row)
        {
            StringBuilder builder     = new StringBuilder();
            bool          firstColumn = true;

            foreach (string value in row)
            {
                // Add separator if this isn't the first value
                if (!firstColumn)
                {
                    builder.Append(',');
                }
                // Implement special handling for values that contain comma or quote
                // Enclose in quotes and double up any double quotes
                if (value.IndexOfAny(new char[] { '"', ',' }) != -1)
                {
                    builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
                }
                else
                {
                    builder.Append(value);
                }
                firstColumn = false;
            }
            row.LineText = builder.ToString();
            string finalText = row.LineText;

            if (encrypted)
            {
                finalText = CryptoUtils.EncryptStringAES(row.LineText);
                finalText = HexString.ToHexString(finalText);
            }
            WriteLine(finalText);
        }
        public void TestEncryption()
        {
            string teststr   = "Random String";
            string encrypted = CryptoUtils.EncryptStringAES(teststr);

            Console.WriteLine(teststr + " -- Encrypted with key=" + CryptoUtils.getKey() + " : " + encrypted);
        }
Exemple #3
0
        /// <summary>
        /// Serializes an object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serializableObject"></param>
        /// <param name="fileName"></param>

        public static void SerializeObject <T>(T serializableObject, string fileName, bool encrypted = false)
        {
            if (serializableObject == null)
            {
                return;
            }

            try
            {
                XmlDocument xmlDocument = new XmlDocument();

                XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
                using (MemoryStream stream = new MemoryStream())
                {
                    serializer.Serialize(stream, serializableObject);
                    stream.Position = 0;
                    if (encrypted)
                    {
                        // This resets the memory stream position for the following read operation
                        stream.Seek(0, SeekOrigin.Begin);

                        // Get the bytes
                        var bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, (int)stream.Length);
                        var serializedString = System.Text.Encoding.Default.GetString(bytes);
                        // Encrypt your bytes with your chosen encryption method, and write the result instead of the source bytes
                        string encryptedBytes = CryptoUtils.EncryptStringAES(serializedString);
                        File.WriteAllText(fileName, encryptedBytes);
                    }
                    else
                    {
                        xmlDocument.Load(stream);
                        xmlDocument.Save(fileName);
                        stream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Serialiazing Object: " + ex);
                throw (new SystemException(ex.ToString()));
            }
        }