Simple Text File Encryption (C#)

Posted on 2008-11-14
Last Modified: 2013-12-17
Can someone provide me an example of how to encrypt a text file that already exists... and also an example to unencrypt it? It doesnt have to be the strongest thing in the world... I just want it to be a step above setting the file parameter to "hidden". Thanks.
Question by:Pretzel_Jesus
    LVL 12

    Expert Comment

    LVL 12

    Accepted Solution

    Here you go. I have tested this code.
    // Encrypt file
    			DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider) DESCryptoServiceProvider.Create();
    			string key = ASCIIEncoding.ASCII.GetString(desCrypto.Key);
    			FileStream fsInput = new FileStream(@"C:\InputFile.txt", FileMode.Open, FileAccess.Read);
    			FileStream fsEncrypted = new FileStream(@"C:\EncryFile.enc", FileMode.Create, FileAccess.Write);
    			ICryptoTransform desEncrypt = desCrypto.CreateEncryptor();
    			CryptoStream cryptoStream = new CryptoStream(fsEncrypted, desEncrypt, CryptoStreamMode.Write);
    			byte[] byteArrayInput = new byte[fsInput.Length - 1];
    			fsInput.Read(byteArrayInput, 0, byteArrayInput.Length);
    			cryptoStream.Write(byteArrayInput, 0, byteArrayInput.Length);
    			// Decrypt the file
    			FileStream fsRead = new FileStream(@"C:\EncryFile.enc", FileMode.Open, FileAccess.Read);
    			ICryptoTransform desDecrypt = desCrypto.CreateDecryptor();
    			CryptoStream decryptoStream = new CryptoStream(fsRead, desDecrypt, CryptoStreamMode.Read);
    			StreamWriter fsDecrypted = new StreamWriter(@"C:\OutputFile.txt");
    			StreamReader reader = new StreamReader(decryptoStream);
    			string fileText = reader.ReadToEnd();

    Open in new window


    Author Closing Comment

    Exactly what I needed thanks!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now