/// <summary> /// Sets the key storage /// </summary> /// <param name="keyFile">File containing all keys for cipher operations</param> public void SetKeyFile(string keyFile) { _keyStorage = new NcaKeyStorage(keyFile); }
public NcaStream(Stream input, NcaVersion version, NcaBodySection[] sections, NcaKeyStorage keyStorage, byte[] decKeyArea, byte[] decTitleKey) { _baseStream = input; _headerStream = new NcaHeaderStream(input, version, keyStorage.HeaderKey); _sections = sections.ToList(); _sectionStreams = new Stream[sections.Length]; for (int i = 0; i < sections.Length; i++) { // In the writable stream, all cipher streams encapsulate the whole stream instead of sub streaming them var sectionIv = new byte[0x10]; if (sections[i].SectionCrypto == NcaSectionCrypto.TitleKey || sections[i].SectionCrypto == NcaSectionCrypto.Ctr) { // In case of Ctr we just set the base ctr, since with setting the stream position the counter will get updated correctly already Array.Copy(sections[i].BaseSectionCtr, sectionIv, 0x10); } else { /* sections encrypted with XTS start with sector id 0 at their respective section offset * since the cipher stream will still start at offset 0, the sector id gets decremented to a point that it will be 0, reaching its section offset * this code can be removed if XTS sections don't start at 0 but with a value representing their section offset */ sectionIv.Decrement(sections[i].MediaOffset, false); } _sectionStreams[i] = new NcaBodyStream(input, sections[i].SectionCrypto, sectionIv, decKeyArea, decTitleKey); } }