Skip to content

Instantly share code, notes, and snippets.

@l4km47
Last active November 29, 2019 05:04
Show Gist options
  • Save l4km47/105395c9c5dc2c20335d7bffdf2c6e3c to your computer and use it in GitHub Desktop.
Save l4km47/105395c9c5dc2c20335d7bffdf2c6e3c to your computer and use it in GitHub Desktop.
AES-Encryptions
using System;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.IO;
namespace CRYPTO
{
public class CRYPT
{
private protected string KEY = "1234567890qwerty"; //16bit encryption key
/// <summary>
/// ENCRYPTION
/// </summary>
/// <param name="data"></param>
/// <returns>ENCRYPTED DATA</returns>
public string ENC(string data)
{
return EncryptData(data);
}
/// <summary>
/// DECRYPTION
/// </summary>
/// <param name="data"></param>
/// <returns>Decrypted data</returns>
public string DEC(string data)
{
return DecryptData(data);
}
public CRYPT()
{
//
}
private RijndaelManaged InitilizeRij()
{
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
RMCrypto.Padding = PaddingMode.PKCS7;
RMCrypto.KeySize = 0x80;
RMCrypto.BlockSize = 0x80;
return RMCrypto;
}
///<summary>
/// manoj Lakmal - 11/30/2019.
///
/// Encrypts a file using Rijndael algorithm.
///</summary>
///<param name="inputFile"></param>
///<param name="outputFile"></param>
public void EncryptFile(string inputFile, string outputFile)
{
try
{
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = InitilizeRij();
byte[] passBytes = Encoding.UTF8.GetBytes(KEY);
byte[] EncryptionkeyBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int len = passBytes.Length;
if (len > EncryptionkeyBytes.Length)
{
len = EncryptionkeyBytes.Length;
}
Array.Copy(passBytes, EncryptionkeyBytes, len);
RMCrypto.Key = EncryptionkeyBytes;
RMCrypto.IV = EncryptionkeyBytes; CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
File.Delete(inputFile);
}
catch (Exception ex)
{
/// MessageBox.Show("Encryption failed!", "Error");
}
}
///<summary>
/// manoj Lakmal - 11/30/2019.
///
/// Decrypts a file using Rijndael algorithm.
///</summary>
///<param name="inputFile"></param>
///<param name="outputFile"></param>
public void DecryptFile(string inputFile, string outputFile)
{
try
{
string password = KEY; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = InitilizeRij();
byte[] passBytes = Encoding.UTF8.GetBytes(KEY);
byte[] EncryptionkeyBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int len = passBytes.Length;
if (len > EncryptionkeyBytes.Length)
{
len = EncryptionkeyBytes.Length;
}
Array.Copy(passBytes, EncryptionkeyBytes, len);
RMCrypto.Key = EncryptionkeyBytes;
RMCrypto.IV = EncryptionkeyBytes;
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(),
CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
// StreamReader streamReader = new StreamReader(cs);
//Save as original file
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
// string s = streamReader.ReadToEnd();
cs.Close();
fsCrypt.Close();
}
catch (Exception ex)
{
/// MessageBox.Show("Encryption failed!", "Error");
}
}
///<summary>
/// manoj Lakmal - 11/30/2019.
///
/// Encrypt string using Rijndael algorithm.
///</summary>
///<param name="textData"></param>
private string EncryptData(string textData)
{
try
{
RijndaelManaged objrij = InitilizeRij();
byte[] passBytes = Encoding.UTF8.GetBytes(KEY);
byte[] EncryptionkeyBytes = new byte[0x10];
int len = passBytes.Length;
if (len > EncryptionkeyBytes.Length)
{
len = EncryptionkeyBytes.Length;
}
Array.Copy(passBytes, EncryptionkeyBytes, len);
objrij.Key = EncryptionkeyBytes;
objrij.IV = EncryptionkeyBytes;
ICryptoTransform objtransform = objrij.CreateEncryptor();
byte[] textDataByte = Encoding.UTF8.GetBytes(textData);
return Convert.ToBase64String(objtransform.TransformFinalBlock(textDataByte, 0, textDataByte.Length));
}
catch (Exception ex)
{
return "";
}
}
///<summary>
/// manoj Lakmal - 11/30/2019.
///
/// Decrypt string using Rijndael algorithm.
///</summary>
///<param name="textData"></param>
private string DecryptData(string EncryptedText)
{
try
{
RijndaelManaged objrij = InitilizeRij();
byte[] encryptedTextByte = Convert.FromBase64String(EncryptedText);
byte[] passBytes = Encoding.UTF8.GetBytes(KEY);
byte[] EncryptionkeyBytes = new byte[0x10];
int len = passBytes.Length;
if (len > EncryptionkeyBytes.Length)
{
len = EncryptionkeyBytes.Length;
}
Array.Copy(passBytes, EncryptionkeyBytes, len);
objrij.Key = EncryptionkeyBytes;
objrij.IV = EncryptionkeyBytes;
byte[] TextByte = objrij.CreateDecryptor().TransformFinalBlock(encryptedTextByte, 0, encryptedTextByte.Length);
return Encoding.UTF8.GetString(TextByte);
}
catch (Exception ex)
{
return "";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment