This is a mirror of official site: http://jasper-net.blogspot.com/

AES Encryption Sample in C# (CSharp)

| Wednesday, May 11, 2011
Recently I have written a post on Encryption and compression in Java. Now i had similar requirement to do in CSharp( I work on Distrubuted systems which are in Different Technology Stack). Here is the sample which does AES Encryption of files/text etc using a key
using System;
using System.IO;
using System.Security.Cryptography; 

//
// Sample encrypt/decrypt functions
// Parameter checks and error handling
// are ommited for better readability
// @author:Ashwin Kumar
// Date : 12/3/2008
public class AESEncryptionUtility
{
static int  Main(string[] args)
    {
if (args.Length < 2)
            {
                Console.WriteLine("Usage: AESEncryptionUtility infile outFile");
                return 1;
            }
string infile = args[0];
            string outfile = args[1];
//string keyfile = args[2];
//var key = File.ReadAllBytes(keyfile);
Encrypt(infile,outfile,"test");
Decrypt(outfile,"_decrypted"+infile,"test");
return 0;

}

    // Encrypt a byte array into a byte array using a key and an IV
    public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
    {
        // Create a MemoryStream to accept the encrypted bytes
        MemoryStream ms = new MemoryStream();
        // Create a symmetric algorithm.
        // We are going to use Rijndael because it is strong and
        // available on all platforms.
        // You can use other algorithms, to do so substitute the
        // next line with something like
        //      TripleDES alg = TripleDES.Create();
        Rijndael alg = Rijndael.Create();

Posted via email from Jasper-net

0 comments: