Python byte for XOR byte decoding

I have an XOR encypted file by VB.net using this function to scramble:

Public Class Crypter
    ...
    'This Will convert String to bytes, then call the other function.
    Public Function Crypt(ByVal Data As String) As String
        Return Encoding.Default.GetString(Crypt(Encoding.Default.GetBytes(Data)))
    End Function

    'This calls XorCrypt giving Key converted to bytes
    Public Function Crypt(ByVal Data() As Byte) As Byte()
        Return XorCrypt(Data, Encoding.Default.GetBytes(Me.Key))
    End Function

    'Xor Encryption.
    Private Function XorCrypt(ByVal Data() As Byte, ByVal Key() As Byte) As Byte()
        Dim i As Integer
        If Key.Length <> 0 Then
            For i = 0 To Data.Length - 1
                Data(i) = Data(i) Xor Key(i Mod Key.Length)
            Next
        End If
        Return Data
    End Function
End Class

and saved this way:

Dim Crypter As New Cryptic(Key)
'open destination file
Dim objWriter As New StreamWriter(fileName)
'write crypted content
objWriter.Write(Crypter.Crypt(data))

Now I need to open the file again using Python, but I'm having trouble getting single bytes , this is the XOR function in python:

def crypto(self, data):
    'crypto(self, data) -> str'
    return ''.join(chr((ord(x) ^ ord(y)) % 256) \
        for (x, y) in izip(data.decode('utf-8'), cycle(self.key))

I had to add% 256, because sometimes x is> 256 ie not one byte .

This thing of two bytes transmitted does not violate decryption , because the key saves "paired" with the following data.

. - , à, è, ì . .

, 256, , , chr...

+3
4

, :

Return Encoding.Default.GetString(Crypt(Encoding.Default.GetBytes(Data)))

, , Crypt, . Convert.ToBase64String, Python (, , Base-64 .

, , XORing , , .

+1

, , 256. Python 2.x chr 256. unichr chr, :

return ''.join(unichr((ord(x) ^ ord(y))) \
    for (x, y) in izip(data.decode('utf-8'), cycle(self.key))
+3

(.. ) StreamWriter? ? ?

Dim objWriter As New StreamWriter(fileName)
objWriter.Write(Crypter.Crypt(data))

Crypter.Crypt StreamWriter.Write?

Public Function Crypt(ByVal Data() As Byte) As Byte()

?

Public Function Crypt(ByVal Data As String) As String

Vb.net...


, , / "²"

for (x, y) in izip(data.decode('utf-8'), cycle(self.key.decode('utf-8'))):
    if (ord(x) ^ ord(y)) > 255 or chr(ord(x) ^ ord(y)) == '\xb2':
        print (x, y, chr((ord(x) ^ ord(y)) % 256),
               unichr(ord(x) ^ ord(y)), ord(x), ord(y))

:

ù K ² ² 249 75
 p ² ² 194 112
Æ t ² ² 198 116
‚ 0 * ‪ 8218 48

, ... , , outh

+2

If you open the file in text mode, it can be interpreted as text in Unicode. Try opening it in binary mode to make all characters in bytes. This should avoid problems with chr. If you are using Python 3.x, remember that when working in binary mode, you should use byte literals, not string literals, the last of which are Unicode strings by design.

+1
source

Source: https://habr.com/ru/post/1744840/


All Articles