0

Hola he hecho un pequeño programa que basicamente encripta cualquier fichero el codigo es el siguiente:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;

namespace Crypter
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            //No Arguments -> Exit
            if (args.Length < 2)
            {
                Console.WriteLine("Syntax: crypter.exe <Exe/Dll to get Encrypted> <Password> (Optional: output file name)");
                Environment.Exit(0);
            }

            String file = args[0];
            String pass = args[1];
            String outFile = "Crypted.exe";

            //If Output Name is specified -> Set it
            if (args.Length == 3)
            {
                outFile = args[2];
            }

            //File doesn't exist -> Exit
            if (!File.Exists(file))
            {
                Console.WriteLine("[!] The selected File doesn't exist!");
                Environment.Exit(0);
            }

            //Everything seems fine -> Reading bytes
            Console.WriteLine("[*] Reading Data...");
            byte[] plainBytes = File.ReadAllBytes(file);

            //Yep, got bytes -> Encoding
            Console.WriteLine("[*] Encoding Data...");
            byte[] encodedBytes = encodeBytes(plainBytes, pass);

            Console.Write("[*] Save to Output File... ");
            File.WriteAllBytes(outFile, encodedBytes);
            Console.WriteLine("Done!");

            Console.WriteLine("\n[*] File successfully encoded!");
        }
        private static byte[] encodeBytes(byte[] bytes, String pass)
        {
        byte[] XorBytes = Encoding.Unicode.GetBytes(pass);

        for (int i = 0; i < bytes.Length; i++)
        {
            bytes[i] ^= XorBytes[i % 16];
        }

        return bytes;
        }
    }
}

Cuando inserto el fichero.txt me devuelve: introducir la descripción de la imagen aquí

Mi pregunta es a que se deba esta excepción. Y segundo que parte del codigo es la causante ya que de momento no he dado con la solución.

El error que me devuelve es:

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bou
ds of the array.
   at Crypter.Program.encodeBytes(Byte[] bytes, String pass)
   at Crypter.Program.Main(String[] args)
4
  • En vez de i % 16 prueba con i & XorBytes.Length en el index de XorBytes, pues estas accediendo a un indice que no existe...
    – NaCl
    el 29 mar. 2017 a las 22:01
  • me devuelve lo mismo Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bou ds of the array. el 29 mar. 2017 a las 22:12
  • Era como la respuesra de abajo :P, mirala para que te ayudes.
    – NaCl
    el 29 mar. 2017 a las 22:23
  • si funciono gracias fue un error por mi parte grave.. el 29 mar. 2017 a las 22:24

1 respuesta 1

3

El error (considero) de la lectura del código ocurre en encodeBytes

<!-- languaje: c# -->
byte[] XorBytes = Encoding.Unicode.GetBytes(pass);

Para el caso usado tiene no más de 4 caracteres : Crypter.exe a.txt 1234 o.txt

en el for tu:

<!-- languaje: c# -->
bytes[i] ^= XorBytes[i % 16];

debe ser

<!-- languaje: c# -->
bytes[i] ^= XorBytes[i % XorBytes.Length];
1
  • si funciono gracias fue un error por mi parte grave.. el 29 mar. 2017 a las 22:23

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.