It contains:
- The generated AES-256 private-key encrypted using a public key
- The actual payload encrypted with the generated AES-256 private-key
На основании вышеуказанного понимания у меня есть этот код (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 байтов всегда отсутствуют. Незатратные и расшифрованные строки идеально соответствуют. Мне нужна кто -то помощь! < /P>
Код, используемый для создания EncryptedCM, если кто -то думает, что это поможет решить проблему: < /p>
static void main()
{
byte[] content = File.ReadAllBytes(inputFile);
byte[] encodedContent = new PKCSUtil().Encode(content, certFile, certPass);
File.WriteAllBytes(outputFile, encodedContent);
Console.WriteLine("Encoded content written successfully.");
}
static void byte[] Encode(byte[] content, string publicKeyCertFile, string? password = null)
{
X509Certificate2 recipientCert = new X509Certificate2(publicKeyCertFile, password);
return Encode(recipientCert, content);
}
public byte[] Encode(X509Certificate2 certificate, byte[] content)
{
var envelopedCms = new EnvelopedCms(new ContentInfo(content));
var recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, certificate, RSAEncryptionPadding.OaepSHA1);
envelopedCms.Encrypt(recipient);
return envelopedCms.Encode();
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... ms-content