public ActionResult Decifrado(string KeyFile, string CipherFile) { CifradoRSA decifrado = new CifradoRSA(); var ValorMax = 0; var diccionario = new Dictionary <int, char>(); var ByteList = decifrado.LecuraCipherFileDecifrado(CipherFile, bufferLengt, ref ValorMax, ref diccionario, nombreArchivo, RutaArchivos); ByteList.Remove(ByteList[0]); var KeyList = decifrado.LecuraKeyFile(KeyFile, bufferLengt); var Key = KeyList.Substring(0, KeyList.IndexOf(",")); var N = KeyList.Substring(Key.Length + 1); var BinaryList = new List <byte>(); var binary = string.Empty; var Auxiliar = string.Empty; foreach (byte bit in ByteList) { binary += Convert.ToString(Convert.ToInt32(bit), 2); binary = binary.PadLeft(8, '0'); foreach (char caracter in binary) { Auxiliar += caracter; if (Auxiliar.Count() == ValorMax) { var value = Convert.ToInt32(Auxiliar, 2); var returnbyte = decifrado.Decifrar(value, Convert.ToInt32(Key), Convert.ToInt32(N), diccionario); BinaryList.Add(returnbyte); Auxiliar = string.Empty; } } binary = string.Empty; } decifrado.EscrituraArchivoDecifrado(BinaryList, RutaArchivos, nombreArchivo); return(RedirectToAction("Download")); }
// GET: FileUpload public ActionResult Index() { //La vista que me va a mostrar todos los archivos que ya se han subido var items = FilesUploaded(); CifradoRSA rsa = new CifradoRSA(); return(View(items)); }
static void Main(string [] args) { byte [] clave_publica; byte [] clave_privada; CifradoRSA.GeneraParClaves(out clave_publica, out clave_privada); // ImprimeCarga("clave_publica", clave_publica); ImprimeCarga("clave_privada", clave_privada); }
public ActionResult GenerarLlaves(RSAViewModel rsa) { try { if (ModelState.IsValid && rsa.ValorP > 0 && rsa.ValorQ > 0) { if (rsa.ValorP == 1 || rsa.ValorQ == 1) { return(View(rsa)); } else { var primo1 = numeroPrimo(rsa.ValorP, 2); var primo2 = numeroPrimo(rsa.ValorQ, 2); if (primo1 == true && primo2 == true) { if (rsa.ValorP * rsa.ValorQ < 255) { ViewBag.Message = "Please verify the multiplication of p*q is greater than 256"; return(View(rsa)); } else { FilePath = Server.MapPath("~/Archivo"); CifradoRSA RSA = new CifradoRSA(); RSA.GenerarLlaves(rsa.ValorP, rsa.ValorQ, FilePath); return(RedirectToAction(nameof(LlavesGeneradas))); } } else { ViewBag.Message = "Please verify the numbers are prime"; return(View(rsa)); } } } else { ViewBag.Message = "Please verify the number is greater than 0"; return(View(rsa)); } } catch { return(View()); } }
public ActionResult Cifrado(string KeyFile, string CipherFile) { CifradoRSA cifrado = new CifradoRSA(); var diccionario = new Dictionary <char, int>(); var ByteList = cifrado.LecuraCipherFile(CipherFile, bufferLengt, ref diccionario); var KeyList = cifrado.LecuraKeyFile(KeyFile, bufferLengt); var Key = KeyList.Substring(0, KeyList.IndexOf(",")); var N = KeyList.Substring(Key.Length + 1); var BinaryList = new List <string>(); var ValorMax = 0; foreach (byte bit in ByteList) { string binario = cifrado.Cifrar(Convert.ToInt32(bit), Convert.ToInt32(Key), Convert.ToInt32(N), ref ValorMax); BinaryList.Add(binario); } if (ValorMax < 8) { ValorMax = 8; } ByteList = new List <byte>(); var Auxiliar = string.Empty; var binary = string.Empty; ByteList.Add(Convert.ToByte(ValorMax)); foreach (string binario in BinaryList) { binary += binario.PadLeft(ValorMax, '0'); foreach (char caracter in binary) { Auxiliar += caracter; if (Auxiliar.Count() == 8) { byte bit = Convert.ToByte(Convert.ToInt32(Auxiliar, 2)); ByteList.Add(bit); Auxiliar = string.Empty; } } binary = string.Empty; } cifrado.EscrituraArchivoCifrado(ByteList, RutaArchivos, nombreArchivo, diccionario); return(RedirectToAction("Download")); }
public ActionResult GenerarLlaves() { var p = Convert.ToInt32(Request.Form["p"]); var q = Convert.ToInt32(Request.Form["q"]); //se comprueba que los numeros ingresados sean primos bool pPrimo = ValidarPrimo(p); bool qPrimo = ValidarPrimo(q); //se valida que ambos sean primos if (pPrimo && qPrimo) { var N = p * q; var phi = (p - 1) * (q - 1); CifradoRSA RSA = new CifradoRSA(); var e = RSA.GenerarLlavePublica(phi, p, q); //aun falta implementar funcion para privada var Ivalue = 1; var d = RSA.GenerarLlavePrivada(phi, phi, e, Ivalue, phi); //generar el archivo de texto var folder = Server.MapPath("~/Files/"); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); var filePath = Path.Combine(folder, "PublicKey.key"); using (var writeStream = new FileStream(filePath, FileMode.Create)) { using (var writer = new BinaryWriter(writeStream)) { writer.Write(e); writer.Write(','); writer.Write(N); } } filePath = Path.Combine(folder, "PrivateKey.key"); using (var writeStream = new FileStream(filePath, FileMode.Create)) { using (var writer = new BinaryWriter(writeStream)) { writer.Write(d); writer.Write(','); writer.Write(N); } } } else { var filePath = Path.Combine(folder, "PublicKey.key"); using (var writeStream = new FileStream(filePath, FileMode.Create)) { using (var writer = new BinaryWriter(writeStream)) { writer.Write(Convert.ToByte(e)); writer.Write(','); writer.Write(Convert.ToByte(N)); } } filePath = Path.Combine(folder, "PrivateKey.key"); using (var writeStream = new FileStream(filePath, FileMode.Create)) { using (var writer = new BinaryWriter(writeStream)) { writer.Write(Convert.ToByte(d)); writer.Write(','); writer.Write(Convert.ToByte(N)); } } } ViewBag.Primo = 1; return(RedirectToAction("DownloadKey")); } else { //0 representa que uno o los dos numeros no son primos //esto funcionara para activar un script en la vista ViewBag.Primo = 0; return(View("GenerarClaves")); } }
public FileResult Upload() { var file = Request.Form.Files[0]; string type_cipher = Request.Form["tipo"]; int BufferLength = 100; //lectura archivo var folderName = Path.Combine("Resources", "Files"); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (!Directory.Exists(pathToSave)) { Directory.CreateDirectory(pathToSave); } string extension = Path.GetExtension(file.FileName); var buffer = new byte[100]; if (extension == ".txt") { if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); string file_nameN = "cifrado.txt"; string pathToSave1 = Path.Combine(pathToSave, "Archivos"); //Carpeta donde estarán los archivos que devuelve cesar string pathToSaveK = Path.Combine(pathToSave, "Keys"); //Carpeta donde estarán los archivos de las llaves //Archivos que devuelve cesar string fullPathC = Path.Combine(pathToSave1, file_nameN); string PathKey = Path.Combine(pathToSave1, "key.txt"); if (Directory.Exists(pathToSave1)) { FileInfo f = new FileInfo(fullPathC); if (f.Exists) { f.Delete(); } f = new FileInfo(PathKey); if (f.Exists) { f.Delete(); } } else { Directory.CreateDirectory(pathToSave1); } //Archivos con las llaves string PathPubl = Path.Combine(pathToSaveK, "public.txt"); string PathPriv = Path.Combine(pathToSaveK, "private.txt"); if (Directory.Exists(pathToSaveK)) { FileInfo f = new FileInfo(PathPubl); f.Delete(); f = new FileInfo(PathPriv); f.Delete(); } else { Directory.CreateDirectory(pathToSaveK); } string clave = Request.Form["clave"]; int cl = int.Parse(clave); Cesar2 cif = new Cesar2(); cif.LlenarDiccionarios(); if (type_cipher == "rsa") { //clave mayor a 26 if (cl > 25) { int n = -(26 - cl); cl = n; } cif.LlenarClaveInt(cl); using (var stream = new FileStream(fullPathC, FileMode.Create)) { //ya se creó el archivo } using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } using (var stream = new FileStream(fullPath, FileMode.Open)) { using (BinaryReader br = new BinaryReader(stream)) { while (br.BaseStream.Position != br.BaseStream.Length) { buffer = br.ReadBytes(BufferLength); //llenar el buffer cif.Cifrar(buffer); cif.EscribirCifrado(fullPathC); } } } string auxp = Request.Form["p"]; string auxq = Request.Form["q"]; int p = int.Parse(auxp); int q = int.Parse(auxq); CifradoRSA cifa = new CifradoRSA(); if (cifa.VerificarPrimo(p) && cifa.VerificarPrimo(q)) { cifa.p = p; cifa.q = q; cifa.n = (cifa.p) * (cifa.q); //generando N cifa.z = (cifa.p - 1) * (cifa.q - 1); //Generando phu cifa.GenerandoE(); //cifa.e = 17; cifa.CalcularD(); cifa.Cifrar(PathKey, cl); using (var stream = new FileStream(PathPubl, FileMode.Create)) { //ya se creó el archivo } using (var stream = new FileStream(PathPriv, FileMode.Create)) { //ya se creó el archivo } cifa.EscribirArchPriv(PathPriv); cifa.EscribirArchPubl(PathPubl); // cifa string pathToSaveZip = Path.Combine(Directory.GetCurrentDirectory(), "Comprimido"); string archzip = Path.Combine(pathToSaveZip, "archivos.zip"); if (Directory.Exists(pathToSaveZip)) { FileInfo f = new FileInfo(archzip); f.Delete(); } else { Directory.CreateDirectory(pathToSaveZip); } ZipFile.CreateFromDirectory(pathToSave1, archzip); var stream1 = new FileStream(archzip, FileMode.Open); return(File(stream1, System.Net.Mime.MediaTypeNames.Application.Octet, "archivo.zip")); } else { return(null); } } else if (type_cipher == "diffie") { string auxB = Request.Form["B"]; Diffie cifa = new Diffie(); cifa.priv = Convert.ToInt32(cl); cifa.publ = Convert.ToInt32(auxB); cifa.CalcularPubl(); cl = cifa.k; //clave mayor a 26 if (cl > 25) { int n = -(25 - cl); cl = n; } cif.LlenarClaveInt(cl); using (var stream = new FileStream(fullPathC, FileMode.Create)) { //ya se creó el archivo } using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } using (var stream = new FileStream(fullPath, FileMode.Open)) { using (BinaryReader br = new BinaryReader(stream)) { while (br.BaseStream.Position != br.BaseStream.Length) { buffer = br.ReadBytes(BufferLength); //llenar el buffer cif.Cifrar(buffer); cif.EscribirCifrado(fullPathC); } } } cifa.EscribirArch(PathPubl, PathPriv); var stream1 = new FileStream(fullPathC, FileMode.Open); return(File(stream1, System.Net.Mime.MediaTypeNames.Application.Octet, "cifrado.txt")); } else { return(null); } } else { return(null); } } else { return(null); } }
public FileResult Upload() { var file = Request.Form.Files[0]; string type_cipher = Request.Form["tipo"]; int BufferLength = 100; //lectura archivo var folderName = Path.Combine("Resources", "Files"); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (!Directory.Exists(pathToSave)) { Directory.CreateDirectory(pathToSave); } string extension = Path.GetExtension(file.FileName); var buffer = new byte[100]; if (extension == ".txt") { if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); string file_nameN = "descifrado.txt"; string pathToSave1 = Path.Combine(pathToSave, "Archivos"); //Carpeta donde estarán los archivos que devuelve cesar //Archivos que devuelve cesar string fullPathC = Path.Combine(pathToSave1, file_nameN); if (Directory.Exists(pathToSave1)) { FileInfo f = new FileInfo(fullPathC); if (f.Exists) { f.Delete(); } } else { Directory.CreateDirectory(pathToSave1); } string clave = Request.Form["clave"]; int cl = int.Parse(clave); if (type_cipher == "rsa") { string auxe = Request.Form["e"]; string auxn = Request.Form["n"]; CifradoRSA cifa = new CifradoRSA(); cifa.e = Convert.ToInt32(auxe); cifa.n = Convert.ToInt32(auxn); cl = cifa.DesCifrar(cl); } else if (type_cipher == "diffie") { string auxB = Request.Form["B"]; Diffie cifa = new Diffie(); cifa.priv = Convert.ToInt32(cl); cifa.publ = Convert.ToInt32(auxB); cifa.CalcularPubl(); cl = cifa.k; } else { return(null); } Cesar2 cif = new Cesar2(); cif.LlenarDiccionarios(); //clave mayor a 26 if (cl > 25) { int n = -(25 - cl); cl = n; } cif.LlenarClaveInt(cl); using (var stream = new FileStream(fullPathC, FileMode.Create)) { //ya se creó el archivo } using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } using (var stream = new FileStream(fullPath, FileMode.Open)) { using (BinaryReader br = new BinaryReader(stream)) { while (br.BaseStream.Position != br.BaseStream.Length) { buffer = br.ReadBytes(BufferLength); //llenar el buffer cif.DesCifrar(buffer); cif.EscribirCifrado(fullPathC); } } } var stream1 = new FileStream(fullPathC, FileMode.Open); return(File(stream1, System.Net.Mime.MediaTypeNames.Application.Octet, "archivo.zip")); } else { return(null); } } else { return(null); } }
[HttpPost] //Recibo un archivo public ActionResult Cifrar(HttpPostedFileBase file1, HttpPostedFileBase file2) { var path1 = ""; var path2 = ""; var fileName = ""; //Valido que no sea nulo y que contenga texto, ya que valido que el archivo pese. if (file1 != null && file1.ContentLength > 0) { try { //Valido que unicamente puedan cargar archivos de texto if (Path.GetExtension(file1.FileName) == ".txt") { //Me va a devolver la ruta en la que se encuentra la carpeta "Archivos" path1 = Path.Combine(Server.MapPath("~/Archivo"), //Toma el nombre del archivo Path.GetFileName(file1.FileName)); //Entonces path, va a ser igual a la ruta + el nombre del archivo // file1.SaveAs(path1); //Guarda el archivo en la carpeta "Archivos" ViewBag.Message = "File uploaded"; fileName = file1.FileName; } } catch { ViewBag.Message = "Invalid file, please upload a .txt"; } } else { ViewBag.Message = "Please upload a file"; } //Valido que no sea nulo y que contenga texto, ya que valido que el archivo pese. if (file2 != null && file2.ContentLength > 0) { try { //Valido que unicamente puedan cargar archivos de texto if (Path.GetExtension(file2.FileName) == ".Key") { //Me va a devolver la ruta en la que se encuentra la carpeta "Archivos" path2 = Path.Combine(Server.MapPath("~/Archivo"), //Toma el nombre del archivo Path.GetFileName(file2.FileName)); //Entonces path, va a ser igual a la ruta + el nombre del archivo file2.SaveAs(path2); //Guarda el archivo en la carpeta "Archivos" ViewBag.Message = "File uploaded"; FilePath = Server.MapPath("~/Archivo"); CifradoRSA rsa = new CifradoRSA(); rsa.LeerTxt(path1, path2, FilePath, fileName); return(RedirectToAction(nameof(ArchivoCifrado))); } } catch { ViewBag.Message = "Invalid file, please upload a .key"; } } else { ViewBag.Message = "Please upload a file"; } return(View()); }