Esempio 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));
     }
 }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            string
                fileNameIn  = "zo_attack1.wav"
            , fileNameInOut = "temp.wav"
            , fileNameOut   = "!zo_attack1.wav";

            RSA rsa = new RSA();

            FileStream inputFileStream, outputFileStream;

            // szyfrowanie
            using (inputFileStream = new FileStream(@fileNameIn, FileMode.Open))
            {
                using (outputFileStream = new FileStream(@fileNameInOut, FileMode.Create))
                {
                    byte[] inputBytes = new byte[rsa.MaxCountOfBytes];
                    inputBytes[0] = 0x80;

                    int nuberOfBytes;
                    while ((nuberOfBytes = inputFileStream.Read(inputBytes, 1, rsa.MaxCountOfBytes - 1)) != 0)
                    {
                        BigInteger inputBlock    = new BigInteger(inputBytes, nuberOfBytes + 1);
                        BigInteger cipheredBlock = rsa.Cipher(inputBlock);

                        BigInteger encipheredBlock = rsa.Encipher(new BigInteger(cipheredBlock.getBytes(), cipheredBlock.getBytes().Length));

                        if (!encipheredBlock.Equals(inputBlock))
                        {
                            throw new Exception();
                        }

                        outputFileStream.WriteByte((byte)(nuberOfBytes));
                        outputFileStream.WriteByte((byte)(cipheredBlock.getBytes().Length));
                        outputFileStream.Write(cipheredBlock.getBytes(), 0, cipheredBlock.getBytes().Length);
                    }
                }
            }

            // deszyfrowanie
            using (inputFileStream = new FileStream(@fileNameInOut, FileMode.Open))
            {
                using (outputFileStream = new FileStream(@fileNameOut, FileMode.Create))
                {
                    byte[] inputBytes = new byte[rsa.N.getBytes().Length];

                    do
                    {
                        int plainTextBytes = inputFileStream.ReadByte();
                        if (plainTextBytes == -1)
                        {
                            break;
                        }

                        int cipheredBytesLenght = inputFileStream.ReadByte();

                        BigInteger inputBlock      = inputFileStream.Read(inputBytes, 0, cipheredBytesLenght);
                        BigInteger encipheredBlock = rsa.Encipher(inputBlock);
                        outputFileStream.Write(encipheredBlock.getBytes(), 1, plainTextBytes);
                    }while (true);
                }
            }
        }