Click here to Skip to main content
15,902,447 members
Home / Discussions / C#
   

C#

 
GeneralRe: Close 'X' [modified] Pin
RussBus7-Dec-07 4:59
RussBus7-Dec-07 4:59 
GeneralUsing NPlot .NET component in Access VBA Pin
Thanks for all the fish6-Dec-07 11:59
Thanks for all the fish6-Dec-07 11:59 
GeneralRe: Using NPlot .NET component in Access VBA Pin
NassosReyzidis6-Dec-07 23:06
NassosReyzidis6-Dec-07 23:06 
QuestionUniqueness, authentication, licensing oh my Pin
askey6-Dec-07 10:46
askey6-Dec-07 10:46 
GeneralRe: Uniqueness, authentication, licensing oh my Pin
martin_hughes6-Dec-07 11:00
martin_hughes6-Dec-07 11:00 
GeneralRe: Uniqueness, authentication, licensing oh my Pin
DaveyM696-Dec-07 13:43
professionalDaveyM696-Dec-07 13:43 
GeneralRe: Uniqueness, authentication, licensing oh my Pin
askey6-Dec-07 17:05
askey6-Dec-07 17:05 
GeneralFile (XmlDocument) Encryption / Decryption improvements Pin
DaveyM696-Dec-07 8:21
professionalDaveyM696-Dec-07 8:21 
Hi,

After many hours of searching, I've managed to get this working using the code below - but I'm wondering if anyone has any suggestions on improving it? I'm sure this is not as efficient as it could be!

Also - how do I go about locking the target file during the read and write?

#region Encryption
    class RijndaelEncrypt
    {
        public static void Encrypt(XmlDocument ThisDocument, string FullPath)
        {
            //Save XML to memory stream
            MemoryStream ms = new MemoryStream();
            ThisDocument.Save(ms);
            //Convert memory stream to byte array
            byte[] xmlArray = ms.ToArray();
            //Encrypt
            RijndaelManaged Rij = new RijndaelManaged();
            Rij.Key = ASCIIEncoding.ASCII.GetBytes(Constants.Misc.MyKey); //MyKey is 16 character string
            Rij.IV = ASCIIEncoding.ASCII.GetBytes(Constants.Misc.MyKey);
            ICryptoTransform rijEncrypt = Rij.CreateEncryptor();
            byte[] encryptedData = rijEncrypt.TransformFinalBlock(xmlArray, 0, xmlArray.Length);
            //Create file stream and write encrypted data
            FileStream fsout = new FileStream(FullPath, FileMode.Create);
            fsout.Write(encryptedData, 0, encryptedData.Length);
            fsout.Close();
        }

        public static XmlDocument Decrypt(string FullPath)
        {
            //Create file stream and get file length
            FileStream fsin = new FileStream(FullPath, FileMode.Open, FileAccess.Read);
            FileInfo finfo = new FileInfo(FullPath);
            //Create binary reader and read from file stream
            BinaryReader br = new BinaryReader(fsin);
            //Decrypt
            byte[] encryptedData = br.ReadBytes((int)finfo.Length);
            RijndaelManaged Rij = new RijndaelManaged();
            Rij.Key = ASCIIEncoding.ASCII.GetBytes(Constants.Misc.MyKey);
            Rij.IV = ASCIIEncoding.ASCII.GetBytes(Constants.Misc.MyKey);
            ICryptoTransform rijDecrypt = Rij.CreateDecryptor();
            byte[] decryptedData = rijDecrypt.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            //Create XML document, fill with decrypted data and return the document
            XmlDocument ThisDocument = new XmlDocument();
            ThisDocument.LoadXml(Encoding.UTF8.GetString(decryptedData));
            return ThisDocument;
        }
    }
    #endregion

GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
led mike6-Dec-07 8:57
led mike6-Dec-07 8:57 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
DaveyM696-Dec-07 9:03
professionalDaveyM696-Dec-07 9:03 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
Ennis Ray Lynch, Jr.6-Dec-07 9:12
Ennis Ray Lynch, Jr.6-Dec-07 9:12 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
DaveyM696-Dec-07 9:23
professionalDaveyM696-Dec-07 9:23 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
led mike6-Dec-07 9:49
led mike6-Dec-07 9:49 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
DaveyM696-Dec-07 11:39
professionalDaveyM696-Dec-07 11:39 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
Skippums7-Dec-07 5:36
Skippums7-Dec-07 5:36 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
DaveyM699-Dec-07 23:43
professionalDaveyM699-Dec-07 23:43 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
Skippums10-Dec-07 4:34
Skippums10-Dec-07 4:34 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
DaveyM6910-Dec-07 5:08
professionalDaveyM6910-Dec-07 5:08 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
Skippums10-Dec-07 5:58
Skippums10-Dec-07 5:58 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
DaveyM6910-Dec-07 10:02
professionalDaveyM6910-Dec-07 10:02 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
Skippums6-Dec-07 9:16
Skippums6-Dec-07 9:16 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
Ennis Ray Lynch, Jr.6-Dec-07 9:19
Ennis Ray Lynch, Jr.6-Dec-07 9:19 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
Skippums6-Dec-07 9:56
Skippums6-Dec-07 9:56 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
DaveyM696-Dec-07 11:18
professionalDaveyM696-Dec-07 11:18 
GeneralRe: File (XmlDocument) Encryption / Decryption improvements Pin
ekynox6-Dec-07 9:24
ekynox6-Dec-07 9:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.