Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Simple Cryptographer - Simple DES/AES Implementation in C#

Rate me:
Please Sign up or sign in to vote.
2.81/5 (29 votes)
21 May 2007CPOL4 min read 292.3K   10.8K   55  
It's very simple and does not focus on performance, but I think that it is simple.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SimpleCryptographer.AES
{
    class FileIO
    {
        public static string FileReadToBinary(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open);

            Console.WriteLine("File size : " + fs.Length);

            #region Read from file 100bytes per 1 time and transform to binary data.

            int fileLength = (int)fs.Length;

            StringBuilder text = new StringBuilder((int)fs.Length * 8);

            byte[] bytes = new byte[100];
            int startindex = 0;
            int IsEnd = -1;

            while (fs.Read(bytes, startindex, bytes.Length) != 0)
            {
                if (IsEnd > 0)
                {

                }
                foreach (byte b in bytes)
                {
                    if (text.Length == fileLength * 8)
                    {
                        break;
                    }

                    text.Append(BaseTransform.FromDeciamlToBinary(b));
                }
            }

            fs.Close();

            return text.ToString();

            #endregion
        }

        public static void WriteBinaryToFile(string filename, string binaryText)
        {
            #region Write binary encrypted or decrypted data to file.

            FileStream fs = new FileStream(filename, FileMode.Create);
            StringBuilder sub_text = new StringBuilder(800);
            byte[] bytes = new byte[100];
            int length = 800;

            for (int i = 0; i <= binaryText.Length / 800; i++)
            {
                int remain = binaryText.Length - i * 800;
                if (remain < 800)
                {
                    length = remain;
                }

                sub_text.Remove(0, sub_text.Length);
                sub_text.Append(binaryText.Substring(i * 800, length));

                for (int j = 0; j < sub_text.Length / 8; j++)
                {                   
                    bytes[j] = BaseTransform.FromBinaryToByte(sub_text.ToString().Substring(j * 8, 8));
                    if (remain < 800)
                    {
                        Console.Write(bytes[j].ToString());
                    }
                }

                fs.Write(bytes, 0, sub_text.Length / 8);
            }
            Console.WriteLine();
            fs.Close();

            #endregion
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Korea (Republic of) Korea (Republic of)
I'm a super-elementary programmer.
I live in Suwon, Republic of Korea.
And i'm not very good at English.
hahaha!!!

Comments and Discussions