{"record":{"id":"39d78dfcf349366a","repo":"dotnet/wpf","slug":"sr-encrypteddatastreamcorrupt","errorCode":null,"errorMessage":"SR.EncryptedDataStreamCorrupt","messagePattern":"SR\\.EncryptedDataStreamCorrupt","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/RightsManagementEncryptedStream.cs","lineNumber":332,"sourceCode":"                // seek to the beginning of the stream \n                _baseStream.Seek(0, SeekOrigin.Begin);\n\n                // read the size prefix \n                byte[] prefixData = new byte[_prefixLengthSize];\n                int bytesRead = PackagingUtilities.ReliableRead\n                                            (_baseStream, prefixData, 0, prefixData.Length);\n\n                // decode length data (from the prefix)\n                if (bytesRead == 0)\n                {\n                    // probably a new stream - just assume length is zero\n                    _streamOnDiskLength = 0;\n                }\n                else\n                    if (bytesRead < _prefixLengthSize)\n                    {\n                        // not zero and shorter than legal length == corrupt file\n                        throw new FileFormatException(SR.EncryptedDataStreamCorrupt);\n                    }\n                    else\n                    {\n                        checked\n                        {\n                            // This will throw on a negative value so we need not\n                            // explicitly check for that\n                            _streamOnDiskLength = (long)BitConverter.ToUInt64(prefixData, 0);\n                        }\n                    }\n                _streamCachedLength = _streamOnDiskLength;\n            }\n        }\n\n        private int InternalRead(long streamPosition, byte[] buffer, int offset, int count)\n        {\n            // use the explicitly passed in Position or reading in the stream \n            // we do not want to rely and change the real stream position ","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/RightsManagementEncryptedStream.cs#L314-L350","documentation":"ParseStreamLength reads a length prefix (the encrypted-stream length stored at the head of the on-disk data) from the base stream. If a nonzero prefix is present but shorter than the legal prefix size (_prefixLengthSize), the file's encrypted data stream is malformed, and a FileFormatException with SR.EncryptedDataStreamCorrupt is thrown. This is a data-corruption signal, not an argument error.","triggerScenarios":"Opening a rights-managed compound file whose stream-length prefix was truncated or overwritten (partial write, bad sector, faulty tool edit) so that bytesRead > 0 but bytesRead < _prefixLengthSize.","commonSituations":"Files truncated by interrupted downloads or copy operations; documents damaged by disk errors; hand-edited or incorrectly re-saved compound files; content migrated across tools that mangled the package header.","solutions":["Restore the file from a known-good backup or re-acquire/re-download the document — a corrupt prefix cannot be repaired in place.","Verify file integrity (compare size/hash against the source) to confirm the truncation.","Re-create the rights-managed document by republishing the content with RM protection from the original material.","Ensure storage/transfer completes atomically (write to temp then move) to prevent partial writes in the future.","Catch FileFormatException when opening untrusted documents and degrade gracefully with a 'file is corrupt' user message."],"exampleFix":"// before\nvar stream = new RightsManagementEncryptedStream(baseStream, cryptoProvider);\n// after\ntry\n{\n    var stream = new RightsManagementEncryptedStream(baseStream, cryptoProvider);\n}\ncatch (FileFormatException)\n{\n    Console.WriteLine(\"The rights-managed document's data stream is corrupt; restore from backup.\");\n}","handlingStrategy":"try-catch","validationCode":"// Sanity-check file size before opening\nvar fi = new FileInfo(path);\nif (fi.Length < RightsManagementEncryptedStreamMinHeaderSize)\n    throw new FileFormatException(\"File too small to contain a valid encrypted stream header.\");","typeGuard":"bool PlausibleEncryptedStreamLength(FileInfo fi, int prefixLengthSize)\n    => fi.Exists && fi.Length >= prefixLengthSize;","tryCatchPattern":"try { stream = new RightsManagementEncryptedStream(baseStream, cryptoProvider); }\ncatch (FileFormatException)\n{ /* mark document as corrupt, offer restore/re-download */ }","preventionTips":["Verify file size/hash after download or copy before opening.","Write large files atomically (temp file + rename) to avoid truncated headers.","Catch FileFormatException at the document-open boundary and surface a recoverable UX path.","Back up rights-managed documents; corrupt prefixes are not repairable in place."],"tags":["fileformatexception","corrupt-file","rights-management","data-integrity"],"backgroundTag":"schema-validation-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}