From 35946c8e846190bb2402f846b370feca7a50a812 Mon Sep 17 00:00:00 2001 From: Ray Koopa Date: Tue, 1 Jan 2019 22:03:36 +0100 Subject: [PATCH] Port (raw) password decryption code to C#. --- src/Syroot.Worms/OnlineWorms/LaunchConfig.cs | 107 +++++++++++++++++-- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/src/Syroot.Worms/OnlineWorms/LaunchConfig.cs b/src/Syroot.Worms/OnlineWorms/LaunchConfig.cs index 8a41b99..9732902 100644 --- a/src/Syroot.Worms/OnlineWorms/LaunchConfig.cs +++ b/src/Syroot.Worms/OnlineWorms/LaunchConfig.cs @@ -18,6 +18,7 @@ namespace Syroot.Worms.OnlineWorms private static readonly char[] _invalidUserNameChars = { '=', '&', ' ' }; private string _userName; + private string _passwordString; // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ @@ -51,11 +52,6 @@ namespace Syroot.Worms.OnlineWorms } } - /// - /// Gets or sets the initially entered password. This value is encrypted before being stored in the config. - /// - public string Password { get; set; } - // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- /// @@ -72,9 +68,11 @@ namespace Syroot.Worms.OnlineWorms { stream.Write("Online Worms Config File"); - stream.Position = 36; - // TODO: Encrypt password and pass appropriate key as ";encryptedData;key". - stream.Write(";Unknown;0"); + if (_passwordString != null) + { + stream.Position = 36; + stream.Write(_passwordString); + } stream.Position = 66; stream.Write("UID=", StringCoding.Raw); @@ -87,5 +85,98 @@ namespace Syroot.Worms.OnlineWorms } return mappedFile; } + + /// + /// Removes any previously set password in the configuration. + /// + public void ClearPassword() + { + _passwordString = null; + } + + /// + /// Decrypts the password stored in the configuration as previously set through + /// . + /// + public string GetPassword() + { + // TODO: Decrypt password: + return _passwordString; + } + + /// + /// Encrypts the given with the provided initial encryption + /// and stores the result in the launch config. + /// + /// The password to store encrypted. + /// The key to encrypt with. + 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; + } } }