Esempio n. 1
0
        /// <summary>
        /// Encrypt data using MapleStory's AES method
        /// </summary>
        /// <param name="iv">IV to use for encryption</param>
        /// <param name="data">data to encrypt</param>
        /// <param name="length">length of data</param>
        /// <param name="key">the AES key to use</param>
        /// <returns>Crypted data</returns>
        public static byte[] AesCrypt(byte[] iv, byte[] data, int length, byte[] key)
        {
            var crypto = new AesManaged
            {
                KeySize = 256,
                Key     = key,
                Mode    = CipherMode.ECB // Should be OFB, but this works too
            };

            using (var memStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memStream, crypto.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    var remaining = length;
                    var size      = 0x5B0;
                    var start     = 0;

                    while (remaining > 0)
                    {
                        var myIv = MapleCrypto.MultiplyBytes(iv, 4, 4);
                        if (remaining < size)
                        {
                            size = remaining;
                        }

                        for (var x = start; x < start + size; x++)
                        {
                            if ((x - start) % myIv.Length == 0)
                            {
                                cryptoStream.Write(myIv, 0, myIv.Length);
                                var newIv = memStream.ToArray();
                                Array.Copy(newIv, myIv, myIv.Length);
                                memStream.Position = 0;
                            }

                            data[x] ^= myIv[(x - start) % myIv.Length];
                        }

                        start     += size;
                        remaining -= size;
                        size       = 0x5B4;
                    }
                }
            }

            return(data);
        }
Esempio n. 2
0
        /// <summary>
        /// Encrypt data using MapleStory's AES method
        /// </summary>
        /// <param name="IV">IV to use for encryption</param>
        /// <param name="data">data to encrypt</param>
        /// <param name="length">length of data</param>
        /// <param name="key">the AES key to use</param>
        /// <returns>Crypted data</returns>
        public static byte[] AesCrypt(byte[] IV, byte[] data, int length, byte[] key)
        {
            AesManaged crypto = new AesManaged
            {
                KeySize = 256,           //in bits
                Key     = key,
                Mode    = CipherMode.ECB // Should be OFB, but this works too
            };

            using (MemoryStream memStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memStream, crypto.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    int remaining = length;
                    int llength   = 0x5B0;
                    int start     = 0;
                    while (remaining > 0)
                    {
                        byte[] myIV = MapleCrypto.MultiplyBytes(IV, 4, 4);
                        if (remaining < llength)
                        {
                            llength = remaining;
                        }
                        for (int x = start; x < (start + llength); x++)
                        {
                            if ((x - start) % myIV.Length == 0)
                            {
                                cryptoStream.Write(myIV, 0, myIV.Length);
                                byte[] newIV = memStream.ToArray();
                                Array.Copy(newIV, myIV, myIV.Length);
                                memStream.Position = 0;
                            }
                            data[x] ^= myIV[(x - start) % myIV.Length];
                        }
                        start     += llength;
                        remaining -= llength;
                        llength    = 0x5B4;
                    }
                }
            }
            return(data);
        }