-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
219 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
l2-unity/Assets/Scripts/Networking/ClientLibrary/Crypto/RSACrypt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
using System.Numerics; | ||
using UnityEngine; | ||
|
||
public class RSACrypt | ||
{ | ||
private RSAParameters _rsaParams; | ||
// hardcoded modulus | ||
private byte[] _modulus = new byte[] { 1, 0, 1 }; | ||
|
||
|
||
public RSACrypt(byte[] exponent, bool needUnscramble) { | ||
if(needUnscramble) { | ||
UnscrambledRSAKey(exponent); | ||
} | ||
|
||
InitRSACrypt(exponent); | ||
} | ||
|
||
private void InitRSACrypt(byte[] exponent) { | ||
_rsaParams = new RSAParameters { | ||
Modulus = _modulus, | ||
D = exponent | ||
}; | ||
} | ||
|
||
public void DecryptRSABlock(byte[] encryptedData) { | ||
// Initialize RSA with the parameters | ||
using (RSA rsa = RSA.Create()) { | ||
rsa.ImportParameters(_rsaParams); | ||
|
||
// Decrypt the data | ||
byte[] decryptedData = rsa.Decrypt(encryptedData, RSAEncryptionPadding.Pkcs1); | ||
|
||
// Convert decrypted data to string | ||
string decryptedMessage = System.Text.Encoding.UTF8.GetString(decryptedData); | ||
Console.WriteLine("Decrypted Message: " + decryptedMessage); | ||
} | ||
} | ||
|
||
public void UnscrambledRSAKey(byte[] rsaKey) { | ||
Debug.Log($"Scrambled RSA: {StringUtils.ByteArrayToString(rsaKey)}"); | ||
|
||
// step 4 : xor last 0x40 bytes with first 0x40 bytes | ||
for (int i = 0; i < 0x40; i++) { | ||
rsaKey[0x40 + i] = (byte)(rsaKey[0x40 + i] ^ rsaKey[i]); | ||
} | ||
// step 3 : xor bytes 0x0d-0x10 with bytes 0x34-0x38 | ||
for (int i = 0; i < 4; i++) { | ||
rsaKey[0x0d + i] = (byte)(rsaKey[0x0d + i] ^ rsaKey[0x34 + i]); | ||
} | ||
// step 2 : xor first 0x40 bytes with last 0x40 bytes | ||
for (int i = 0; i < 0x40; i++) { | ||
rsaKey[i] = (byte)(rsaKey[i] ^ rsaKey[0x40 + i]); | ||
} | ||
// step 1 : 0x4d-0x50 <-> 0x00-0x04 | ||
for (int i = 0; i < 4; i++) { | ||
byte temp = rsaKey[0x00 + i]; | ||
rsaKey[0x00 + i] = rsaKey[0x4d + i]; | ||
rsaKey[0x4d + i] = temp; | ||
} | ||
|
||
Debug.Log($"Unscrambled RSA: {StringUtils.ByteArrayToString(rsaKey)}"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
l2-unity/Assets/Scripts/Networking/ClientLibrary/Crypto/RSACrypt.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
l2-unity/Assets/Scripts/Networking/ClientLibrary/Crypto/SHACrypt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using UnityEngine; | ||
|
||
public class SHACrypt | ||
{ | ||
public static string ComputeSha256Hash(string rawData) { | ||
// Create a SHA256 | ||
using (SHA256 sha256Hash = SHA256.Create()) { | ||
// ComputeHash - returns byte array | ||
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData)); | ||
|
||
// Convert byte array to a string | ||
StringBuilder builder = new StringBuilder(); | ||
for (int i = 0; i < bytes.Length; i++) { | ||
builder.Append(bytes[i].ToString("x2")); | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
|
||
public static byte[] ComputeSha256HashToBytes(string rawData) { | ||
// Create a SHA256 | ||
using (SHA256 sha256Hash = SHA256.Create()) { | ||
// ComputeHash - returns byte array | ||
return sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
l2-unity/Assets/Scripts/Networking/ClientLibrary/Crypto/SHACrypt.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...ty/Assets/Scripts/Networking/ClientLibrary/Packet/Loginserver/ServerPackets/InitPacket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.