Ejemplo n.º 1
0
        static void Write(IO.EndianWriter s, bool encrypt, IO.IEndianStreamable obj,
                          long size = 0, ulong userKey = 0, Action <IO.EndianWriter> writeLeftovers = null)
        {
            if (!encrypt)
            {
                obj.Write(s);

                if (writeLeftovers != null)
                {
                    writeLeftovers(s);
                }
            }
            else
            {
                using (var ms = new System.IO.MemoryStream())
                    using (var sin = new IO.EndianWriter(ms, Shell.EndianFormat.Big))
                        using (var encrypted = new IO.EndianReader(ms, Shell.EndianFormat.Big))
                        {
                            obj.Write(sin);

                            if (writeLeftovers != null)
                            {
                                writeLeftovers(sin);
                            }

                            encrypted.Seek(0);

                            var tea = new Security.Cryptography.PhxTEA(encrypted, s);
                            tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyGameFile, userKey);
                            tea.Encrypt(size);
                        }
            }
        }
Ejemplo n.º 2
0
        static void Read(IO.EndianReader s, bool decrypt, IO.IEndianStreamable obj,
                         long size = 0, ulong userKey = 0, Action <IO.EndianReader> readLeftovers = null)
        {
            if (!decrypt)
            {
                obj.Read(s);
            }
            else
            {
                using (var ms = new System.IO.MemoryStream())
                    using (var sout = new IO.EndianWriter(ms, Shell.EndianFormat.Big))
                        using (var decrypted = new IO.EndianReader(ms, Shell.EndianFormat.Big))
                        {
                            long position = s.BaseStream.Position;

                            var tea = new Security.Cryptography.PhxTEA(s, sout);
                            tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyGameFile, userKey);
                            tea.Decrypt(size);

                            decrypted.Seek(0);
                            obj.Read(decrypted);

                            if (readLeftovers != null)
                            {
                                readLeftovers(decrypted);
                            }
                        }
            }
        }
Ejemplo n.º 3
0
        static void Stream(IO.EndianStream s, bool crypt, IO.IEndianStreamSerializable obj,
                           long size = 0, ulong userKey = 0, Action <IO.EndianStream> streamLeftovers = null)
        {
            if (!crypt)
            {
                obj.Serialize(s);

                if (s.IsWriting && streamLeftovers != null)
                {
                    streamLeftovers(s);
                }
            }
            else
            {
                using (var ms = new System.IO.MemoryStream())
                    using (var crypted = new IO.EndianStream(ms, s.ByteOrder))
                    {
                        crypted.StreamMode = s.StreamMode;

                        if (s.IsReading)
                        {
                            var tea = new Security.Cryptography.PhxTEA(s.Reader, crypted.Writer);
                            tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyGameFile, userKey);
                            tea.Decrypt(size);

                            crypted.Seek(0);
                        }

                        obj.Serialize(crypted);

                        if (streamLeftovers != null)
                        {
                            streamLeftovers(crypted);
                        }

                        if (s.IsWriting)
                        {
                            crypted.Seek(0);

                            var tea = new Security.Cryptography.PhxTEA(crypted.Reader, s.Writer);
                            tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyGameFile, userKey);
                            tea.Encrypt(size);
                        }
                    }
            }
        }
Ejemplo n.º 4
0
        public static void CryptStream(IO.EndianReader input, IO.EndianWriter output, CryptographyTransformType transformType)
        {
            Contract.Requires(input != null);
            Contract.Requires(output != null);
            // This should be OK because PhxTEA is buffered
            //Contract.Requires(input.BaseStream != output.BaseStream);

            var tea = new Security.Cryptography.PhxTEA(input, output);

            tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyEra);

            switch (transformType)
            {
            case CryptographyTransformType.Decrypt:
                tea.Decrypt();
                break;

            case CryptographyTransformType.Encrypt:
                tea.Encrypt();
                break;
            }
        }