Skip to content

Commit 84c773d

Browse files
committed
Update MapleAESEncryption - fix depreciated AesManaged
1 parent 6d49049 commit 84c773d

File tree

1 file changed

+66
-65
lines changed

1 file changed

+66
-65
lines changed

MapleLib/MapleCryptoLib/MapleAESEncryption.cs

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,74 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2121
namespace MapleLib.MapleCryptoLib
2222
{
2323

24-
/// <summary>
25-
/// Class to handle the AES Encryption routines
26-
/// </summary>
27-
public class MapleAESEncryption
28-
{
24+
/// <summary>
25+
/// Class to handle the AES Encryption routines
26+
/// </summary>
27+
public class MapleAESEncryption
28+
{
2929

30-
/// <summary>
31-
/// Encrypt data using MapleStory's AES algorithm
32-
/// </summary>
33-
/// <param name="IV">IV to use for encryption</param>
34-
/// <param name="data">Data to encrypt</param>
35-
/// <param name="length">Length of data</param>
36-
/// <returns>Crypted data</returns>
37-
public static byte[] AesCrypt(byte[] IV, byte[] data, int length)
38-
{
39-
return AesCrypt(IV, data, length, MapleCryptoConstants.GetTrimmedUserKey(ref MapleCryptoConstants.UserKey_WzLib));
40-
}
30+
/// <summary>
31+
/// Encrypt data using MapleStory's AES algorithm
32+
/// </summary>
33+
/// <param name="IV">IV to use for encryption</param>
34+
/// <param name="data">Data to encrypt</param>
35+
/// <param name="length">Length of data</param>
36+
/// <returns>Crypted data</returns>
37+
public static byte[] AesCrypt(byte[] IV, byte[] data, int length)
38+
{
39+
return AesCrypt(IV, data, length, MapleCryptoConstants.GetTrimmedUserKey(ref MapleCryptoConstants.UserKey_WzLib));
40+
}
4141

42-
/// <summary>
43-
/// Encrypt data using MapleStory's AES method
44-
/// </summary>
45-
/// <param name="IV">IV to use for encryption</param>
46-
/// <param name="data">data to encrypt</param>
47-
/// <param name="length">length of data</param>
48-
/// <param name="key">the AES key to use</param>
49-
/// <returns>Crypted data</returns>
50-
public static byte[] AesCrypt(byte[] IV, byte[] data, int length, byte[] key)
51-
{
52-
AesManaged crypto = new AesManaged
42+
/// <summary>
43+
/// Encrypt data using MapleStory's AES method
44+
/// </summary>
45+
/// <param name="IV">IV to use for encryption</param>
46+
/// <param name="data">data to encrypt</param>
47+
/// <param name="length">length of data</param>
48+
/// <param name="key">the AES key to use</param>
49+
/// <returns>Crypted data</returns>
50+
public static byte[] AesCrypt(byte[] IV, byte[] data, int length, byte[] key)
51+
{
52+
using (Aes aes = Aes.Create())
5353
{
54-
KeySize = 256, //in bits
55-
Key = key,
56-
Mode = CipherMode.ECB // Should be OFB, but this works too
57-
};
54+
aes.KeySize = 256; // in bits
55+
aes.Key = key;
56+
aes.Mode = CipherMode.ECB; // Should be OFB, but this works too
5857

59-
using (MemoryStream memStream = new MemoryStream())
60-
{
61-
using (CryptoStream cryptoStream = new CryptoStream(memStream, crypto.CreateEncryptor(), CryptoStreamMode.Write))
62-
{
63-
int remaining = length;
64-
int llength = 0x5B0;
65-
int start = 0;
66-
while (remaining > 0)
67-
{
68-
byte[] myIV = MapleCrypto.MultiplyBytes(IV, 4, 4);
69-
if (remaining < llength)
70-
{
71-
llength = remaining;
72-
}
73-
for (int x = start; x < (start + llength); x++)
74-
{
75-
if ((x - start) % myIV.Length == 0)
76-
{
77-
cryptoStream.Write(myIV, 0, myIV.Length);
78-
byte[] newIV = memStream.ToArray();
79-
Array.Copy(newIV, myIV, myIV.Length);
80-
memStream.Position = 0;
81-
}
82-
data[x] ^= myIV[(x - start) % myIV.Length];
83-
}
84-
start += llength;
85-
remaining -= llength;
86-
llength = 0x5B4;
87-
}
88-
}
89-
}
90-
return data;
91-
}
92-
}
58+
using (MemoryStream memStream = new MemoryStream())
59+
{
60+
using (ICryptoTransform encryptor = aes.CreateEncryptor())
61+
using (CryptoStream cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
62+
{
63+
int remaining = length;
64+
int llength = 0x5B0;
65+
int start = 0;
66+
while (remaining > 0)
67+
{
68+
byte[] myIV = MapleCrypto.MultiplyBytes(IV, 4, 4);
69+
if (remaining < llength)
70+
{
71+
llength = remaining;
72+
}
73+
for (int x = start; x < (start + llength); x++)
74+
{
75+
if ((x - start) % myIV.Length == 0)
76+
{
77+
cryptoStream.Write(myIV, 0, myIV.Length);
78+
byte[] newIV = memStream.ToArray();
79+
Array.Copy(newIV, myIV, myIV.Length);
80+
memStream.Position = 0;
81+
}
82+
data[x] ^= myIV[(x - start) % myIV.Length];
83+
}
84+
start += llength;
85+
remaining -= llength;
86+
llength = 0x5B4;
87+
}
88+
}
89+
}
90+
return data;
91+
}
92+
}
93+
}
9394
}

0 commit comments

Comments
 (0)