AES Cryptography Using .NET (Using AesCryptoServiceProvider Class)
Post date: Mar 7, 2011 10:55:20 AM
Characters are NOT the same as bytes.
Encoding
The character 'A' must be encoded into a certain bit pattern that the machine can use. This is a mostly arbitrary decision on the part of hardware implementors. You could say that A=1, B=2, and so on. For ASCII (the American Standard for Computer Information Interchange), designers chose a 7-bit encoding (A=1000001), giving room for encoding 128 different characters in those seven bits.
Since a byte is 8-bits on most hardware today, the hardware just pads the extra bit with a zero (A=01000001). So even in the case of simple ASCII, characters are not the same thing as bytes.
ASCII reserves 32 encodings for special characters like the TAB, NEWLINE, FORMFEED, and then the remaining encodings are for printing characters like SPACE, !, @, #, $, A-Z, a-z, 0-9, and so on.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
Methods are provided to convert arrays and strings of Unicode characters to and from arrays of bytes encoded for a target code page.
A number of Encoding implementations are provided in the System.Text namespace, including:
The ASCIIEncoding class encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F.
The UnicodeEncoding class encodes each Unicode character as two consecutive bytes. Both little-endian (code page 1200) and big-endian (code page 1201) byte orders are supported.
The UTF7Encoding class encodes Unicode characters using the UTF-7 encoding (UTF-7 stands for UCS Transformation Format, 7-bit form). This encoding supports all Unicode character values, and can also be accessed as code page 65000.
The UTF8Encoding class encodes Unicode characters using the UTF-8 encoding (UTF-8 stands for UCS Transformation Format, 8-bit form). This encoding supports all Unicode character values, and can also be accessed as code page 65001.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Cryptographic
{
public class MyCryptographic
{
//Object for AES cryptography
AesCryptoServiceProvider AESProvider = new AesCryptoServiceProvider();
public bool EncryptFile(String inputFile, String OutputFile, String keys)
{
CryptoStream cryptStream = null;
FileStream fsInput = null;
FileStream fsCiperText = null;
try
{
//File stream to read the data from the Seleted file
fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
//File stream used to write my cipertext to the selected location.
fsCiperText = new FileStream(OutputFile, FileMode.Create, FileAccess.Write);
AESProvider.Key = ASCIIEncoding.ASCII.GetBytes(keys);
AESProvider.IV = ASCIIEncoding.ASCII.GetBytes(keys);
//Create Encryption files
//create symmentric encryptor object using Key and Initialize vecor.
ICryptoTransform encrypt = AESProvider.CreateEncryptor();
//Stream make link with datastream to Cryptographic transformation
cryptStream = new CryptoStream(fsCiperText, encrypt, CryptoStreamMode.Write);
//Calculate number of bytes in the input file
byte[] byteArrayInput = new byte[fsInput.Length];
//Read block of bytes from the stream and write into given buffer
fsInput.Read(byteArrayInput, 0, byteArrayInput.Length);
//Write sequence of bytes.
cryptStream.Write(byteArrayInput, 0, byteArrayInput.Length);
//Close all the file streams
cryptStream.Close();
fsInput.Close();
fsCiperText.Close();
return true;
}
catch (Exception ex)
{
cryptStream.Close();
fsInput.Close();
fsCiperText.Close();
return false;
}
}
public bool DecryptFile(String inputFile, String outputFile, String keys)
{
FileStream fsCiperFile = null;
StreamWriter fsOutputFile = null;
try
{
//Read the cipertext from the input files
fsCiperFile = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
//Output file given after decryption
fsOutputFile = new StreamWriter(outputFile);
AESProvider.Key = ASCIIEncoding.ASCII.GetBytes(keys);
AESProvider.IV = ASCIIEncoding.ASCII.GetBytes(keys);
//Decrypt the data based on key and IV values
ICryptoTransform decrypt = AESProvider.CreateDecryptor();
CryptoStream decryptStream = new CryptoStream(fsCiperFile, decrypt, CryptoStreamMode.Read);
//Write the decrypted data into the Output files
fsOutputFile.Write(new StreamReader(decryptStream).ReadToEnd());
fsOutputFile.Flush();
fsOutputFile.Close();
return true;
}
catch (Exception ex)
{
fsCiperFile.Close();
fsOutputFile.Close();
return false;
}
}
}
}