public Stream GetReadStream() { Stream stream = null; try { stream = new FileStream(this.ArchiveFilePath, FileMode.Open, FileAccess.Read); if (this.Flags.IsMemoryResident) { stream = new SubStream(stream, this.Position, this.CompressedSize); if (this.Flags.IsEncrypted) { stream = new EncryptionStream(stream, StreamAccessMode.Read); } if (this.Flags.IsCompressed) { stream = new ZlibStream(stream, System.IO.Compression.CompressionMode.Decompress); } using (var tempReader = new BinaryReader(stream)) { stream = new MemoryStream(tempReader.ReadBytes((int)this.UncompressedSize)); } } return(stream); } catch { if (stream != null) { stream.Dispose(); } throw; } }
public Stream GetReadStream() { Stream stream = null; try { stream = this.BlockContentsSource.GetReadStream(); stream = new SubStream(stream, this.PositionInBlock, this.CompressedSize); if (!this.BlockContentsSource.Flags.IsMemoryResident) { if (this.BlockContentsSource.Flags.IsEncrypted) { stream = new EncryptionStream(stream, StreamAccessMode.Read); } if (this.BlockContentsSource.Flags.IsCompressed) { stream = new ZlibStream(stream, System.IO.Compression.CompressionMode.Decompress); } } return(stream); } catch { if (stream != null) { stream.Dispose(); } throw; } }
// byte[] 1 2 5 10 29 // 把原本的字节数组,和密钥进行异或,变成新的字节数组 // 0001 ^ 0110 1111 0111 1010 1000 0001 1111 0011 = 0110 1111 0111 1010 1000 0001 1111 001[0] ->这里有个1变了 public static void EcryptAssetBundle(string abPath) { byte[] EncryKey = EncryptionStream.UInt32ToByte4(encryKey); byte[] bytes = File.ReadAllBytes(abPath); for (int i = 0; i < bytes.Length; ++i) { bytes[i] ^= EncryKey[i % 4];//异或,不同就是1 0^0=0 1^0=1 0^1=1 1^1=0 } File.WriteAllBytes(abPath, bytes); }
public void TestReadMoreBytesThanAvailable() { using (var encryptionStream = new EncryptionStream(TestData.GetStream(this.origBytes), AnnoRDA.IO.StreamAccessMode.Read)) { var actualCipher = new byte[this.origBytes.Length + 3]; Assert.Equal(this.origBytes.Length, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); Assert.Equal(this.expectedCipher, actualCipher.Take(this.origBytes.Length).ToArray()); Assert.Equal(0, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); // EOF } }
public void TestReadMode() { using (var encryptionStream = new EncryptionStream(TestData.GetStream(this.origBytes), AnnoRDA.IO.StreamAccessMode.Read)) { Assert.False(encryptionStream.CanWrite); Assert.True(encryptionStream.CanRead); var buffer = new byte[this.origBytes.Length]; Assert.Throws <NotSupportedException>(() => encryptionStream.Write(buffer, 0, buffer.Length)); encryptionStream.Read(buffer, 0, buffer.Length); } }
public void TestWriteEvenNumberOfBytesAllAtOnce() { Assert.Equal(0, this.origBytes.Length % 2); using (var baseStream = TestData.GetStream(new byte[this.origBytes.Length])) { using (var encryptionStream = new EncryptionStream(baseStream, AnnoRDA.IO.StreamAccessMode.Write, leaveOpen: true)) { encryptionStream.Write(this.origBytes, 0, this.origBytes.Length); } AssertStreamContentsEqual(this.expectedCipher, baseStream); } }
public void TestReadWithAFewRealBytes() { this.origBytes = new byte[] { 0xD6, 0x63, 0x90, 0x19, 0x55, 0x39, 0x96, 0x23 }; this.expectedCipher = new byte[] { 0x64, 0x00, 0x61, 0x00, 0x74, 0x00, 0x61, 0x00 }; using (var encryptionStream = new EncryptionStream(TestData.GetStream(this.origBytes), AnnoRDA.IO.StreamAccessMode.Read)) { var actualCipher = new byte[this.origBytes.Length]; encryptionStream.Read(actualCipher, 0, actualCipher.Length); Assert.Equal(this.expectedCipher, actualCipher); } }
public void TestReadEvenNumberOfBytesAllAtOnce() { Assert.Equal(0, this.origBytes.Length % 2); using (var encryptionStream = new EncryptionStream(TestData.GetStream(this.origBytes), AnnoRDA.IO.StreamAccessMode.Read)) { var actualCipher = new byte[this.origBytes.Length]; Assert.Equal(actualCipher.Length, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); Assert.Equal(this.expectedCipher, actualCipher); Assert.Equal(0, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); // EOF } }
public void TestReadOneByOne() { using (var encryptionStream = new EncryptionStream(TestData.GetStream(this.origBytes), AnnoRDA.IO.StreamAccessMode.Read)) { var actualCipher = new byte[1]; foreach (var expectedByte in this.expectedCipher) { Assert.Equal(1, encryptionStream.Read(actualCipher, 0, 1)); Assert.Equal(new byte[] { expectedByte }, actualCipher); } Assert.Equal(0, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); // EOF } }
public void TestWriteOneByOne() { using (var baseStream = TestData.GetStream(new byte[this.origBytes.Length])) { using (var encryptionStream = new EncryptionStream(baseStream, AnnoRDA.IO.StreamAccessMode.Write, leaveOpen: true)) { foreach (var origByte in this.origBytes) { var buffer = new byte[] { origByte }; encryptionStream.Write(buffer, 0, 1); } } AssertStreamContentsEqual(this.expectedCipher, baseStream); } }
public void TestWriteEvenNumberOfBytesPartially() { Assert.True(this.origBytes.Length > 4); Assert.Equal(0, this.origBytes.Length % 2); using (var baseStream = TestData.GetStream(new byte[this.origBytes.Length])) { using (var encryptionStream = new EncryptionStream(baseStream, AnnoRDA.IO.StreamAccessMode.Write, leaveOpen: true)) { encryptionStream.Write(this.origBytes.Take(this.origBytes.Length - 4).ToArray(), 0, this.origBytes.Length - 4); encryptionStream.Flush(); AssertStreamContentsEqual(this.expectedCipher.Take(this.origBytes.Length - 4).ToArray(), baseStream); encryptionStream.Write(this.origBytes.Skip(this.origBytes.Length - 4).ToArray(), 0, 4); } AssertStreamContentsEqual(this.expectedCipher, baseStream); } }
public static void DecryptAssetBundle(string abPath, string name, Action <UnityEngine.Object> onLoadedCall) { var fileStream = new EncryptionStream(abPath, FileMode.Open, FileAccess.Read, encryKey); //var fileStream = new FileStream( abPath, FileMode.Open, FileAccess.Read ); var myLoadedAssetBundle = AssetBundle.LoadFromStream(fileStream); var obj = myLoadedAssetBundle.LoadAsset <GameObject>(name); try { onLoadedCall(obj); } catch (System.Exception e) { Debug.LogError(e.Message); } myLoadedAssetBundle.Unload(false); fileStream.Close(); }
public void TestReadOddNumberOfBytesPartially() { Assert.True(this.origBytes.Length > 3); Assert.Equal(0, this.origBytes.Length % 2); using (var encryptionStream = new EncryptionStream(TestData.GetStream(this.origBytes), AnnoRDA.IO.StreamAccessMode.Read)) { var actualCipher = new byte[this.origBytes.Length - 3]; Assert.Equal(actualCipher.Length, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); var expectedPartial = this.expectedCipher.Take(this.origBytes.Length - 3).ToArray(); Assert.Equal(expectedPartial, actualCipher); actualCipher = new byte[3]; Assert.Equal(actualCipher.Length, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); expectedPartial = this.expectedCipher.Skip(this.origBytes.Length - 3).ToArray(); Assert.Equal(expectedPartial, actualCipher); Assert.Equal(0, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); // EOF } }
public void TestWriteOddNumberOfBytesAllAtOnce() { Assert.Equal(0, this.origBytes.Length % 2); Assert.True(this.origBytes.Length > 2); // drop last byte to get odd number of bytes this.origBytes = this.origBytes.Take(this.origBytes.Length - 1).ToArray(); this.expectedCipher = this.expectedCipher.Take(this.expectedCipher.Length - 1).ToArray(); // now the last byte should not be encrypted this.expectedCipher[this.expectedCipher.Length - 1] = this.origBytes[this.origBytes.Length - 1]; Assert.Equal(1, this.origBytes.Length % 2); using (var baseStream = TestData.GetStream(new byte[this.origBytes.Length])) { using (var encryptionStream = new EncryptionStream(baseStream, AnnoRDA.IO.StreamAccessMode.Write, leaveOpen: true)) { encryptionStream.Write(this.origBytes, 0, this.origBytes.Length); } AssertStreamContentsEqual(this.expectedCipher, baseStream); } }
public BinaryReader GetReaderForPotentiallyEncryptedBlock(Context context, BinaryReader currentReader, BlockHeader block) { if (block.IsEncrypted) { Stream wrappedStream = null; try { wrappedStream = new SubStream(currentReader.BaseStream, currentReader.BaseStream.Position, block.CompressedFileHeadersSize, leaveOpen: currentReader == context.Reader.BaseReader); wrappedStream = new EncryptionStream(wrappedStream, StreamAccessMode.Read); return(new BinaryReader(wrappedStream)); } catch { if (wrappedStream != null) { wrappedStream.Dispose(); } throw; } } return(currentReader); }
public static IEnumerator AsyncLoad(string abPath, string name, Action <UnityEngine.Object> onLoadedCall) { var fileStream = new EncryptionStream(abPath, FileMode.Open, FileAccess.Read, encryKey); //var fileStream = new FileStream( abPath, FileMode.Open, FileAccess.Read ); var bundleLoadRequest = AssetBundle.LoadFromStreamAsync(fileStream); yield return(bundleLoadRequest); var myLoadedAssetBundle = bundleLoadRequest.assetBundle; var assetLoadRequest = myLoadedAssetBundle.LoadAssetAsync(name); yield return(assetLoadRequest); try { onLoadedCall(assetLoadRequest.asset); } catch (System.Exception e) { Debug.LogError(e.Message); } myLoadedAssetBundle.Unload(false); fileStream.Close(); }
public void TestReadOddNumberOfBytesAllAtOnce() { Assert.Equal(0, this.origBytes.Length % 2); Assert.True(this.origBytes.Length > 2); // drop last byte to get odd number of bytes this.origBytes = this.origBytes.Take(this.origBytes.Length - 1).ToArray(); this.expectedCipher = this.expectedCipher.Take(this.expectedCipher.Length - 1).ToArray(); // now the last byte should not be encrypted this.expectedCipher[this.expectedCipher.Length - 1] = this.origBytes[this.origBytes.Length - 1]; Assert.Equal(1, this.origBytes.Length % 2); using (var encryptionStream = new EncryptionStream(TestData.GetStream(this.origBytes), AnnoRDA.IO.StreamAccessMode.Read)) { // we'll need two passes to read all data var actualCipher = new byte[this.origBytes.Length]; Assert.Equal(actualCipher.Length - 1, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); Assert.Equal(1, encryptionStream.Read(actualCipher, actualCipher.Length - 1, 1)); Assert.Equal(this.expectedCipher, actualCipher); Assert.Equal(0, encryptionStream.Read(actualCipher, 0, actualCipher.Length)); // EOF } }
public AesEncoderNode(System.Security.Cryptography.ICryptoTransform encoder) { mStream = new EncryptionStream(this, encoder); }