I was trying to decrypt a JSON request with a key, hex and textbody but all the codes I found are from 2013 - 2017. I tried using this one
namespace AES {
using System.IO;
using System;
using System.Security.Cryptography;
class Program {
public static byte[] FromBase64Url(string base64Url) {
// Replace '-' with '+' and '_' with '/' for standard Base64
string base64 = base64Url.Replace('-', '+').Replace('_', '/');
// Add padding if necessary. Base64 strings should have a length that is a multiple of 4.
/**/
int padding = 3 - ((base64.Length + 3) % 4);
if (padding > 0) {
base64 += new string('=', padding);
}
//*/
// Convert the Base64 string to a byte array
return Convert.FromBase64String(base64);
}
static void Main(string[] args) {
var mode = "CFB";
var message = "HgnDCsJPaosJkvCF8jqPHUdSOVtCO2GeVQWiBCV9ocDRDM2EGUzNYRXy";
byte[] encrypted;
var plaintext = "";
//if (args.Length >0) mode=args[0];
if (args.Length > 1) message = args[1];
try {
using
var aes = Aes.Create();
aes.Mode = CipherMode.CFB;
if (mode == "CBC") aes.Mode = CipherMode.CBC;
else if (mode == "CFB") aes.Mode = CipherMode.CFB;
else if (mode == "CTS") aes.Mode = CipherMode.CTS;
else if (mode == "ECB") aes.Mode = CipherMode.ECB;
else if (mode == "OFB") aes.Mode = CipherMode.OFB;
byte[] temp_iv = Convert.FromHexString("76396c566a476c4c50694e376469487a"); // Convert IV from hex to bytes
byte[] temp_key = System.Text.Encoding.UTF8.GetBytes("ei9dzmx9f3l2CdFliYMb7iwJ7d0ce58d");
byte[] temp_message = FromBase64Url(message);
aes.Key = temp_key;
aes.IV = temp_iv;
//aes.Padding = PaddingMode.PKCS7;
aes.Padding = PaddingMode.None;
ICryptoTransform encryptor = aes.CreateEncryptor();
ICryptoTransform decryptor = aes.CreateDecryptor();
using(MemoryStream msEncrypt = new MemoryStream()) {
using(CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
using(StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
swEncrypt.Write(message);
}
encrypted = msEncrypt.ToArray();
msEncrypt.Close();
csEncrypt.Close();
}
}
using(MemoryStream msDecrypt = new MemoryStream(temp_message)) {
using(CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
using(StreamReader plainTextReader = new StreamReader(csDecrypt)) {
plaintext = plainTextReader.ReadToEnd();
msDecrypt.Close();
csDecrypt.Close();
}
}
}
string str1 = "Message: " + message;
str1 = str1 + "\nIV " + BitConverter.ToString(aes.IV);
str1 = str1 + "\nKey " + BitConverter.ToString(aes.Key);
str1 = str1 + "\nMode: " + mode;
str1 = str1 + "\n\nCipher: " + BitConverter.ToString(encrypted).Replace("-", string.Empty);
str1 = str1 + "\nCipher: " + Convert.ToBase64String(encrypted);
str1 = str1 + "\nDecrypted Byte Code: " + BitConverter.ToString(temp_message);
str1 = str1 + "\nDecrypted: " + plaintext;
Console.WriteLine("{0}", str1);
} catch(Exception e) {
Console.WriteLine("Error: {0}", e);
}
}
}
}
The var message is an encrypted JSON data, I've provided the value there so anyone can run it themselves. It is a Base64String.
This returned Padding is Invalid Error when I didn't set the padding and also returned gibberish when I did. I tried all the encodings and all the Algorithms to decrypt (It was encoded using CFB but just in case I covered all bases)

System.Security.Cryptographywithout incident.using (MemoryStream msDecrypt = new MemoryStream(temp_message))should beusing (MemoryStream msDecrypt = new MemoryStream(encrypted)). Make that change and everything works. I am voting to close as a typoHgnDCs...was generated), but not correct, so that a workaround is required. CFB is a stream cipher mode that does not need padding if implemented correctly. However, the MS implementation requires padding to a multiple of the segment size, which for CFB128 is a full block. As a consequence, the ciphertext must be shortened accordingly after encryption. An alternative that works as intended is C#/BouncyCastle.message = Hgn...) appears to be encrypted a second time (encrypted), and in the second part the ciphertext seems to be decrypted (plaintext). It is not clear to me what the first part is supposed to do. Perhaps you are looking for a way to generate the ciphertextmessage = Hgn...?