public void EncryptWoItm()
        {
            string keys = "AABBCCDDEEFFGGHH";
            string csv  =
                "100101,HELMET,NORMAL,,0,,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,,,0,0,0,0,0,0,0,0,1,,,40,40,20,,0,0,0,0,0,0,,,,0,0,0,0,0,0,,,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,,NONE,HEAD,,,,0,0,0,0,0,0,1,0,,100101,,,,,,,,,,,,,,,,,,,,,,,,,0,0,5,1,0";
            string csv1 =
                "100101, HELMET, NORMAL, , 0, , 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, , , 0, 0, 0, 0, 0, 0, 0, 0, 1, , , 40, 40, 20, , 0, 0, 0, 0, 0, 0, , , , 0, 0, 0, 0, 0, 0, , , , , , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, , , 0, , NONE, HEAD, , , , 0, 0, 0, 0, 0, 0, 1, 0, , 100101, , , , , , , , , , , , , , , , , , , , , , , , , 0, 0, 5, 1, 0";
            Camellia c = new Camellia();

            byte[] key       = Encoding.UTF8.GetBytes(keys);
            uint   keyLength = (uint)key.Length * 8;

            byte[][] subkey      = new byte[34][];
            byte[]   oinput      = Encoding.UTF8.GetBytes(csv1);
            int      inlen       = oinput.Length;
            int      padding     = 16 - inlen % 16;
            int      totalLength = inlen + padding;

            byte[] input  = new byte[totalLength];
            int    length = input.Length;

            Buffer.BlockCopy(oinput, 0, input, 0, inlen);
            byte[] output = new byte[length];

            Span <byte> inPtr  = input;
            Span <byte> outPtr = output;

            c.KeySchedule(keyLength, key, subkey);

            int current = 0;

            while (current < length)
            {
                int xorLen = current + 16 < length ? 16 : length - current;
                // for (int i = 0; i < xorLen; i++)
                //  {
                //    input[current + i] = (byte) (input[current + i] ^ prv[i]);
                //  }

                c.CryptBlock(
                    false,
                    keyLength,
                    inPtr.Slice(current, 16),
                    subkey,
                    outPtr.Slice(current, 16)
                    );
                // for (int i = 0; i < xorLen; i++)
                //  {
                //    prv[i] = output[current + i];
                // }
                current += xorLen;
                // Console.WriteLine($"{current} {xorLen} {length}");
            }

            Console.WriteLine(HexDump(output));
            Console.WriteLine(Util.ToHexString(output));
            Console.WriteLine($"ActualSize:{output.Length} A:{output.Length + 4:X8} B:{inlen:X8}");

            // 416 = actualt data suze
            // 420
            // 412
            bool done = true;
        }
Example #2
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (file.MaxOffset <= 0x10)
            {
                return(null);
            }
            var header = new byte[0x10];

            foreach (var scheme in KnownSchemes.Values)
            {
                var encryption = new Camellia(scheme.Key);
                ReadEncrypted(file.View, encryption, 0, header, 0, 0x10);
                ILibIndexReader reader;
                if (Binary.AsciiEqual(header, 0, "LIBP"))
                {
                    reader = new LibPReader(file, encryption, header, scheme);
                }
                else if (Binary.AsciiEqual(header, 0, "LIBU"))
                {
                    reader = LibUReader.Create(file, encryption);
                }
                else
                {
                    continue;
                }
                using (reader)
                {
                    if (reader.ReadIndex())
                    {
                        return(new MalieArchive(file, this, reader.Dir, encryption));
                    }
                }
            }
            return(null);
        }
Example #3
0
 protected LibIndexReader(ArcView file, Camellia encryption, byte[] header)
 {
     m_view       = file.View;
     m_max_offset = file.MaxOffset;
     m_enc        = encryption;
     m_header     = header;
 }
 public void EncryptIntermediate128BitKey() // working 11/20/2018 11:35 AM
 {
     byte[] P      = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
     byte[] result = Camellia.EncryptBlock(P, P);
     Assert.IsTrue(compareArrays(result,
                                 new byte[16] {
         0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73, 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
     }, out int FirstDiff));
 }
 public void EncryptIntermediate256BitKey() // working 11/20/2018 11:35 AM
 {
     byte[] P = new byte[16] {
         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
     };
     byte[] K = new byte[32] {
         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
     };
     byte[] result = Camellia.EncryptBlock(P, K);
     Assert.IsTrue(compareArrays(result,
                                 new byte[16] {
         0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c, 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
     }, out int FirstDiff));
 }
 public void DecryptIntermediate192BitKey() // working 11/20/2018 11:35 AM
 {
     byte[] P = new byte[16] {
         0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8, 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
     };
     byte[] K = new byte[24] {
         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
     };
     byte[] result = Camellia.DecryptBlock(P, K);
     Assert.IsTrue(compareArrays(result,
                                 new byte[16] {
         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
     }, out int FirstDiff));
 }
