我有下面的代码(某种编码器,通过用置换等将一些字母表替换成另一个字母表来“编码”字符串)
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(最好是原生的,不要使用Python)。到目前为止我所做的:
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
我的第一个攻击是使用pythonnet,但我还有另一个问题:
import clr
from System import Text
outAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/++"
outAlphabet = list(outAlphabet.encode())
turkishEncoding = Text.Encoding.GetEncoding("iso-8859-9")
bytes = [bytes(a) for a in range(255)]
inAlphabet = turkishEncoding.GetChars(bytes)
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)
也许我没有正确解析“iso-8859-9”字母表:
c:\Python27\python.exe dec.py
Traceback (most recent call last):
File "dec.py", line 8, in <module>
inAlphabet = turkishEncoding.GetChars(bytes)
TypeError: No method matches given arguments for GetChars
解决了将“ISO-8859-9”字符映射设置为字典的问题,如果有人知道其他更干净的方法,请告诉我:
输出:
如果您使用的是较低的python版本,如Python2.7,则不会造成问题,因为在这个版本中,它只为“/”操作返回整数
但在Python3.x中,它返回实数而不是整数
在Python 3.6中
输出:8.972549019607843
在Python2.7中
输出:8
我认为问题在于c#
返回一个
int
,但为python格式可以返回不能用作索引的实数
相关问题 更多 >
编程相关推荐