Beispiel #1
0
        public IActionResult Cifrarllaves([FromRoute] string nombre, [FromForm] IFormFile file, [FromForm] IFormFile filellave)
        {
            using var fileRead      = file.OpenReadStream();
            using var fileReadllave = filellave.OpenReadStream();
            try
            {
                RSA prueba = new RSA();
                using var fileWrite = new FileStream(nombre + ".txt", FileMode.OpenOrCreate);
                var          writer = new BinaryWriter(fileWrite);
                StreamReader leer   = new StreamReader(fileReadllave);
                int          n      = Convert.ToInt32(leer.ReadLine());
                int          eod    = Convert.ToInt32(leer.ReadLine());

                if (filellave.FileName.Contains("public.key"))
                {
                    using var reader = new BinaryReader(fileRead);
                    var           buffer   = new byte[2000];
                    List <byte[]> bytelist = new List <byte[]>();
                    int           size     = 0;
                    while (fileRead.Position < fileRead.Length)
                    {
                        buffer = reader.ReadBytes(2000);
                        foreach (var value in buffer)
                        {
                            //writer.Write(prueba.Manualbytetoint(prueba.CipherAndDecipher(value, eod, n)));
                            byte[] valor = prueba.CipherAndDecipher((int)value, eod, n);
                            bytelist.Add(valor);
                            if (valor.Length > size)
                            {
                                size = valor.Length;
                            }
                        }
                    }

                    writer.Write(size);
                    foreach (var number in bytelist)
                    {
                        int correccion = size - number.Length;
                        writer.Write(number);
                        while (correccion != 0)
                        {
                            writer.Write(BigInteger.Zero.ToByteArray());
                            correccion--;
                        }
                    }
                }

                if (filellave.FileName.Contains("private.key"))
                {
                    using var reader = new BinaryReader(fileRead);
                    int size   = BitConverter.ToInt32(reader.ReadBytes(4));
                    var buffer = new byte[1000 * size];

                    while (fileRead.Position < fileRead.Length)
                    {
                        byte[] number     = reader.ReadBytes(size);
                        int    deciphered = prueba.Manualbytetoint(number);
                        byte[] final      = prueba.CipherAndDecipher(deciphered, eod, n);
                        writer.Write(final);
                    }
                }
                writer.Close();
                leer.Close();
                fileWrite.Close();
                var files = System.IO.File.OpenRead(nombre + ".txt");
                return(new FileStreamResult(files, "application/txt")
                {
                    FileDownloadName = nombre + ".txt"
                });
            }

            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }