Esempio n. 1
0
 internal static byte[] Cipher(byte[] buffer, ref byte[] key, bool decrypt)
 {
     var cipher = new Blowfish(key);
     var b = new byte[8];
     using (var m = new MemoryStream(buffer, true))
     {
         while (m.Position < m.Length)
         {
             //Endian swap 2 sets of 4 bytes
             for (int i = 3; i >= 0; i--)
             {
                 b[i] = (byte)m.ReadByte();
             }
             for (int i = 7; i >= 4; i--)
             {
                 b[i] = (byte)m.ReadByte();
             }
             //cipher the 8 bytes
             if (decrypt) cipher.Decipher(b, 8);
             else cipher.Encipher(b, 8);
             //Reset stream position to prepare for writing.
             m.Position -= 8;
             //Endian swap 4 bytes twice
             for (int i = 3; i >= 0; i--)
             {
                 m.WriteByte(b[i]);
             }
             for (int i = 7; i >= 4; i--)
             {
                 m.WriteByte(b[i]);
             }
         }
     }
     return buffer;
 }
Esempio n. 2
0
 public byte[] Decrypt(byte[] buffer)
 {
     _cipher = new Blowfish(_key);
     var b = new byte[8];
     ushort seed = 0;
     buffer = Functions.Cipher(buffer, ref _key, true);
     using (var m = new MemoryStream(buffer, true))
     {
         //Get the XOR seed
         m.Position = 0;
         var b2 = new byte[4];
         m.Read(b2, 0, 4);
         var k = BitConverter.ToUInt32(b2, 0);
         seed = (ushort)(k >> 16);
     }
     //XOR the data using the seed
     buffer = XOR(buffer.Skip(4).ToArray(), seed);
     //Get expected checksum
     UInt32 csum = BitConverter.ToUInt32(buffer, 0);
     //Strip checksum
     byte[] end = new byte[buffer.Length - 4];
     Buffer.BlockCopy(buffer, 4, end, 0, end.Length);
     //Sum the byte[]
     UInt32 checksum = CalcChecksum(end);
     //Compare them
     if (csum != (checksum)) return end;
     return end;
 }
Esempio n. 3
0
        /// <summary>
        /// Decrypt a buffer containing MH4U DLC
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns>Decrypted DLC</returns>
        public byte[] Decrypt(byte[] buffer)
        {
            _cipher = new Blowfish(Key);
            var b = new byte[8];
            //var hash = new byte[20];
            using (var m = new MemoryStream(buffer, true))
            {
                while (m.Position < m.Length - 4) //0x1c //4 //28=24+4
                {
                    //Endian swap 2 sets of 4 bytes
                    for (int i = 3; i >= 0; i--)
                    {
                        b[i] = (byte)m.ReadByte();
                    }
                    for (int i = 7; i >= 4; i--)
                    {
                        b[i] = (byte)m.ReadByte();
                    }
                    //Decrypt the 8 bytes
                    _cipher.Decipher(b, 8);
                    //Reset stream position to prepare for writing.
                    m.Position -= 8;
                    //Endian swap 4 bytes twice
                    for (int i = 3; i >= 0; i--)
                    {
                        m.WriteByte(b[i]);
                    }
                    for (int i = 7; i >= 4; i--)
                    {
                        m.WriteByte(b[i]);
                    }
                }
                //m.Position -= 24;
                ////capture sh1 hash
                //m.Read(hash, 0, 20);
                //var sh1 = System.Security.Cryptography.SHA1.Create(); sh1.ComputeHash(buffer, 0, buffer.Length - 28);
                //if (!sh1.Hash.SequenceEqual(hash))
                //{
                //    MessageBox.Show("Invalid SHA1 hash in footer.");
                //    return;
                //}
            }

            //Trim the hash (24) and other useless data (4) by cloning into a new array.
            return StripJunk(buffer);
        }
Esempio n. 4
0
 public byte[] Encrypt(byte[] buffer)
 {
     _cipher = new Blowfish(_key);
     var checksum = BitConverter.GetBytes(CalcChecksum(buffer));
     var buff = new byte[buffer.Length + 4];
     checksum.CopyTo(buff, 0);
     buffer.CopyTo(buff, 4);
     //Why we actually bother to randomise this is beyond me, might as well be 0, oh well.
     var seed = (ushort)((new Random()).Next() >> 16);
     //XOR!
     buffer = XOR(buff, seed);
     //Write seed
     buff = new byte[buffer.Length + 4];
     (BitConverter.GetBytes((seed << 16) + 0x10)).CopyTo(buff, 0);
     buffer.CopyTo(buff, 4);
     //Encrypt
     buffer = Functions.Cipher(buff, ref _key, false);
     return buffer;
 }
