Created
December 30, 2013 06:28
-
-
Save yssharma/8178524 to your computer and use it in GitHub Desktop.
C# encryption/ decryption code for Rijndael implementation. Cross platform compatible with the java code provided here:
https://gist.github.com/yashs360/8178448
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Data; | |
| using System.Security; | |
| using System.Security.Cryptography; | |
| using System.Runtime.InteropServices; | |
| using System.Text.RegularExpressions; | |
| using System.Text; | |
| using System.IO; | |
| namespace Rijndael.csproj | |
| { | |
| public partial class ScriptMain | |
| { | |
| public static void Main() | |
| { | |
| // Secret key must be same as the java code for csoss platform compatibility. | |
| // Secret ket must be 16/24/32 char(byte) long to support AES Java code | |
| EncryptFile(@"C:\path\to\file\data.csv", @"C:\path\to\file\data_encrypted.csv",@"SecretKey1234567"); | |
| DecryptFile(@"C:\path\to\file\data.csv", @"C:\path\to\file\data_decrypted.csv", @"SecretKey1234567"); | |
| } | |
| private static void EncryptFile(string inputFile, string outputFile, string skey) | |
| { | |
| try | |
| { | |
| using (RijndaelManaged aes = new RijndaelManaged()) | |
| { | |
| byte[] key = Encoding.UTF8.GetBytes(skey); | |
| byte[] IV = Encoding.UTF8.GetBytes(skey); | |
| aes.Padding = PaddingMode.PKCS7; | |
| aes.Mode = CipherMode.CBC; | |
| aes.KeySize = 128; | |
| aes.BlockSize = 128; | |
| using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create)) | |
| { | |
| using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV)) | |
| { | |
| using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write)) | |
| { | |
| using (FileStream fsIn = new FileStream(inputFile, FileMode.Open)) | |
| { | |
| int data; | |
| while ((data = fsIn.ReadByte()) != -1) | |
| { | |
| cs.WriteByte((byte)data); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| catch (Exception ex) { } | |
| } | |
| private static void DecryptFile(string inputFile, string outputFile, string skey) | |
| { | |
| try | |
| { | |
| using (RijndaelManaged aes = new RijndaelManaged()) | |
| { | |
| byte[] key = Encoding.UTF8.GetBytes(skey); | |
| byte[] IV = Encoding.UTF8.GetBytes(skey); | |
| aes.Padding = PaddingMode.PKCS7; | |
| aes.Mode = CipherMode.CBC; | |
| aes.KeySize = 128; | |
| aes.BlockSize = 128; | |
| using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open)) | |
| { | |
| using (FileStream fsOut = new FileStream(outputFile, FileMode.Create)) | |
| { | |
| using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV)) | |
| { | |
| using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read)) | |
| { | |
| int data; while ((data = cs.ReadByte()) != -1) | |
| { fsOut.WriteByte((byte)data); } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| catch (Exception ex) { } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment