EllanJiang/GameFramework · error · GameFrameworkException

Code is invalid.

Error message

Code is invalid.

What it means

Utility.Encryption.GetSelfXorBytes(byte[] bytes, int startIndex, int length, byte[] code) throws GameFrameworkException("Code is invalid.") when the XOR encryption key array is null. The XOR routine needs at least one key byte to transform the buffer; a null code makes the operation impossible, so the library throws before mutating the data. This check fires only after the early-return when bytes itself is null.

Solutions

  1. Ensure the XOR key byte array is initialized (non-null, length > 0) before encrypting or decrypting.
  2. Verify the code path that loads/provides the key actually ran (initialization order).
  3. Guard with a null/empty check and skip or fail gracefully when no key is configured.

Example fix

// before
Utility.Converter.GetSelfXorBytes(data, 0, data.Length, xorKey);
// after
if (xorKey == null || xorKey.Length == 0) throw new InvalidOperationException("XOR key not configured");
Utility.Converter.GetSelfXorBytes(data, 0, data.Length, xorKey);
Defensive patterns

Strategy: validation

Validate before calling

if (code == null || code.Length == 0) throw new InvalidOperationException("XOR key must be configured before encryption");

Type guard

bool IsValidXorKey(byte[] code) => code != null && code.Length > 0;

Try / catch

try { Utility.Converter.GetSelfXorBytes(data, 0, data.Length, key); }
catch (GameFrameworkException ex) { Log.Error("XOR encryption failed: {0}", ex.Message); /* abort save or use unencrypted fallback */ }

Prevention

When it happens

Trigger: Calling GetSelfXorBytes / GetXorBytes / GetQuickSelfXorBytes with a null key, e.g. Utility.Converter.GetSelfXorBytes(data, 0, data.Length, null), commonly when the encryption key failed to load or was never set.

Common situations: Save-file encryption where the key comes from config that is missing; checksum/obfuscation helpers where a hardcoded key was removed during refactoring; uninitialized static key fields before initialization completes.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/b31b173291fcdb20. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Utility/Utility.Encryption.cs:111

            }

            /// <summary>
            /// 将 bytes 使用 code 做异或运算。此方法将复用并改写传入的 bytes 作为返回值,而不额外分配内存空间。
            /// </summary>
            /// <param name="bytes">原始及异或后的二进制流。</param>
            /// <param name="startIndex">异或计算的开始位置。</param>
            /// <param name="length">异或计算长度。</param>
            /// <param name="code">异或二进制流。</param>
            public static void GetSelfXorBytes(byte[] bytes, int startIndex, int length, byte[] code)
            {
                if (bytes == null)
                {
                    return;
                }

                if (code == null)
                {
                    throw new GameFrameworkException("Code is invalid.");
                }

                int codeLength = code.Length;
                if (codeLength <= 0)
                {
                    throw new GameFrameworkException("Code length is invalid.");
                }

                if (startIndex < 0 || length < 0 || startIndex + length > bytes.Length)
                {
                    throw new GameFrameworkException("Start index or length is invalid.");
                }

                int codeIndex = startIndex % codeLength;
                for (int i = startIndex; i < length; i++)
                {
                    bytes[i] ^= code[codeIndex++];
                    codeIndex %= codeLength;

View on GitHub (pinned to d0c010b051)