Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Temas - Azav

#1
Python / Cifrado Compresor [Archivos]
Enero 29, 2015, 12:33:42 AM
Buenas a todos, hace tiempo que he estado adentrándome en el manejo de archivos con python. Lo primero que creé fue una calculadora para hacer cambios de binarios, decimales, octales y hexadecimales (No tienes permitido ver los links. Registrarse o Entrar a mi cuenta), bastante simple a decir verdad. Esta vez traigo algo un poco más elaborado.  Se trata de un algoritmo para cifrar archivos, pero que además comprime el archivo al cifrarlo. Para comprimirlo reduce la cantidad de bytes 0x00 en el archivo. Podría ilustrarse así si lo vemos como en un editor hexadecimal:
Código: text
Caso 1: A1 00 00 00 00 B2 01 7C > A1 00 03 B2 01 7C          (aquí nos ahorramos dos bytes)
Caso 2: B1 FB 00 CC A2 72 A1 2D > B1 FB 00 00 CC A2 72 A1 2D (aquí aumentamos en un byte)
Caso 3: B1 FB 00 00 A2 72 A1 2D > B1 FB 00 01 A2 72 A1 2D    (aquí no ganamos ni perdimos )

Teóricamente podría suceder que el archivo aumente su tamaño (si el caso 2 se repite mucho y el caso 1 no), pero en la práctica es sumamente extraño que pase, casi siempre el archivo va a reducir al menos unos miles de bytes. No estoy seguro si esta forma de compresión ya existía pero hago el aporte igualmente.

Luego de realizar la compresión viene el cifrado, que vendría siendo un algoritmo simple que cifra byte a byte con un contraseña. El algoritmo fue una invención mía, no es XOR aunque se parece un poco.  Luego para decodificar es el proceso inverso, creo que no es necesario explicarlo.