Example #7
0
        private static int ReadEncrypted(ArcView.Frame view, Camellia enc, long offset, byte[] buffer, int index, int length)
        {
            int offset_pad  = (int)offset & 0xF;
            int aligned_len = (offset_pad + length + 0xF) & ~0xF;

            byte[] aligned_buf;
            int    block = 0;

            if (aligned_len == length)
            {
                aligned_buf = buffer;
                block       = index;
            }
            else
            {
                aligned_buf = new byte[aligned_len];
            }

            int read = view.Read(offset - offset_pad, aligned_buf, block, (uint)aligned_len);

            if (read < offset_pad)
            {
                return(0);
            }

            for (int block_count = aligned_len / 0x10; block_count > 0; --block_count)
            {
                enc.DecryptBlock(offset, aligned_buf, block);
                block  += 0x10;
                offset += 0x10;
            }
            if (aligned_buf != buffer)
            {
                Buffer.BlockCopy(aligned_buf, offset_pad, buffer, index, length);
            }
            return(Math.Min(length, read - offset_pad));
        }
Example #8
0
 public EncryptedStream(ArcView mmap, Camellia encryption)
 {
     m_view = mmap.CreateFrame();
     m_enc = encryption;
     m_max_offset = mmap.MaxOffset;
 }
Example #9
0
 public MalieArchive(ArcView file, ArchiveFormat format, ICollection<Entry> dir, Camellia encryption)
     : base(file, format, dir)
 {
     Encryption = encryption;
 }
Example #10
0
 public LibUReader(ArcView file, Camellia encryption, byte[] header)
     : base(file, encryption, header)
 {
     var input = new EncryptedStream (file, encryption);
     m_input = new BinaryReader (input, Encoding.Unicode);
 }
Example #11
0
 public LibPReader(ArcView file, Camellia encryption, byte[] header) : base(file, encryption, header)
 {
     m_base_offset = 0;
 }
 public void Encrypt_Exception_NullData() // working 11/20/2018 11:35 AM
 {
     Camellia.EncryptBlock(null, new byte[16]);
     Assert.Fail("Did not throw exception");
 }
Example #13
0
 public MalieArchive(ArcView file, ArchiveFormat format, ICollection <Entry> dir, Camellia encryption)
     : base(file, format, dir)
 {
     Encryption = encryption;
 }
 public void SelfTest() // working 11/20/2018 11:35 AM
 {
     Assert.IsTrue(Camellia.RunSelfTests());
 }
 public void Decrypt_Exception_InvalidKeyLength() // working 11/20/2018 11:35 AM
 {
     Camellia.DecryptBlock(new byte[16], new byte[1]);
     Assert.Fail("Did not throw exception");
 }
 public void Decrypt_Exception_NullKey() // working 11/20/2018 11:35 AM
 {
     Camellia.DecryptBlock(new byte[16], null);
     Assert.Fail("Did not throw exception");
 }
Example #17
0
 public CamelliaDecryptor(uint[] key)
 {
     m_enc = new Camellia(key);
 }
Example #18
0
 public LibScheme(uint align, string key) : this(align, Camellia.GenerateKey(key))
 {
 }
Example #19
0
        public static LibUReader Create(ArcView file, Camellia encryption)
        {
            var input = new EncryptedStream(file, encryption);

            return(new LibUReader(input));
        }
Example #20
0
            public LibUReader(ArcView file, Camellia encryption, byte[] header) : base(file, encryption, header)
            {
                var input = new EncryptedStream(file, encryption);

                m_input = new BinaryReader(input, Encoding.Unicode);
            }
Example #21
0
 public LibScheme(string key) : this(Camellia.GenerateKey(key))
 {
 }
Example #22
0
 public override ArcFile TryOpen(ArcView file)
 {
     if (file.MaxOffset <= 0x10)
         return null;
     var header = new byte[0x10];
     foreach (var key in KnownKeys.Values)
     {
         var encryption = new Camellia (key);
         ReadEncrypted (file.View, encryption, 0, header, 0, 0x10);
         LibIndexReader reader;
         if (Binary.AsciiEqual (header, 0, "LIBP"))
             reader = new LibPReader (file, encryption, header);
         else if (Binary.AsciiEqual (header, 0, "LIBU"))
             reader = new LibUReader (file, encryption, header);
         else
             continue;
         using (reader)
         {
             var dir = reader.ReadIndex();
             if (dir != null)
                 return new MalieArchive (file, this, dir, encryption);
         }
     }
     return null;
 }
