C#转换为Python(Linux)

我有以下代码(某种编码器,通过用置换等方式将某个字母替换为另一个字母来“编码”字符串)

using System;
using System.Text;
using System.Linq;

public class SorguDecrypt
{
    private char[] outAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/++".ToCharArray();

    private char[] inAlphabet = null;

    private Encoding turkishEncoding = null;

    public SorguDecrypt()
    {
        turkishEncoding = Encoding.GetEncoding("iso-8859-9");
        var bytes = Enumerable.Range(0, 255).Select(a => (byte)a).ToArray();
        inAlphabet = turkishEncoding.GetChars(bytes);
    }

    public string Decode(string cipherText)
    {
        if (String.IsNullOrEmpty(cipherText)) return String.Empty;

        StringBuilder decodedString = new StringBuilder();

        cipherText = cipherText.PadRight(((cipherText.Length - 1) / 4 + 1) * 4, (char)0);

        for (int j = 0; j < cipherText.Length; j += 4)
        {
            string s = cipherText.Substring(j, 4);

            int c1 = Array.IndexOf(outAlphabet,(s[0]));
            int c2 = Array.IndexOf(outAlphabet, (s[1])) + (s[0] == '/' ? 64 : 0) ;
            int c3 = Array.IndexOf(outAlphabet, (s[2])) + (s[1] == '/' ? 64 : 0) ;
            int c4 = Array.IndexOf(outAlphabet, (s[3])) + (s[2] == '/' ? 64 : 0) ;

            int r1 = (c2 * 16) / 255;
            int r2 = (c3 * 64) / 255;

            char dc1 = inAlphabet[(c1 * 4 + r1)];
            char dc2 = inAlphabet[((c2 * 16 + r2) % 256)];
            int r3 = Math.Min(254,(c3 - Array.IndexOf(inAlphabet, dc2) % 16 * 4) % 64 * 64 + c4);
            char dc3 = c4 != -1 ? inAlphabet[r3] : (char)0;

            decodedString.Append((dc1));
            if (c3 != -1) decodedString.Append((dc2));
            if (c4 != -1) decodedString.Append((dc3));
        }

        return decodedString.ToString();
    }

    public static void Main()
    {
        SorguDecrypt decrypter = new SorguDecrypt();
        string enc = "MLLBGL99845PGLfCII13GKH4HLFT";
        string dec = decrypter.Decode(enc);
        Console.WriteLine("{0}",dec);
    }

}

我想将其移植到Python(大概是本机,不使用pythonnet)。到目前为止,我做了什么:

import array

outAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/++"
outAlphabet = list(outAlphabet.encode())
bytesIso8859_9 = array.array('B', [0xA4])
inAlphabet = bytesIso8859_9.tostring().decode("ISO-8859-9")

def Decode(cipherText):

    decodedString = ""
    cipherText = cipherText.ljust(((len(cipherText) - 1) / 4 + 1) * 4, '0')

    num1 = 0
    num2 = 4
    while num1 < len(cipherText):
        s = cipherText[num1:num2]

        c1 = outAlphabet.index(s[0])
        c2 = outAlphabet.index(s[1]) + int(('64' if s[0] == '/' else '0'))
        c3 = outAlphabet.index(s[2]) + int(('64' if s[1] == '/' else '0'))
        c4 = outAlphabet.index(s[3]) + int(('64' if s[2] == '/' else '0'))

        r1 = (c2 * 16) / 255
        r2 = (c3 * 64) / 255

        dc1 = inAlphabet[(c1 * 4 + r1)]
        dc2 = inAlphabet[((c2 * 16 + r2) % 256)]
        r3 = min(254,(c3 - inAlphabet.index(dc2) % 16 * 4) % 64 * 64 + c4)
        dc3 = (inAlphabet[r3] if c4 != -1 else '0')

        decodedString+=dc1
        if (c3 != -1):
            decodedString+=dc2
        if (c4 != -1):
            decodedString+=dc3

        num1 = num1 + 4
        num2 = num2 + 4

    return str(decodedString)

enc = "MLLBGL99845PGLfCII13GKH4HLFT"
dec = Decode(enc)
print(dec)

在C#下运行:

dec.exe
YUKARI AYAZLI CADDESİ

在Python下,我陷入困境:

$ python dec.py
Traceback (most recent call last):
  File "dec.py", line 44, in <module>
    dec = Decode(enc)
  File "dec.py", line 27, in Decode
    dc1 = inAlphabet[(c1 * 4 + r1)]
IndexError: string index out of range
评论