mirror of
https://gitlab.com/Syroot/Worms.git
synced 2025-03-04 17:35:22 +03:00
Port (raw) password decryption code to C#.
This commit is contained in:
parent
24b545f8e2
commit
35946c8e84
@ -18,6 +18,7 @@ namespace Syroot.Worms.OnlineWorms
|
|||||||
private static readonly char[] _invalidUserNameChars = { '=', '&', ' ' };
|
private static readonly char[] _invalidUserNameChars = { '=', '&', ' ' };
|
||||||
|
|
||||||
private string _userName;
|
private string _userName;
|
||||||
|
private string _passwordString;
|
||||||
|
|
||||||
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
|
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -51,11 +52,6 @@ namespace Syroot.Worms.OnlineWorms
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the initially entered password. This value is encrypted before being stored in the config.
|
|
||||||
/// </summary>
|
|
||||||
public string Password { get; set; }
|
|
||||||
|
|
||||||
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
|
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -72,9 +68,11 @@ namespace Syroot.Worms.OnlineWorms
|
|||||||
{
|
{
|
||||||
stream.Write("Online Worms Config File");
|
stream.Write("Online Worms Config File");
|
||||||
|
|
||||||
|
if (_passwordString != null)
|
||||||
|
{
|
||||||
stream.Position = 36;
|
stream.Position = 36;
|
||||||
// TODO: Encrypt password and pass appropriate key as ";encryptedData;key".
|
stream.Write(_passwordString);
|
||||||
stream.Write(";Unknown;0");
|
}
|
||||||
|
|
||||||
stream.Position = 66;
|
stream.Position = 66;
|
||||||
stream.Write("UID=", StringCoding.Raw);
|
stream.Write("UID=", StringCoding.Raw);
|
||||||
@ -87,5 +85,98 @@ namespace Syroot.Worms.OnlineWorms
|
|||||||
}
|
}
|
||||||
return mappedFile;
|
return mappedFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes any previously set password in the configuration.
|
||||||
|
/// </summary>
|
||||||
|
public void ClearPassword()
|
||||||
|
{
|
||||||
|
_passwordString = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decrypts the password stored in the configuration as previously set through
|
||||||
|
/// <see cref="SetPassword(String, Int32)"/>.
|
||||||
|
/// </summary>
|
||||||
|
public string GetPassword()
|
||||||
|
{
|
||||||
|
// TODO: Decrypt password:
|
||||||
|
return _passwordString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encrypts the given <paramref name="password"/> with the provided initial encryption <paramref name="key"/>
|
||||||
|
/// and stores the result in the launch config.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="password">The password to store encrypted.</param>
|
||||||
|
/// <param name="key">The key to encrypt with.</param>
|
||||||
|
public void SetPassword(string password, int key)
|
||||||
|
{
|
||||||
|
// TODO: Encrypt password.
|
||||||
|
_passwordString = $";{password};{key}";
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
public string DecryptPassword(string data, int key)
|
||||||
|
{
|
||||||
|
byte[] decodeBuffer = new byte[64];
|
||||||
|
int decodeIdx = 0;
|
||||||
|
int idx = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int subIdx = 0;
|
||||||
|
uint curValue = 0;
|
||||||
|
string dataRemain = data.Substring(idx);
|
||||||
|
do
|
||||||
|
{
|
||||||
|
uint baseValue = 1;
|
||||||
|
if (subIdx > 0)
|
||||||
|
{
|
||||||
|
int subIdxCounter = subIdx;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
baseValue *= 36;
|
||||||
|
subIdxCounter--;
|
||||||
|
} while (subIdxCounter != 0);
|
||||||
|
}
|
||||||
|
char c = dataRemain[subIdx];
|
||||||
|
uint factor = (uint)(c < '0' || c > '9' ? c - '7' : c - '0');
|
||||||
|
curValue += baseValue * factor;
|
||||||
|
++subIdx;
|
||||||
|
} while (subIdx < 7);
|
||||||
|
idx += 7;
|
||||||
|
int value = DecryptValue(curValue, 0) - key;
|
||||||
|
Buffer.BlockCopy(BitConverter.GetBytes(value), 0, decodeBuffer, decodeIdx++, sizeof(int));
|
||||||
|
} while (idx < data.Length);
|
||||||
|
return Encoding.ASCII.GetString(decodeBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int DecryptValue(uint value, int shiftArrayIndex)
|
||||||
|
{
|
||||||
|
int[][] shifts = new int[][]
|
||||||
|
{
|
||||||
|
new[] { 18, 29, 7, 25, 15, 31, 22, 27, 9, 26, 3, 13, 19, 14, 20, 11, 5, 2, 23, 16, 10, 24, 28, 17, 6, 30, 0, 21, 12, 8, 4, 1 },
|
||||||
|
new[] { 26, 31, 17, 10, 30, 16, 24, 2, 29, 8, 20, 15, 28, 11, 13, 4, 19, 23, 0, 12, 14, 27, 6, 18, 21, 3, 9, 22, 7, 1, 25, 5 }
|
||||||
|
};
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
if (value != 0)
|
||||||
|
{
|
||||||
|
int idx = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
uint highBits = (uint)(value - (value & ~1));
|
||||||
|
value >>= 1; // / 2
|
||||||
|
if (highBits != 0)
|
||||||
|
{
|
||||||
|
int shift = shifts[shiftArrayIndex][idx];
|
||||||
|
result += (int)(highBits << shift);
|
||||||
|
}
|
||||||
|
idx++;
|
||||||
|
} while (value != 0);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user