Example #23
0
        private static int ReadEncrypted(ArcView.Frame view, Camellia enc, long offset, byte[] buffer, int index, int length)
        {
            int offset_pad  = (int)offset & 0xF;
            int aligned_len = (offset_pad + length + 0xF) & ~0xF;
            byte[] aligned_buf;
            int block = 0;
            if (aligned_len == length)
            {
                aligned_buf = buffer;
                block = index;
            }
            else
            {
                aligned_buf = new byte[aligned_len];
            }

            int read = view.Read (offset - offset_pad, aligned_buf, block, (uint)aligned_len);
            if (read < offset_pad)
                return 0;

            for (int block_count = aligned_len / 0x10; block_count > 0; --block_count)
            {
                enc.DecryptBlock (offset, aligned_buf, block);
                block  += 0x10;
                offset += 0x10;
            }
            if (aligned_buf != buffer)
                Buffer.BlockCopy (aligned_buf, offset_pad, buffer, index, length);
            return Math.Min (length, read-offset_pad);
        }
Example #24
0
 protected LibIndexReader(ArcView file, Camellia encryption, byte[] header)
 {
     m_view = file.View;
     m_max_offset = file.MaxOffset;
     m_enc = encryption;
     m_header = header;
 }
Example #25
0
 public LibPReader(ArcView file, Camellia encryption, byte[] header, LibScheme scheme)
     : base(file, encryption, header)
 {
     m_base_offset = 0;
     m_data_align  = scheme.DataAlign - 1;
 }
Example #26
0
 public LibPReader(ArcView file, Camellia encryption, byte[] header)
     : base(file, encryption, header)
 {
     m_base_offset = 0;
 }
Example #27
0
 public EncryptedStream(ArcView mmap, Camellia encryption)
 {
     m_view       = mmap.CreateFrame();
     m_enc        = encryption;
     m_max_offset = mmap.MaxOffset;
 }
Example #28
0
        static void Main(string[] args)
        {
            if (!Camellia.RunSelfTests())
            {
                //throw new Exception("FAILED TEST");
                Console.WriteLine("*** SELFTEST FAILED****");
            }
            else
            {
                Console.WriteLine("SELF TEST OK!");
            }
            byte[] P  = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
            byte[] K1 = new byte[16] {
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
            };
            byte[] K2 = new byte[24] {
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
            };
            byte[] K3 = new byte[32] {
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
            };
            byte[] result; int FirstDiff;

            result = Camellia.EncryptBlock(P, K1);
            if (!compareArrays(result, new byte[] { 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73, 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43 }, out FirstDiff))
            {
                //throw new Exception("FAILED TEST");
                Console.WriteLine("*** TEST 1 FAILED****");
            }
            else
            {
                Console.WriteLine("FULL ENCRYPTION TEST 1 OK!");
            }
            result = Camellia.DecryptBlock(result, K1);
            if (!compareArrays(result, P, out FirstDiff))
            {
                //throw new Exception("FAILED TEST");
                Console.WriteLine("*** TEST 1 FAILED****");
            }
            else
            {
                Console.WriteLine("FULL DECRYPTION TEST 1 OK!");
            }


            result = Camellia.EncryptBlock(P, K2);
            if (!compareArrays(result, new byte[] { 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8, 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9 }, out FirstDiff))
            {
                //throw new Exception("FAILED TEST");
                Console.WriteLine("*** TEST 2 FAILED ****");
            }
            else
            {
                Console.WriteLine("FULL ENCRYPTION TEST 2 OK!");
            }
            result = Camellia.DecryptBlock(result, K2);
            if (!compareArrays(result, P, out FirstDiff))
            {
                //throw new Exception("FAILED TEST");
                Console.WriteLine("*** TEST 2 FAILED****");
            }
            else
            {
                Console.WriteLine("FULL DECRYPTION TEST 2 OK!");
            }


            result = Camellia.EncryptBlock(P, K3);
            if (!compareArrays(result, new byte[] { 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c, 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09 }, out FirstDiff))
            {
                //throw new Exception("FAILED TEST");
                Console.WriteLine("*** TEST 3 FAILED ****");
            }
            else
            {
                Console.WriteLine("FULL ENCRYPTION TEST 3 OK!");
            }
            result = Camellia.DecryptBlock(result, K3);
            if (!compareArrays(result, P, out FirstDiff))
            {
                //throw new Exception("FAILED TEST");
                Console.WriteLine("*** TEST 3 FAILED****");
            }
            else
            {
                Console.WriteLine("FULL DECRYPTION TEST 3 OK!");
            }


            Console.WriteLine("Press ENTER to quit...");
            Console.ReadLine();
        }