| licenseKey 最后一个A解码时没用到 
 
 [C#] 纯文本查看 复制代码 using System;
class Program
{
    private static void DecodeAlphaNumString(string key, byte[] bits, uint bitsLen, uint bitCount, uint startIndex)
    {
        ushort num = 11;
        uint num2 = 0;
        uint num3 = 0;
        while (num2 < bitsLen && num3 < bitCount)
        {
            ushort num5 = GetAlphaNumValue(key[(int)startIndex++]);
            num5 <<= num;
            uint num6 = num2;
            bits[(int)num6] |= (byte)((num5 & 0xFF00) >> 8);
            uint num7 = num2 + 1;
            bits[(int)num7] |= (byte)(num5 & 0xFF);
            if (num < 8)
            {
                num += 3;
                num2 += 1;
            }
            else
            {
                num -= 5;
            }
            num3 += 5;
        }
    }
    private static ushort GetAlphaNumValue(char alphaNum)
    {
        return (ushort)"ABJCKTDL4UEMW71FNX52YGP98Z63HRS0".IndexOf(alphaNum);
    }
    private static char GetAlphaNumCharacter(ushort value)
    {
        return "ABJCKTDL4UEMW71FNX52YGP98Z63HRS0"[value];
    }
    private static string EncodeAlphaNumString(byte[] bits, uint bitsLen, uint bitCount, uint startIndex)
    {
        ushort num = 11;
        uint num2 = 0;
        uint num3 = 0;
        char[] encodedChars = new char[bitCount / 5 + 1]; // Each character encodes 5 bits
        while (num2 < bitsLen && num3 < bitCount)
        {
            uint num6 = num2;
            uint num7 = num2 + 1;
            ushort num5 = (ushort)((bits[(int)num6] << 8) | bits[(int)num7]);
            num5 >>= num;
            encodedChars[num3 / 5] = GetAlphaNumCharacter((ushort)(num5 & 0x1F)); // 0x1F = 00011111 in binary, to get the last 5 bits
            if (num < 8)
            {
                num += 3;
                num2 += 1;
            }
            else
            {
                num -= 5;
            }
            num3 += 5;
        }
        return new string(encodedChars);
    }
    static void Main(string[] args)
    {
        // Parameters and constants
        var licenseKey = "WDN304RKRDHBUEDDA1";
        uint bitCount = 65;
        uint startIndex = 5;
        uint bitsLen = 9;
        byte[] bits = new byte[bitsLen];
        // DecodeAlphaNumString
        DecodeAlphaNumString(licenseKey, bits, bitsLen, bitCount, startIndex);
        // Output decoded bits
        Console.WriteLine("Decoded Bits: " + BitConverter.ToString(bits));
        // EncodeAlphaNumString
        string encodedString = EncodeAlphaNumString(bits, bitsLen, bitCount, startIndex);
        // Output encoded string
        Console.WriteLine("Encoded String: " + licenseKey.Substring(0, (int)startIndex) + encodedString);
        Console.ReadKey();
    }
}
 |