Esempio n. 5
0
        public void CheckTestVectors()
        {
            // Create the keys.
            byte[][] keys = new byte[variable_key.Length + set_key.Length][];
            int      i    = 0;

            for (i = 0; i < variable_key.Length; ++i)
            {
                keys[i] = variable_key[i];
            }

            for (int j = 1; j <= set_key.Length; ++j, ++i)
            {
                keys[i] = new byte[j];
                Array.Copy(set_key, 0, keys[i], 0, j);
            }

            for (i = 0; i < keys.Length; ++i)
            {
                byte[]   key = keys[i];
                Blowfish bf = new Blowfish(key);
                uint     xl, xr;
                xl = plaintext_l[i];
                xr = plaintext_r[i];
                bf.callEncipher(ref xl, ref xr);
                //bf.callEncipher(ref x1,ref xr);
                // Encipher(ref xl, ref xr);
                if (xl != ciphertext_l[i] || xr != ciphertext_r[i])
                {
                    throw new ApplicationException("Encription Failed");
                }
                xl = ciphertext_l[i];
                xr = ciphertext_r[i];
                // bf.Decipher(ref xl, ref xr);
                bf.callDecipher(ref xl, ref xr);
                if (xl != plaintext_l[i] || xr != plaintext_r[i])
                {
                    throw new ApplicationException("Decription Failed");
                }
            }
        }
Esempio n. 6
0
 BlowfishStream(Stream stream, Blowfish bf, Target target)
 {
     this.stream = stream;
     this.bf = bf;
     this.target = target;
 }
Esempio n. 7
0
 BlowfishStream(Stream stream, Blowfish bf, Target target)
 {
     this.stream = stream;
     this.bf     = bf;
     this.target = target;
 }
Esempio n. 8
0
        private ArcFile OpenEncrypted(ArcView file, int entry_count, uint main_key)
        {
            if (1 == entry_count)
                return null; // empty archive
            long current_offset = 8;

            uint seed = file.View.ReadUInt32 (current_offset+0x44);
            var twister = new MersenneTwister (seed);
            byte[] blowfish_key = BitConverter.GetBytes (twister.Rand());
            if (!BitConverter.IsLittleEndian)
                Array.Reverse (blowfish_key);

            var blowfish = new Blowfish (blowfish_key);
            var dir = new List<Entry> (entry_count-1);
            byte[] name_buffer = new byte[0x40];
            for (int i = 1; i < entry_count; ++i)
            {
                current_offset += 0x48;
                file.View.Read (current_offset, name_buffer, 0, 0x40);
                uint offset = file.View.ReadUInt32 (current_offset+0x40) + (uint)i;
                uint size   = file.View.ReadUInt32 (current_offset+0x44);
                blowfish.Decipher (ref offset, ref size);
                twister.SRand (main_key + (uint)i);
                uint name_key = twister.Rand();
                string name = DecipherName (name_buffer, name_key);

                var entry = FormatCatalog.Instance.Create<Entry> (name);
                entry.Offset = offset;
                entry.Size   = size;
                if (!entry.CheckPlacement (file.MaxOffset))
                    return null;
                dir.Add (entry);
            }
            return new FrontwingArchive (file, this, dir, blowfish);
        }
Esempio n. 9
0
 /// <summary>
 /// Parse certain executable resources for encryption passphrase.
 /// Returns null if no passphrase found.
 /// </summary>
 public static string GetPassFromExe(string filename)
 {
     var exe = NativeMethods.LoadLibraryEx (filename, IntPtr.Zero, 0x20); // LOAD_LIBRARY_AS_IMAGE_RESOURCE
     if (IntPtr.Zero == exe)
         throw new Win32Exception (Marshal.GetLastWin32Error());
     try
     {
         var code = GetResource (exe, "DATA", "V_CODE2");
         if (null == code || code.Length < 8)
             return null;
         var key = GetResource (exe, "KEY", "KEY_CODE");
         if (null != key)
         {
             for (int i = 0; i < key.Length; ++i)
                 key[i] ^= 0xCD;
         }
         else
         {
             key = Encoding.ASCII.GetBytes ("windmill");
         }
         var blowfish = new Blowfish (key);
         blowfish.Decipher (code, code.Length/8*8);
         int length = Array.IndexOf<byte> (code, 0);
         if (-1 == length)
             length = code.Length;
         return Encodings.cp932.GetString (code, 0, length);
     }
     finally
     {
         NativeMethods.FreeLibrary (exe);
     }
 }
Esempio n. 10
0
 public FrontwingArchive(ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, Blowfish cipher)
     : base(arc, impl, dir)
 {
     Encryption = cipher;
 }