Вручную дешифруя контент конвертC#

Место общения программистов C#
Anonymous
Вручную дешифруя контент конверт

Сообщение Anonymous »

I have a situation where I have an envelopeCms object on disk.
It contains:
  • The generated AES-256 private-key encrypted using a public key
  • The actual payload encrypted with the generated AES-256 private-key
Now, I Имейте со мной частный ключ AES-256. Я хочу использовать это, чтобы вручную расшифровать зашифрованные данные. 16 байтов фактического зашифрованного контента.
На основании вышеуказанного понимания у меня есть этот код (dotnet8), не используя Bouncycastle или каких -либо сторонних библиотек, только Microsoft System.security.cryptography.pkcs nuget: < /p>
using System.Security.Cryptography;
using System.Text;
using System.Security.Cryptography.Pkcs;

static void Main(string[] args)
{
string inputFile = "D:\\temp\\test.txt.enc"; // my envelopedCmd file created by another utility
byte[] baEncryptedEnvelopedMessage = File.ReadAllBytes(inputFile);
DecodeDirect(baEncryptedEnvelopedMessage);
}

public static byte[] DecodeDirect(byte[] encryptedMessage)
{
string aesEncrytionKeyFile = "D:\\temp\\decryptionkey.bin";
EnvelopedCms envelopedCms = new EnvelopedCms();
envelopedCms.Decode(encryptedMessage);

byte[] aesKey;
byte[] baEncKey = envelopedCms.RecipientInfos[0].EncryptedKey;

aesKey = File.ReadAllBytes(aesEncrytionKeyFile);

byte[] iv;
byte[] baEncContentOnly;
byte[] baEncContentPlusIV = envelopedCms.ContentInfo.Content;

ExtractEncryptedContentAndIV(baEncContentPlusIV, aesKey, out iv, out baEncContentOnly);

byte[] content = DecryptAes(baEncContentOnly, aesKey, iv); // this is not matching the original unecrypted bytes. This one has length: 415 whereas original content has length: 431

string decryptedTextContent = Encoding.Default.GetString(content); // for debugging, this is not matching

return content;
}

private static void ExtractEncryptedContentAndIV(byte[] encryptedContent, byte[] aesKey, out byte[] iv, out byte[] actualCipherText)
{
int ivLength = 16;
iv = new byte[ivLength];
Buffer.BlockCopy(encryptedContent, 0, iv, 0, ivLength);

actualCipherText = new byte[encryptedContent.Length - ivLength];
Buffer.BlockCopy(encryptedContent, ivLength, actualCipherText, 0, actualCipherText.Length);
}

private static byte[] DecryptAes(byte[] cipherText, byte[] key, byte[] iv)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;

using (ICryptoTransform decryptor = aesAlg.CreateDecryptor())
{
return decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
}
}
}
< /code>
Результаты:
Я на самом деле могу увидеть «большинство» из расшифрованного контента (строка), имея правильные ценности. hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world"
The decrypted content:
"o world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world"
I have tried with other strings like:
Original:
"ABCDEFGHIJK hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world LMNOPQRSTUV"
Decrypted:
"o world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world Lmnopqrstuv "< /p>
После попытки нескольких различных контента, похоже, что первые 16 байтов всегда отсутствуют. Незатратные и расшифрованные строки идеально соответствуют. Мне нужна помощь!

Подробнее здесь: https://stackoverflow.com/questions/794 ... ms-content

Вернуться в «C#»