El código puede ser fácilmente traducido a otros lenguajes (tal vez me animo a pasarlo a VB6) y no vendría mal para quienes hacen crypters. Inicialmente el código resultaba ser un poco lento, por eso no lo había publicado, pero optimicé el código y mejoró muchísimo. Sin más, les dejo el source:
Código: python
def CheckReducement(data, byte):
    amount_list = []
    pos = data.find(byte)
    while pos != -1:
        i = 1
        while True:
            if (pos + i) < len(data):
                if data[pos + i:pos + i + 1] == byte:
                    i += 1
                else:
                    amount_list.append(i)
                    pos = data.find(byte, pos + i)
                    break
                else:
                    amount_list.append(i)
                    pos = -1
                    break
    bytes_reducidos = 0
    for amount in amount_list:
        if amount == 1:
            bytes_reducidos -= 1
        elif amount >= 3 and amount <= 256:
            ahorro = amount - 2
            bytes_reducidos += ahorro
        elif amount > 256:
            ahorro = (((amount // 256) * 254) + ((amount % 256) - 2))
            bytes_reducidos += ahorro
    return bytes_reducidos

def DoReducement(inputfile, outputfile, password):
    f = open(inputfile, "br")
    data = f.read()
    f.close()       
    reduccion = CheckReducement(data, b"\x00")
    porcentaje = (reduccion * 100) / len(data)
    byte_list = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
    pos = data.find(b"\x00")
    pos2 = 0
    nueva_data = b""
    while pos != -1:
        i = 1
        while True:
            if (pos + i) < len(data):
                if data[pos + i:pos + i + 1] == b"\x00":
                    i += 1
                else:
                    if i <= 256:
                        nueva_data += data[pos2:pos + 1] + byte_list[i - 1:i]
                        pos2 = pos + i
                        pos = data.find(b"\x00", pos2)
                    else:
                        nueva_data += data[pos2:pos + 1] + b"\xFF"
                        pos2 = pos + 256
                        pos = data.find(b"\x00", pos2)
                    break
            else:
                if i <= 256:
                    nueva_data += data[pos2:pos + 1] + byte_list[i - 1:i]
                    pos2 = pos + i
                    pos = data.find(b"\x00", pos2)
                else:
                    nueva_data += data[pos2:pos + 1] + b"\xFF"
                    pos2 = pos + 256
                    pos = data.find(b"\x00", pos2)
                break
    nueva_data += data[pos2:]
    nueva_data = EncodeBytes(nueva_data, password)
    f = open(outputfile, "bw")
    f.write(nueva_data)
    f.close()
    return "El archivo fue cifrado con éxito y su peso se redujo en un " + str(porcentaje)[:4] + "% (" + str(reduccion) + " bytes reducidos)."

       
def DoIncrement(inputfile, outputfile, password):
    f = open(inputfile, "br")
    data = f.read()
    f.close()       
    data = DecodeBytes(data, password)
    byte_list = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
    zero_list = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    pos = data.find(b"\x00")
    pos2 = 0
    nueva_data = b""
    while pos != -1:
        amount = byte_list.find(data[pos + 1:pos + 2])
        zero_bytes = zero_list[:amount]
        nueva_data += data[pos2:pos + 1] + zero_bytes
        pos2 = pos + 2
        pos = data.find(b"\x00", pos2)
    nueva_data += data[pos2:]
    f = open(outputfile, "bw")
    f.write(nueva_data)
    f.close()       
    return "El archivo fue decifrado con éxito."

def EncodeBytes(bytes_data, password): 
    return b''.join(chr((bytes_data[i] + ord(password[i % len(password)])) % 256).encode('iso8859-1') for i in range(len(bytes_data)))
               
def DecodeBytes(bytes_data, password):
    return b''.join(chr((bytes_data[i] - ord(password[i % len(password)])) % 256).encode('iso8859-1') for i in range(len(bytes_data)))
       
print(DoReducement("archivo.exe", "cifrado.exe", "contraseña"))
print(DoIncrement("cifrado.exe", "archivo_original.exe", "contraseña"))

Para quienes quieran estudiar mejor el algoritmo les dejo el source no optimizado, que hace exactamente lo mismo que el código de arriba pero es más fácil de entender.

Código: python
def CheckReducement(data, byte):
    amount_list = []
    pos = data.find(byte)
    while pos != -1:
        i = 1
        while True:
            if (pos + i) < len(data):
                if data[pos + i:pos + i + 1] == byte:
                    i += 1
                else:
                    amount_list.append(i)
                    pos = data.find(byte, pos + i)
                    break
            else:
                amount_list.append(i)
                pos = -1
                break
    bytes_reducidos = 0
    for amount in amount_list:
        if amount == 1:
            bytes_reducidos -= 1
        elif amount >= 3 and amount <= 256:
            ahorro = amount - 2
            bytes_reducidos += ahorro
        elif amount > 256:
            ahorro = (((amount // 256) * 254) + ((amount % 256) - 2))
            bytes_reducidos += ahorro
    return bytes_reducidos

def DoReducement(inputfile, outputfile, password):
    f = open(inputfile, "br")
    data = f.read()
    f.close()       
    reduccion = CheckReducement(data, b"\x00")
    porcentaje = (reduccion * 100) / len(data)
    byte_list = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
    pos = data.find(b"\x00")
    while pos != -1:
        i = 1
        while True:
            if (pos + i) < len(data):
                if data[pos + i:pos + i + 1] == b"\x00":
                   i += 1
                else:
                    if i <= 256:
                        data = data[:pos + 1] + byte_list[i - 1:i] + data[pos + i:]
                    else:
                        data = data[:pos + 1] + byte_list[255:] + data[pos + 256:]
                    pos = data.find(b"\x00", pos + 2)
                    break
            else:
                    if i <= 256:
                        data = data[:pos + 1] + byte_list[i - 1:i] + data[pos + i:]
                    else:
                        data = data[:pos + 1] + byte_list[255:] + data[pos + 256:]
                    pos = data.find(b"\x00", pos + 2)
                    break
    data = EncodeBytes(data, password)
    f = open(outputfile, "bw")
    f.write(data)
    f.close()       
    return "El archivo fue cifrado con éxito y su peso se redujo en un " + str(porcentaje)[:4] + "% (" + str(reduccion) + " bytes reducidos)."

       
def DoIncrement(inputfile, outputfile, password):
    f = open(inputfile, "br")
    data = f.read()
    f.close()       
    data = DecodeBytes(data, password)
    byte_list = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
    zero_list = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    pos = data.find(b"\x00")
    while pos != -1:
        amount = byte_list.find(data[pos + 1:pos + 2])
        zero_bytes = zero_list[:amount]
        data = data[:pos + 1] + zero_bytes + data[pos + 2:]
        pos = data.find(b"\x00", pos + len(zero_bytes) + 1)
    f = open(outputfile, "bw")
    f.write(data)
    f.close()       
    return "El archivo fue decifrado con éxito."

def EncodeBytes(bytes_data, password):
    byte_list = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
    i = 0
    nueva_data = b''
    while i < len(bytes_data):
        j = i % len(password)
        suma = bytes_data[i] + ord(password[j])
        if suma > 255:
            suma = suma % 256
        nueva_data += byte_list[suma:suma + 1] 
        i += 1
    return nueva_data
               
def DecodeBytes(bytes_data, password):
    byte_list = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
    nueva_data = b''
    i = 0
    while i < len(bytes_data):
        j = i % len(password)
        resta = bytes_data[i] - ord(password[j])
        if resta < 0:
            resta = resta % 256
        nueva_data += byte_list[resta:resta + 1]       
        i += 1
    return nueva_data
       
print(DoReducement("archivo.exe", "cifrado.exe", "contraseña"))
print(DoIncrement("cifrado.exe", "archivo_original.exe", "contraseña"))


Espero alguien pueda darle buen uso, saludos a todos.
#2
Python / NumSys Calculator by Azav
Enero 23, 2015, 04:16:23 AM
Numbering System Calculator by Azav

La semana pasada estuve investigando las representaciones ASCII de los caracteres, para las cuales se usan distintos sistemas numéricos (varían en su base). Los cuatro sistemas numéricos más usados son el binario, el octal, el decimal y el hexadecimal. Por ejemplo: la letra 'a' tiene por valor ASCII el numero 97 en el sistema decimal, o bien 1100001 en binario, o bien 141 en octal o bien 61 en hexadecimal (este último es que se usa en los editores de archivos ejecutables). Resulta que para entender mejor cómo se llevan a cabo las transformaciones de un sistema a otro creé esta herramienta , que también permite realizar operaciones (suma y resta) en los diversos sistemas numéricos. No es nada del otro mundo pero a mi me sirvió para entender mejor cómo funcionan estos sistemas.

La herramienta la programé en Python 3.4 y usé tkinter para la interfaz gráfica. Convertí el archivo .py a ejecutable usando py2exe. Captura (se ve un poco mal):


Source code:
Código: python
from tkinter import *
from tkinter import messagebox, ttk

def cambio_de_base(base, valorbase10):
caracteres = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lista = [valorbase10]
while True:
division = valorbase10 // base
if division == 0:
break
else:
lista.append(division)
valorbase10 = division
lista.reverse()
valorbase2 = ''
for i in lista:
resto = i % base
valorbase2 += caracteres[resto:resto + 1]
return valorbase2

def cambio_a_decimal(base, valorbase):
caracteres = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for char in valorbase:
if caracteres[:base].find(char) == -1:
return "ERROR"
j = len(valorbase)
k = -1
sumatoria = 0
for i in range(j):
valor_parcial = caracteres.find(valorbase[k:j]) * (base ** i)
sumatoria = sumatoria + valor_parcial
j = k
k = k - 1
return sumatoria


class Interfaz():
def __init__(self):
#Main Window
self.mainWindow = Tk()
self.mainWindow.geometry("450x250")
self.mainWindow.title("Azav's Numeric Calculator")
self.mainWindow.protocol("WM_DELETE_WINDOW", self.quit)

#ComboBoxs
ComboVar = StringVar()
self.Combo = ttk.Combobox(self.mainWindow, textvariable=ComboVar, width = "2", values=('+', '-'))
self.Combo.place(x=105, y=85)

#Textboxs
self.Sumando1 = Text(self.mainWindow, height = "0.7", width = "30", relief = "flat")
self.Sumando1.place(x=150, y=55)
self.Sumando2 = Text(self.mainWindow, height = "0.7", width = "30", relief = "flat")
self.Sumando2.place(x=150, y=85)
self.Suma = Text(self.mainWindow, height = "0.7", width = "30", relief = "flat")
self.Suma.place(x=150, y=140)

def start(self):
def Operate():
nro1 = self.Sumando1.get('1.0', 'end').strip().upper()
nro2 = self.Sumando2.get('1.0', 'end').strip().upper()
base = OptVar.get()
nro1_dec = cambio_a_decimal(base, nro1)
nro2_dec = cambio_a_decimal(base, nro2)
if nro1_dec == "ERROR":
messagebox.showwarning("Azav's Numeric Calculator", nro1 + " is not a valid value for base " + str(base) + ".")
return
if nro2_dec == "ERROR":
messagebox.showwarning("Azav's Numeric Calculator", nro2 + " is not a valid value for base " + str(base) + ".")
return
if self.Combo.get().strip() == '+':
resultado = nro1_dec + nro2_dec
else:
resultado = nro1_dec - nro2_dec
if resultado < 0:
messagebox.showwarning("Azav's Numeric Calculator", "Negative numbers are not supported.")
return
resultado_base = cambio_de_base(base, resultado)
self.Suma.delete('1.0', 'end')
self.Suma.insert('1.0', str(resultado_base))
info_list = []
if base != 2:
info_list.append("Binary result: " + str(cambio_de_base(2, resultado)))
if base != 8:
info_list.append("Octal result: " + str(cambio_de_base(8, resultado)))
if base != 10:
info_list.append("Decimal result: " + str(resultado))
if base != 16:
info_list.append("Hexdecimal result: " + str(cambio_de_base(16, resultado)))
info = ""
for i in info_list:
info = info + i + "\n"
lbInfo['text'] = info

def BaseConverter():
base = OptVar.get()
suma = self.Suma.get('1.0', 'end').strip().upper()
NewGUI = BaseConverterGUI()
NewGUI.txtBase.delete('1.0', 'end')
NewGUI.txtBase.insert('1.0', str(base))
exec("NewGUI.txt" + str(base) + ".delete('1.0', 'end')")
exec("NewGUI.txt" + str(base) + ".insert('1.0', suma)")
NewGUI.ConvertBases()
NewGUI.start()

#Combo Config
self.Combo.set('+')

#OptionButton
OptVar = IntVar()
opt1 = Radiobutton(self.mainWindow, text="Binary", width = "10", variable=OptVar, indicatoron=0, value=2).place(x=15, y=15)
opt2 = Radiobutton(self.mainWindow, text="Octal", width = "10", variable=OptVar, indicatoron=0, value=8).place(x=115, y=15)
opt3 = Radiobutton(self.mainWindow, text="Decimal", width = "10", variable=OptVar, indicatoron=0, value=10).place(x=215, y=15)
opt4 = Radiobutton(self.mainWindow, text="Hexdecimal", width = "10", variable=OptVar, indicatoron=0, value=16)
opt4.place(x=315, y=15)
opt4.select()

#Buttons
cmdCalcular = Button(self.mainWindow,  text = "Calculate", width = "10", height = "4", command = lambda:Operate()).place(x=15, y=55)
cmdBaseConverter = Button(self.mainWindow,  text = "Base Convert", width = "10", height = "1", command = lambda:BaseConverter()).place(x=15, y=140)

#Labels
lbLine = Label(self.mainWindow, text="__________________________________", font=("Courier New", 8)).place(x=150, y=110)
lbInfo = Label(self.mainWindow, text="", font=("Courier New", 10), justify="left")
lbInfo.place(x=15, y=180)

#Start
self.mainWindow.mainloop()

def quit(self):
if messagebox.askyesno("Azav's Numeric Calculator", "Are you sure you want to quit?"):
self.mainWindow.destroy()
self.mainWindow.quit()
quit()


class BaseConverterGUI():
def __init__(self):
#Main Window
self.mainWindow = Tk()
self.mainWindow.geometry("1100x500")
self.mainWindow.title("Azav's Numeric Calculator")

#TextBoxs
for i in range(2, 14):
exec('self.txt' + str(i) + ' = Text(self.mainWindow, height = "0.6", width = "30", relief = "flat")')
exec('self.txt' + str(i) + '.place(x=90, y=' + str(30 * i) + ')')
for i in range(14, 26):
exec('self.txt' + str(i) + ' = Text(self.mainWindow, height = "0.6", width = "30", relief = "flat")')
exec('self.txt' + str(i) + '.place(x=460, y=' + str(30 * (i - 12)) + ')')
for i in range(26, 37):
exec('self.txt' + str(i) + ' = Text(self.mainWindow, height = "0.6", width = "30", relief = "flat")')
exec('self.txt' + str(i) + '.place(x=830, y=' + str(30 * (i - 24)) + ')')

self.txtBase = Text(self.mainWindow, height = "0.6", width = "10", relief = "flat")
self.txtBase.place(x=575, y=470)
self.txtBase.insert('1.0', '10')

def start(self):
def DoConversion():
self.ConvertBases()

#Labels
for i in range(2, 14):
if i == 2 or i == 8 or i == 10:
color = ', fg = "red"'
else:
color = ''
exec('lb' + str(i) + ' = Label(self.mainWindow' + color + ', text="Base ' + str(i) + ':", font=("Courier New", 10)).place(x=20, y=' + str(30 * i) + ')')
for i in range(14, 26):
if i == 16:
color = ', fg = "red"'
else:
color = ''
exec('lb' + str(i) + ' = Label(self.mainWindow' + color + ', text="Base ' + str(i) + ':", font=("Courier New", 10)).place(x=390, y=' + str(30 * (i - 12)) + ')')
for i in range(26, 37):
exec('lb' + str(i) + ' = Label(self.mainWindow, text="Base ' + str(i) + ':", font=("Courier New", 10)).place(x=760, y=' + str(30 * (i - 24)) + ')')
lbBase = Label(self.mainWindow, text="Base:", font=("Courier New", 10)).place(x=520, y=470)

#Buttons
cmdCalcular = Button(self.mainWindow,  text = "Calculate", width = "25", command = lambda:DoConversion()).place(x=500, y=440)

#Start
self.mainWindow.mainloop()

def ConvertBases(self):
local_var = locals()
BaseGuia = self.txtBase.get('1.0', 'end').strip()
try:
int(BaseGuia)
except:
messagebox.showwarning("Azav's Numeric Calculator", "Base must be a number.")
return
if int(BaseGuia) < 2 or int(BaseGuia) > 36:
messagebox.showwarning("Azav's Numeric Calculator", "Base must be a value beetween 2 and 36.")
return
exec("valor = self.txt" + BaseGuia + ".get('1.0', 'end').strip().upper()", globals(), local_var)
base_decimal = cambio_a_decimal(int(BaseGuia), local_var['valor'])
if base_decimal == "ERROR":
messagebox.showwarning("Azav's Numeric Calculator", local_var['valor'] + " is not valid value for base " + BaseGuia + ".")
return
else:
for i in range(2, 37):
exec("self.txt" + str(i) + ".delete('1.0', 'end')")
exec("self.txt" + str(i) + ".insert('1.0', cambio_de_base(i, base_decimal))")


GUI = Interfaz()
GUI.start()

Lo importante son las dos funciones del inicio, el resto es pura interfaz.

¡Espero le sirva a alguien!
#3
He escuchado que en Python el manejo de hilos es un poco enredado y que tampoco se lleva muy bien con tkinter, el problema que tengo es el siguiente: tengo una clase para la interfaz, y otra clase para un proceso cualquiera (es un proceso que se queda en un while infinito). Inicio primero la clase de la interfaz y luego la clase con el proceso infinito. Luego, mi idea es que cuando se cierre la interfaz, también se termine el proceso infinito y el programa termine. El código simplificado sería así:

Código: python
import time, threading
from tkinter import *
from tkinter import messagebox

finalizar = False

class Interfaz(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
global finalizar
#Main Window
self.mainWindow = Tk()
self.mainWindow.geometry("200x200")
self.mainWindow.title("My GUI Title")
#Label
lbCommand = Label(self.mainWindow, text="Hola mundo", font=("Courier New", 16)).place(x=20, y=20)
#Start
self.mainWindow.mainloop()
#Cuando se cierre la GUI seteamos finalizar a True
finalizar = True

class ClaseDos(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
global finalizar
while not finalizar:
print("Loop")
time.sleep(3)

GUI = Interfaz()
GUI.start()
Clase = ClaseDos()
Clase.start()


Cuando hago click en el botón cerrar (en la esquina superior derecha de la GUI) me lanza el siguiente error:

CitarTcl_AsyncDelete: async handler deleted by the wrong thread

No logro entender por qué este error, y en Google no encuentro mucha información útil. ¿Alguien sabe a qué se debe?

¡Saludos!
#4
Python / Azav Cipher Algorithm
Octubre 10, 2014, 12:38:57 AM
¡Buenas a todos!

Vengo a dejarles aquí un código para encriptar y desencriptar texto. Lo he llamado Azav Cipher, lo he creado completamente yo en Python 3.4

Características del cifrado:
+ No tiene una cantidad máxima de caracteres a encriptar.
+ No usa clave.
+ Para un mismo texto plano se generan siempre textos encriptados diferentes y no siempre de igual largo (lo hice así intencionalmente).
+ Desencripta perfecto cualquiera de los textos encriptados.
+ En esta segunda versión le he creado una GUI con tkinter, bastante simple pero ha quedado bien.

Código: python
from random import randint, choice
from tkinter import Tk, messagebox, Text, Button

def encode(string):
#Pasamos a binario el mensaje original:
binMSG = ""
i = 0
while i < len(string):
caracter_MSG_binario = bin(ord(string[i])).replace("0b", "").zfill(8)
binMSG = binMSG + caracter_MSG_binario
i += 1
#Dividimos el mensaje binario en dos cadenas de igual largo.
Segmento1 = binMSG[:(int(len(binMSG)/2))]
Segmento2 = binMSG[(int(len(binMSG)/2)):]
#A cada segmento agregamos una cadena aleotoria inicial para generar encriptaciones diferentes para un mismo mensaje.
Segmento1 = random_bin_chain(8) + Segmento1
Segmento2 = random_bin_chain(8) + Segmento2
# (0 = @), (1 = $), (1 = &), (2 = *)
i = 0
cadena_final = ""
while i < len(Segmento1):
if Segmento1[i] == Segmento2[i] == "0":
cadena_final += "@"
elif Segmento1[i] == Segmento2[i] == "1":
cadena_final += "$"
elif Segmento1[i] == "1" and Segmento2[i] == "0":
cadena_final += "&"
elif Segmento1[i] == "0" and Segmento2[i] == "1":
cadena_final += "*"
i += 1
#Transformamos los simbolos en caracteres normales (letras y numeros) y acortamos la cadena a la mitad.
simbolos = ["@", "$", "&", "*"]
valores = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.="
i = k = 0
while i < 4:
for elemento in simbolos:
match = elemento + simbolos[i]
coincidencias = cadena_final.count(match)
j = 1
while j <= coincidencias:
cadena_final = cadena_final.replace(match, choice(valores[k:k+4]), 1)
j += 1
k += 4
i += 1
return cadena_final
#El largo de la cadena final es siempre mayor a 2*(string + 2)
#El largo de la cadena final es siempre menor a (8/3)*(string + 2)

def decode(string):
simbolos = ["@", "$", "&", "*"]
valores = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.="
#Lo primero es obtener la cadena de simbolos ["@", "$", "&", "*"]
#Para ello transformamos las letras en simbolos.
listavalores = []
k = 0
while (k+4) <= len(valores):
listavalores.append(valores[k:k+4])
k += 4

i = k = 0
while i < 4:
for elemento in simbolos:
match = elemento + simbolos[i]
for caracter in listavalores[k]:
string = string.replace(caracter, match)
k += 1
i += 1
cadena_simbolos = string[8:]
#La cadena de simbolos representa la suma de los dos segmentos del mensaje
segmento1 = segmento2 = ""
for elemento in cadena_simbolos:
if elemento == "@":
segmento1 = segmento1 + "0"
segmento2 = segmento2 + "0"
elif elemento == "$":
segmento1 = segmento1 + "1"
segmento2 = segmento2 + "1"
elif elemento == "&":
segmento1 = segmento1 + "1"
segmento2 = segmento2 + "0"
elif elemento == "*":
segmento1 = segmento1 + "0"
segmento2 = segmento2 + "1"
cadena_binaria = segmento1 + segmento2
#Ahora queda transformar la cadena binaria a texto plano.
if (len(cadena_binaria) % 8) == 0:
repeticiones = (len(cadena_binaria) // 8)
j = k = 0
valor_original = ""
while k <= (repeticiones - 1):
segmento_8 = cadena_binaria[j:j + 8]
ascii_value = bin_to_ascii(segmento_8)
str_value = chr(ascii_value)
valor_original = valor_original + str(str_value)
j += 8
k += 1
else:
return ("Invalid text to decode.")
return valor_original

def random_bin_chain(large):
binary_chain = ""
for i in range(0, large):
binary_chain = binary_chain + str(randint(0, 1))
return binary_chain

def bin_to_ascii(bin_number):
    bin_number_str = str(bin_number)
    if len(bin_number_str) == 8:
        i = 0
        dec_number = 0
        while i <= 7:
            dec_number = dec_number + (int(bin_number_str[i]) * (2 ** (7 - i)))
            i += 1
        return dec_number
    else:
        return "Error decoding."

def load_GUI():
def EncodeClicked():
string = txtInput.get('1.0', 'end').strip()
if len(string) > 0:
result = encode(string)
txtInput.delete('1.0', 'end')
txtInput.insert('1.0', result)
else:
messagebox.showwarning("Azav Cipher","The plain text must contain at least 1 character.")

def DecodeClicked():
string = txtInput.get('1.0', 'end').strip()
if len(string) >= 4:
if decode(string) != "Invalid text to decode.":
result = decode(string)
txtInput.delete('1.0', 'end')
txtInput.insert('1.0', result)
else:
messagebox.showwarning("Azav Cipher","The encoded text is not valid to decode.")
else:
messagebox.showwarning("Azav Cipher","The encoded text must contain at least 4 characters.")

#Main Window
mainWindow = Tk()
mainWindow.geometry("520x270")
mainWindow.title("Azav Cipher")
#TextBox
txtInput = Text(mainWindow, height="12", width="60", relief = 'flat')
txtInput.place(x=20, y=20)
#Encode & Decode Buttons
cmdEncode = Button(mainWindow,  text = 'Encode', width = 30, command = lambda:EncodeClicked()).place(x=20, y = 230)
cmdEncode = Button(mainWindow,  text = 'Decode', width = 30, command = lambda:DecodeClicked()).place(x=285, y = 230)
#Start
mainWindow.mainloop()


load_GUI()


¡Espero a alguien le sirva!  ;)
#5
Presentaciones y cumpleaños / Me presento: Azav
Octubre 07, 2014, 11:45:58 PM
Escribo para presentarme, puede ser que algunos me conozcan como azav3, participo activamente en el foro indetectables pero también quise venir a darme una vuelta por aquí, hacer mis aportes y compartir con esta comunidad.

Entré al mundo del malware el año 2010, y aunque los estudios me mantienen un poco ocupado intento mantenerme al día. Soy de Chile, actualmente estudio Ingeniería y este es mi mayor hobby.

El año pasado ANTRAX me pidió ayuda para armar la Malware Data Base (No tienes permitido ver los links. Registrarse o Entrar a mi cuenta), me invitó a participar del foro pero justo después de eso estuve retirado del asunto, y ahora, un año y medio después estoy regresando. He vuelto entrar a mi skype (szudisch para quienes quieran agregarme) y aunque no he visto a ANTRAX, varios de mis amigos siguen ahí.

Bueno, era más o menos eso, un saludo a todos, espero formar parte de esta comunidad y de a poco ir conociendo a sus usuarios. :)