Skip to content

Instantly share code, notes, and snippets.

View muhaisen's full-sized avatar

Aidan Muhaisen muhaisen

View GitHub Profile
@muhaisen
muhaisen / ManagedAESDecryptor.cs
Created March 13, 2021 11:10
Decrypts Salesforce encryptWithManagedIV using C#
using Newtonsoft.Json;
using System;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
namespace ManagedAES
{
/// <summary>
@muhaisen
muhaisen / Decrypt.cs
Created March 13, 2021 10:28
Tests decrypting Salesforce encryptWithManagedIV
public static void Decrypt()
{
using (AesManaged myAes = new AesManaged())
{
var protected_string = "W+YnphJ2T8pH4X+FNTwZvFPab+R4wRbZg/6HuyZ8Ese15LYp1UJLs7HvmIco4w6A";
byte[] protected_bytes = RetrieveCipher(protected_string);
// Encryption key
var encrption_key = Convert.FromBase64String(Constants.s_encrption_key);
// IV
var byte_IV = RetrieveIv(protected_string);
@muhaisen
muhaisen / DecryptStringFromBytes_Aes.cs
Created March 13, 2021 10:19
Decrypts cipher text Aes
/// <summary>
/// Decrypts the cipher text.
/// </summary>
/// <param name="cipherText"></param>
/// <param name="Key"></param>
/// <param name="IV"></param>
/// <returns></returns>
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
@muhaisen
muhaisen / RetrieveCipher.cs
Last active March 13, 2021 11:03
Gets the cipher text from Salesforce encryptWithManagedIV
/// <summary>
/// Retivers the cipher text without the IV
/// </summary>
/// <param name="base64_cipher_text"></param>
/// <returns></returns>
public static byte[] RetrieveCipher(string base64_cipher_text)
{
byte[] cipher_binary = Convert.FromBase64String(base64_cipher_text);
var length = cipher_binary.Length - 16;
@muhaisen
muhaisen / RetrieveIv.cs
Created March 13, 2021 10:17
Gets the IV from Salesforce encryptWithManagedIV
/// <summary>
/// Returns the IV needed for decryption. 16 Bytes
/// </summary>
/// <param name="base64_cipher_text"></param>
/// <returns></returns>
public static byte[] RetrieveIv(string base64_cipher_text)
{
// Just 16 the first bytes
// 16 not divisable by 3 (4 characters per 3 bytes)
// 4 -> 3