public static MemoryStream DecryptFile(string dumpFilePath, RSACryptoServiceProvider rsaKey) { MemoryStream outFs = new MemoryStream(); using (AesManaged aesManaged = new AesManaged()) { aesManaged.KeySize = 256; aesManaged.BlockSize = 128; aesManaged.Mode = CipherMode.CBC; byte[] LenK = new byte[4]; byte[] LenIV = new byte[4]; using (FileStream inFs = new FileStream(dumpFilePath, FileMode.Open)) { inFs.Seek(0, SeekOrigin.Begin); inFs.Seek(0, SeekOrigin.Begin); inFs.Read(LenK, 0, 3); inFs.Seek(4, SeekOrigin.Begin); inFs.Read(LenIV, 0, 3); int lenK = BitConverter.ToInt32(LenK, 0); int lenIV = BitConverter.ToInt32(LenIV, 0); int startC = lenK + lenIV + 8; int lenC = (int)inFs.Length - startC; byte[] KeyEncrypted = new byte[lenK]; byte[] IV = new byte[lenIV]; inFs.Seek(8, SeekOrigin.Begin); inFs.Read(KeyEncrypted, 0, lenK); inFs.Seek(8 + lenK, SeekOrigin.Begin); inFs.Read(IV, 0, lenIV); byte[] KeyDecrypted = rsaKey.Decrypt(KeyEncrypted, false); using (ICryptoTransform transform = aesManaged.CreateDecryptor(KeyDecrypted, IV)) { int count = 0; int offset = 0; int blockSizeBytes = aesManaged.BlockSize / 8; byte[] data = new byte[blockSizeBytes]; inFs.Seek(startC, SeekOrigin.Begin); using (CryptoStream outStreamDecrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write)) { do { count = inFs.Read(data, 0, blockSizeBytes); offset += count; outStreamDecrypted.Write(data, 0, count); } while (count > 0); outStreamDecrypted.FlushFinalBlock(); } } } } return outFs; }
c# Расшифровываем файл, зашифрованный ключом из сертификата x509 / x509 Certificate Decrypt file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.