Exemplo n.º 1
0
        public ActionResult GenerarLlaver(int p, int q)
        {
            RSAprocess   llaves           = new RSAprocess();
            SubirArchivo obtencionLllaves = new SubirArchivo();

            obtencionLllaves.P = p;
            obtencionLllaves.Q = q;
            string key = (Server.MapPath(@"~\LlavesGeneradas\"));

            llaves.GenerandoLlaves(p, q, key);

            return(View());
        }
Exemplo n.º 2
0
        public void lecturasRSA(string rutallave, string rutatexto, int cif)
        {
            RSAprocess rsa            = new RSAprocess();
            var        byteBufferKey  = new byte[bufferLength];
            var        byteBufferText = new byte[bufferLength];

            using (var stream = new FileStream(rutallave, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        byteBufferKey = reader.ReadBytes(bufferLength);
                    }
                }
            }

            string converted = Encoding.ASCII.GetString(byteBufferKey, 0, byteBufferKey.Length);

            string[] llaves = converted.Split(',');

            using (var stream = new FileStream(rutatexto, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        byteBufferText = reader.ReadBytes(bufferLength);
                    }
                }
            }

            var    nuevoCifrado = new List <byte>();
            string ruta         = string.Empty;

            string[] path = rutatexto.Split('.');
            if (cif == 1)
            {
                ruta = path[0] + ".rsacif";
                for (int i = 0; i < byteBufferText.Length; i++)
                {
                    var byteInicial = rsa.CifrandoRSA(byteBufferText[i], Convert.ToInt32(llaves[0]), Convert.ToInt32(llaves[1]));
                    nuevoCifrado.Add(Convert.ToByte(rsa.CifrandoRSACantByte(byteInicial)));
                    nuevoCifrado.Add(Convert.ToByte(byteInicial % 256));
                }
            }
            else if (cif == 2)
            {
                ruta = path[0] + "descif.txt";
                for (var i = 0; i < byteBufferText.Length; i += 2)
                {
                    var cantidadBytes = Convert.ToInt32(byteBufferText[i]) * 256;
                    var resto         = Convert.ToInt32(byteBufferText[i + 1]);
                    var numDescifrar  = cantidadBytes + resto;
                    nuevoCifrado.Add(Convert.ToByte(rsa.DescifrandoRSA(numDescifrar, Convert.ToInt32(llaves[0]), Convert.ToInt32(llaves[1]))));
                }
            }

            using (var writeStream1 = new FileStream(ruta, FileMode.OpenOrCreate))
            {
                using (var writer = new BinaryWriter(writeStream1))
                {
                    foreach (var caracter in nuevoCifrado)
                    {
                        writer.Write(caracter);
                    }
                }
            }
        }