コード例 #1
0
        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;
            }
        }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
    // 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);
    }
コード例 #4
0
        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
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        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
            }
        }
コード例 #9
0
 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
     }
 }
コード例 #10
0
 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);
     }
 }
コード例 #11
0
        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);
            }
        }
コード例 #12
0
    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();
    }
コード例 #13
0
        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
            }
        }
コード例 #14
0
        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);
            }
        }
コード例 #15
0
 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);
 }
コード例 #16
0
    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();
    }
コード例 #17
0
        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
            }
        }
コード例 #18
0
 public AesEncoderNode(System.Security.Cryptography.ICryptoTransform encoder)
 {
     mStream = new EncryptionStream(this, encoder);
 }
コード例 #19
0
 public AesEncoderNode(System.Security.Cryptography.ICryptoTransform encoder)
 {
     mStream = new EncryptionStream(this, encoder);
 }