Exemplo n.º 1
0
 public async Task <ActionResult> Cipher(IFormFile file, IFormFile key, string name)
 {
     try
     {
         string basePath  = _env.ContentRootPath;
         string TempFile  = basePath + @"\Temp\temp.txt";
         string TempFile2 = basePath + @"\Temp\temp_key.txt";
         RSAkey Key       = new RSAkey();
         byte[] FileBytes;
         if (key.FileName.Substring(key.FileName.Length - 3, 3) != "key")
         {
             return(StatusCode(500));
         }
         using (FileStream fs = System.IO.File.Create(TempFile2))
         {
             await key.CopyToAsync(fs);
         }
         using (StreamReader reader = new StreamReader(TempFile2))
         {
             string   base_string    = reader.ReadToEnd();
             string[] Key_Attributes = base_string.Split("|");
             Key.modulus = BigInteger.Parse(Key_Attributes[0]);
             Key.power   = BigInteger.Parse(Key_Attributes[1]);
         }
         using (FileStream fs = System.IO.File.Create(TempFile))
         {
             await file.CopyToAsync(fs);
         }
         RSA Cipher = new RSA();
         if (file.FileName.Substring(file.FileName.Length - 3, 3) == "rsa")
         {
             if (Cipher.Decipher(TempFile, out FileBytes, Key) == false)
             {
                 return(StatusCode(500));
             }
             return(File(FileBytes, "text/plain", name + ".txt"));
         }
         else
         {
             if (Cipher.Cipher(TempFile, out FileBytes, Key) == false)
             {
                 return(StatusCode(500));
             }
             return(File(FileBytes, "text/plain", name + ".rsa"));
         }
     }
     catch (Exception)
     {
         return(StatusCode(500));
     }
 }