Created
November 7, 2015 07:26
-
-
Save money4honey/12e0d1a457dbe02ce3cd to your computer and use it in GitHub Desktop.
This file contains 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.IO; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace myClasses | |
{ | |
public class EncryptMaster | |
{ | |
private static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) | |
{ | |
try | |
{ | |
byte[] encryptedBytes = null; | |
// Set your salt here, change it to meet your flavor: | |
// The salt bytes must be at least 8 bytes. | |
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
using (RijndaelManaged AES = new RijndaelManaged()) | |
{ | |
AES.KeySize = 256; | |
AES.BlockSize = 128; | |
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); | |
AES.Key = key.GetBytes(AES.KeySize / 8); | |
AES.IV = key.GetBytes(AES.BlockSize / 8); | |
AES.Mode = CipherMode.CBC; | |
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)) | |
{ | |
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); | |
cs.Close(); | |
} | |
encryptedBytes = ms.ToArray(); | |
} | |
} | |
return encryptedBytes; | |
} | |
catch { return null; } | |
} | |
private static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) | |
{ | |
try | |
{ | |
byte[] decryptedBytes = null; | |
// Set your salt here, change it to meet your flavor: | |
// The salt bytes must be at least 8 bytes. | |
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
using (RijndaelManaged AES = new RijndaelManaged()) | |
{ | |
AES.KeySize = 256; | |
AES.BlockSize = 128; | |
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); | |
AES.Key = key.GetBytes(AES.KeySize / 8); | |
AES.IV = key.GetBytes(AES.BlockSize / 8); | |
AES.Mode = CipherMode.CBC; | |
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) | |
{ | |
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); | |
cs.Close(); | |
} | |
decryptedBytes = ms.ToArray(); | |
} | |
} | |
return decryptedBytes; | |
} | |
catch { return null; } | |
} | |
public static void EncryptFile(string oldPath, string newPath, string pass) | |
{ | |
try | |
{ | |
int sizeLimitMB = 5; | |
FileInfo f = new FileInfo(oldPath); | |
long fileSize = f.Length; | |
if (fileSize < (sizeLimitMB * 1000000)) | |
{ | |
string file = oldPath; | |
string password = pass; | |
byte[] bytesToBeEncrypted = File.ReadAllBytes(file); | |
byte[] passwordBytes = Encoding.UTF8.GetBytes(password); | |
// Hash the password with SHA256 | |
passwordBytes = SHA256.Create().ComputeHash(passwordBytes); | |
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes); | |
if (bytesEncrypted != null) | |
{ | |
string fileEncrypted = newPath; | |
File.WriteAllBytes(fileEncrypted, bytesEncrypted); | |
} | |
} | |
} | |
catch { } | |
} | |
public static void DecryptFile(string oldPath, string newPath, string pass) | |
{ | |
try | |
{ | |
int sizeLimitMB = 5; | |
FileInfo f = new FileInfo(oldPath); | |
long fileSize = f.Length; | |
if (fileSize < (sizeLimitMB * 1000000)) | |
{ | |
string fileEncrypted = oldPath; | |
string password = pass; | |
byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted); | |
byte[] passwordBytes = Encoding.UTF8.GetBytes(password); | |
passwordBytes = SHA256.Create().ComputeHash(passwordBytes); | |
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes); | |
if (bytesDecrypted != null) | |
{ | |
string file = newPath; | |
File.WriteAllBytes(file, bytesDecrypted); | |
} | |
} | |
} | |
catch { } | |
} | |
public static string EncryptText(string input, string password) | |
{ | |
try | |
{ | |
// Get the bytes of the string | |
byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input); | |
byte[] passwordBytes = Encoding.UTF8.GetBytes(password); | |
// Hash the password with SHA256 | |
passwordBytes = SHA256.Create().ComputeHash(passwordBytes); | |
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes); | |
string result = Convert.ToBase64String(bytesEncrypted); | |
return result; | |
} | |
catch { return null; } | |
} | |
public static string DecryptText(string input, string password) | |
{ | |
try | |
{ | |
// Get the bytes of the string | |
byte[] bytesToBeDecrypted = Convert.FromBase64String(input); | |
byte[] passwordBytes = Encoding.UTF8.GetBytes(password); | |
passwordBytes = SHA256.Create().ComputeHash(passwordBytes); | |
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes); | |
string result = Encoding.UTF8.GetString(bytesDecrypted); | |
return result; | |
} | |
catch { return null; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment