Skip to content

Instantly share code, notes, and snippets.

@bitsydarel
Created December 17, 2017 16:48
Show Gist options
  • Save bitsydarel/0858fd8ad2cd2e945bfb04346104a554 to your computer and use it in GitHub Desktop.
Save bitsydarel/0858fd8ad2cd2e945bfb04346104a554 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ZS_Workscript_Data
{
public static class SoftwareHelper
{
public static string DecryptWithAES(string toDecrypt, string password)
{
var valueToDecrypt = Convert.FromBase64String(toDecrypt);
var SALFT_BYTES = Encoding.ASCII.GetBytes("from_darel_bitsy_to_zoomsupport");
var passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
using (var memoryStream = new MemoryStream())
{
using (var AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var encryptionKey = new Rfc2898DeriveBytes(passwordBytes, SALFT_BYTES, 1000);
AES.Key = encryptionKey.GetBytes(AES.KeySize / 8);
AES.IV = encryptionKey.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var encryptionAlgo = new CryptoStream(memoryStream, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
encryptionAlgo.Write(valueToDecrypt, 0, valueToDecrypt.Length);
encryptionAlgo.Close();
}
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
}
internal static string EncryptWithAES(string toEncrypt, string password)
{
var toEncryptInByes = Encoding.UTF8.GetBytes(toEncrypt);
var SALFT_BYTES = Encoding.ASCII.GetBytes("from_darel_bitsy_to_zoomsupport");
var passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
using (var memoryStream = new MemoryStream())
{
using (var AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var encryptionKey = new Rfc2898DeriveBytes(passwordBytes, SALFT_BYTES, 1000);
AES.Key = encryptionKey.GetBytes(AES.KeySize / 8);
AES.IV = encryptionKey.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var encryptionAlgo = new CryptoStream(memoryStream, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
encryptionAlgo.Write(toEncryptInByes, 0, toEncryptInByes.Length);
encryptionAlgo.Close();
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment