Exemplo n.º 1
0
        private void encryptBtn(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Controllers.KeyController kc = new Controllers.KeyController();

                string fileName;
                byte[] plainBytes;
                fileName = dlg.FileName; //Will be useful in the future when selecting files

                plainBytes = File.ReadAllBytes(@fileName);
                Console.WriteLine("Getting bytes of file");
                byte[] IV = File.ReadAllBytes(@"C:\\Users\\SengokuMedaru\\Desktop\\keys\\IV.txt");
                Console.WriteLine("Getting bytes of IV");
                byte[] encryptedSymmetricKey = File.ReadAllBytes(@"C:\\Users\\SengokuMedaru\\Desktop\\keys\\encryptedSymmetricKey.txt");
                Console.WriteLine("Getting bytes of encrypted symmetric key");

                //Gets the symmetric key by decrypting the encrypted symmetric key with the decryption (private) key
                byte[] decryptedSymmetricKey = kc.asymmetricDecryption(encryptedSymmetricKey);
                //Encrypts plaintext with symmetric key
                byte[] cipherText = kc.symmetricEncryption(plainBytes, decryptedSymmetricKey, IV);

                //For debugging purposes
                fileName = System.IO.Path.GetFileNameWithoutExtension(fileName);
                byte[] testOutput = cipherText;
                System.IO.File.WriteAllBytes(@"C:\\Users\\SengokuMedaru\\Desktop\\EncryptedText\\encrypted_" + fileName, testOutput);
                Console.WriteLine(fileName + " has successfully been encrypted!");
                Console.WriteLine("");
            }
        }
Exemplo n.º 2
0
        private void uploadButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Controllers.KeyController kc = new Controllers.KeyController();

                string fileName;
                fileName  = dlg.FileName; //Will be useful in the future when selecting files
                filename1 = fileName;
                byte[] byteFormatOfFile = File.ReadAllBytes(@fileName);



                //Hash Computation
                SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                byte[] byteComputedHash;

                using (FileStream stream = File.OpenRead(fileName))
                {
                    SHA1Managed sha = new SHA1Managed();
                    byteComputedHash = sha.ComputeHash(stream);
                }
                Controllers.HashController hashController = new Controllers.HashController();
                stringComputedHash = hashController.HexStringFromBytes(byteComputedHash).ToUpper();

                //Important line to ensure user inputted hash stays at no more than 40 chars
                hash = stringUserHashInput(userHashInput);
                Console.WriteLine(hash);



                //Actions to take if hash not equals to computed hash
                if (!(hash.Equals(stringComputedHash)))
                {
                    //Do stuff:
                    //Stuff like an error message box pop up (done)
                    //Clear the userInputHash box (done)
                    //Break from the current method (done)
                    Controllers.Prompt.ShowDialog1("Hash does not match!", "Error");
                    userHashInput.Document.Blocks.Clear();
                }

                else
                {
                    byte[] IV = System.IO.File.ReadAllBytes(@"C:\\Users\\SengokuMedaru\\Desktop\\keys\\IV.txt");
                    Console.WriteLine("Gets bytes of IV");
                    byte[] encryptedSymmetricKey = System.IO.File.ReadAllBytes(@"C:\\Users\\SengokuMedaru\\Desktop\\keys\\encryptedSymmetricKey.txt");

                    //Gets the symmetric key by decrypting the encrypted symmetric key with the decryption (private) key
                    byte[] decryptedSymmetricKey = kc.asymmetricDecryption(encryptedSymmetricKey);
                    //Encrypts plaintext with symmetric key
                    byte[] cipherText = kc.symmetricEncryption(byteFormatOfFile, decryptedSymmetricKey, IV);

                    //For debugging purposes
                    fileName = System.IO.Path.GetFileNameWithoutExtension(fileName);
                    byte[] testOutput = cipherText;
                    System.IO.File.WriteAllBytes(@"C:\\Users\\SengokuMedaru\\Desktop\\EncryptedText\\encrypted_" + fileName, testOutput);
                    Console.WriteLine(fileName + " has successfully been encrypted!");
                    Console.WriteLine("");

                    //Bryan code below

                    ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["connString"];
                    string     connectionString          = conSettings.ConnectionString;
                    string     VirusName;
                    string     type;
                    SqlCommand cmd5;

                    String imgLocation = "";
                    int    virus       = 0;
                    byte[] checkMD5;

                    path = filename1;
                    Console.Write(path);

                    byte[] md5HashBytes = ComputeMd5Hash(path);

                    String strMd5 = ToHexadecimal(md5HashBytes);

                    Console.Write(strMd5);


                    con = new SqlConnection(connectionString);
                    con.Open();
                    // cmd5 = new SqlCommand("SELECT vxVirusName, vxType FROM [User].[dbo].[vx] WHERE vxMD5 = " + checkMD5 + " ; ", con);
                    cmd5 = new SqlCommand("SELECT vxVirusName, vxType,vxMD5,BYTE FROM [User].[dbo].[vx] WHERE vxMD5 =  @checkMD5 ", con);

                    cmd5.Parameters.Add(new SqlParameter("@checkMD5", strMd5));



                    Console.Write(strMd5);

                    SqlDataReader reader1 = cmd5.ExecuteReader();
                    if (reader1.Read() == true)
                    {
                        VirusName = reader1.GetString(0);
                        type      = reader1.GetString(1);
                        System.Windows.MessageBox.Show(" \n Virus Dected : " + VirusName + " | Type Of Virus : " + type);
                        Console.WriteLine(" \n Virus Dected : " + VirusName + " | Type Of Virus : " + type);
                        con.Close();
                        virus = virus + 1;
                    }
                    else
                    {
                        con.Close();

                        con.Open();

                        string finalPath = fullPath();

                        FileStream   Stream = new FileStream(finalPath, FileMode.Open, FileAccess.Read);
                        BinaryReader brs    = new BinaryReader(Stream);
                        byte[]       images = brs.ReadBytes((int)Stream.Length);

                        String strImage = System.Text.Encoding.UTF8.GetString(images);


                        cmd2 = new SqlCommand("SELECT vxVirusName, vxType,vxMD5,BYTE FROM [User].[dbo].[vx] WHERE tID = 5002 ", con);

                        //  cmd2.Parameters.Add(new SqlParameter("@BYTE", strImage));

                        SqlDataReader reader2 = cmd2.ExecuteReader();

                        if (reader2.Read() == true)
                        {
                            if (strImage.Contains(reader2.GetString(3)))
                            {
                                virus     = virus + 1;
                                VirusName = reader2.GetString(0);
                                type      = reader2.GetString(1);
                                System.Windows.MessageBox.Show("\n VIRUS DECTED!Your File is not being Uploaded \n Virus Dected : " + VirusName + " | Type Of Virus : " + type);
                                Console.WriteLine(" \n VIRUS DECTED! Your File is not being Uploaded");
                                Console.WriteLine(" \n Virus Dected : " + VirusName + " | Type Of Virus : " + type);
                            }
                            else
                            {
                                Console.WriteLine("\n There is no virus! Very Good!! ");
                                Console.WriteLine("\n File is now being uploaded......");
                                System.Windows.MessageBox.Show("\n File is now being uploaded......");
                            }
                        }
                        else
                        {
                            Console.WriteLine("\n There is no virus! Very Good!! ");
                            Console.WriteLine("\n File is now being uploaded......");
                        }
                        con.Close();
                    }



                    if (virus == 0)
                    {
                        ConnectionStringSettings conSettings1 = ConfigurationManager.ConnectionStrings["connString1"];
                        ConnectionStringSettings conSettings2 = ConfigurationManager.ConnectionStrings["connString2"];
                        ConnectionStringSettings conSettings3 = ConfigurationManager.ConnectionStrings["connString3"];

                        string connectionString1 = conSettings1.ConnectionString;
                        string connectionString2 = conSettings2.ConnectionString;
                        string connectionString3 = conSettings3.ConnectionString;

                        imgLocation = dlg.FileName.ToString();
                        con         = new SqlConnection(connectionString);
                        con1        = new SqlConnection(connectionString1);
                        con2        = new SqlConnection(connectionString2);
                        con3        = new SqlConnection(connectionString3);
                        byte[] images  = null;
                        byte[] images1 = null;
                        byte[] images2 = null;
                        byte[] images3 = null;

                        BitArray b1;

                        FileStream   Stream = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
                        BinaryReader brs    = new BinaryReader(Stream);
                        images = brs.ReadBytes((int)Stream.Length);

                        String strImage = System.Text.Encoding.UTF8.GetString(images);
                        // Console.Write(strImage);
                        int leng = images.Length / 3;
                        Console.Write("Length : " + leng);
                        images1 = images.Take(leng).ToArray();
                        // images1 = strImage.Substring(1,leng);


                        images2 = images.Skip(leng).Take(leng).ToArray();
                        int leng2 = leng + leng;
                        images3 = images.Skip(leng2).Take(leng).ToArray();
                        con.Open();
                        con1.Open();
                        con2.Open();
                        con3.Open();

                        //BitArray bits1 = new BitArray(images1);
                        //  BitArray bits2 = new BitArray(images2);
                        // BitArray bits3 = new BitArray(images3);

                        string sqlQuery = "Insert into dbo.UserFiles(Username,Name,Image)Values( 'superman' , 'man' , @images )";
                        cmd = new SqlCommand(sqlQuery, con);
                        cmd.Parameters.Add(new SqlParameter("@images", images));
                        cmd.ExecuteNonQuery();

                        string sqlQuery1 = "Insert into dbo.UserFiles1(Username,Name,Image)Values( '123456' , 'man' , @images1 )";
                        cmd = new SqlCommand(sqlQuery1, con1);
                        cmd.Parameters.Add(new SqlParameter("@images1", images1));
                        cmd.ExecuteNonQuery();

                        string sqlQuery2 = "Insert into dbo.UserFiles3(Username,Name,Image)Values( '123456' , 'man' , @images2 )";
                        cmd = new SqlCommand(sqlQuery2, con2);
                        cmd.Parameters.Add(new SqlParameter("@images2", images3));
                        cmd.ExecuteNonQuery();

                        string sqlQuery3 = "Insert into dbo.UserFiles2(Username,Name,Image)Values( '123456' , 'man' , @images3 )";
                        cmd = new SqlCommand(sqlQuery3, con3);
                        cmd.Parameters.Add(new SqlParameter("@images3", images2));
                        cmd.ExecuteNonQuery();

                        // cmd.Parameters.Add(new SqlParameter("@images",images));

                        // int N = cmd.ExecuteNonQuery();
                        con.Close();
                        Console.WriteLine("\n Data Has Been Uploaded");
                        System.Windows.MessageBox.Show("Datas Saved Success");
                        NavigationService.Navigate(new Page2());
                    }
                }

                // 25.7.17 UPDATE
                //
                // 1) Hash is not consistent. It is correct for some files,  but wrong for some other files. (SOLVED)
                //
                // 2) Implement password in stego function. This password will be the input to generate a symmetric key to decrypt a file inside the carrier.
                //    File will be encrypted with this password(key), then stego-ed into the carrier.
                //    If key entered is correct, then the correct file will be extracted.
                //    If key entered is incorrect, a corrupted file will be extracted.
                //
                // 3) File decryption not complete (SOLVED)
            }
        }
Exemplo n.º 3
0
        private void uploadButton_Click(object sender, RoutedEventArgs e)
        {
            con = new SqlConnection(connectionString);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            sqlQuery = "SELECT keyPath FROM dbo.test WHERE UserID='" + currentUserName + "'";
            cmd      = new SqlCommand(sqlQuery, con);
            SqlDataReader DataRead1 = cmd.ExecuteReader();
            string        bigPath   = null;

            while (DataRead1.Read())
            {
                bigPath = DataRead1.GetString(0);
            }
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }



            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Controllers.KeyController kc = new Controllers.KeyController();

                string fileName;
                fileName  = dlg.FileName;
                filename1 = fileName;
                byte[] byteFormatOfFile = File.ReadAllBytes(@fileName);



                //Hash Computation
                SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                byte[] byteComputedHash;

                using (FileStream stream = File.OpenRead(fileName))
                {
                    SHA1Managed sha = new SHA1Managed();
                    byteComputedHash = sha.ComputeHash(stream);
                }
                Controllers.HashController hashController = new Controllers.HashController();
                stringComputedHash = hashController.HexStringFromBytes(byteComputedHash).ToUpper();

                //Important line to ensure user inputted hash stays at no more than 40 chars
                hash = stringUserHashInput(userHashInput);
                Console.WriteLine(hash);

                String filename     = System.IO.Path.GetFileNameWithoutExtension(dlg.FileName);
                String fullfilename = System.IO.Path.GetFullPath(dlg.FileName);
                String extension    = System.IO.Path.GetExtension(dlg.FileName);


                //Actions to take if hash not equals to computed hash
                if (!(hash.Equals(stringComputedHash)))
                {
                    //Do stuff:
                    //Stuff like an error message box pop up (done)
                    //Clear the userInputHash box (done)
                    //Break from the current method (done)
                    Controllers.Prompt.ShowDialog1("Hash does not match!", "Error");
                    userHashInput.Document.Blocks.Clear();
                }

                //If both hashes match, start encryption & slicing processes
                else
                {
                    byte[] IV = System.IO.File.ReadAllBytes(@bigPath + "\\IV.txt");
                    Console.WriteLine("Gets bytes of IV");
                    byte[] encryptedSymmetricKey = System.IO.File.ReadAllBytes(@bigPath + "\\encryptedSymmetricKey.txt");

                    //Gets the symmetric key by decrypting the encrypted symmetric key with the decryption (private) key
                    byte[] decryptedSymmetricKey = kc.asymmetricDecryption(encryptedSymmetricKey);
                    //Encrypts plaintext with symmetric key
                    byte[] cipherText = kc.symmetricEncryption(byteFormatOfFile, decryptedSymmetricKey, IV);



                    //Bryan code below
                    ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["connString"];
                    string     connectionString          = conSettings.ConnectionString;
                    string     VirusName;
                    string     type;
                    SqlCommand cmd5;

                    String imgLocation = "";
                    int    virus       = 0;
                    byte[] checkMD5;

                    path = filename1;
                    Console.Write(path);

                    byte[] md5HashBytes = ComputeMd5Hash(path);

                    String strMd5 = ToHexadecimal(md5HashBytes);

                    Console.Write(strMd5);


                    con = new SqlConnection(connectionString);
                    con.Open();
                    // cmd5 = new SqlCommand("SELECT vxVirusName, vxType FROM [User].[dbo].[vx] WHERE vxMD5 = " + checkMD5 + " ; ", con);
                    cmd5 = new SqlCommand("SELECT vxVirusName, vxType,vxMD5,BYTE FROM [User].[dbo].[vx] WHERE vxMD5 =  @checkMD5 ", con);

                    cmd5.Parameters.Add(new SqlParameter("@checkMD5", strMd5));



                    Console.Write(strMd5);

                    SqlDataReader reader1 = cmd5.ExecuteReader();
                    if (reader1.Read() == true)
                    {
                        VirusName = reader1.GetString(0);
                        type      = reader1.GetString(1);
                        System.Windows.MessageBox.Show(" \n Virus Dected : " + VirusName + " | Type Of Virus : " + type);
                        Console.WriteLine(" \n Virus Dected : " + VirusName + " | Type Of Virus : " + type);
                        con.Close();
                        virus = virus + 1;
                    }
                    else
                    {
                        con.Close();

                        con.Open();

                        string finalPath = fullPath();

                        FileStream   Stream = new FileStream(finalPath, FileMode.Open, FileAccess.Read);
                        BinaryReader brs    = new BinaryReader(Stream);
                        byte[]       images = brs.ReadBytes((int)Stream.Length);

                        String strImage = System.Text.Encoding.UTF8.GetString(images);


                        cmd2 = new SqlCommand("SELECT vxVirusName, vxType,vxMD5,BYTE FROM [User].[dbo].[vx] WHERE tID = 5002 ", con);

                        //  cmd2.Parameters.Add(new SqlParameter("@BYTE", strImage));

                        SqlDataReader reader2 = cmd2.ExecuteReader();

                        if (reader2.Read() == true)
                        {
                            if (strImage.Contains(reader2.GetString(3)))
                            {
                                virus     = virus + 1;
                                VirusName = reader2.GetString(0);
                                type      = reader2.GetString(1);

                                System.Windows.MessageBox.Show(" \n VIRUS DECTED! Your File is not being Uploaded");
                                System.Windows.MessageBox.Show(" \n Virus Dected : " + VirusName + " | Type Of Virus : " + type);
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("\n There is no virus! Very Good!! ");
                                Console.WriteLine("\n File is now being uploaded......");
                                System.Windows.MessageBox.Show("\n File is now being uploaded......");
                            }
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("\n There is no virus! Very Good!! ");
                            System.Windows.MessageBox.Show("\n File is now being uploaded......");
                        }
                        con.Close();
                    }



                    if (virus == 0)
                    {
                        FileModel fm = new FileModel(currentUserName, filename, cipherText, getFileSize(cipherText.Length), getCurrent(), "no", "no", extension, currentUserName);
                        fm.setShow(true);
                        FileModel.setFileModel(fm);
                        //FileModel fm = FileModel.getFileModel();
                        Page UploadingConsole = new Layout.Upload.Page2();
                        NavigationService.Navigate(UploadingConsole);
                    }
                }

                // 25.7.17 UPDATE
                //
                // 1) Hash is not consistent. It is correct for some files,  but wrong for some other files. (SOLVED)
                //
                // 2) Implement password in stego function. This password will be the input to generate a symmetric key to decrypt a file inside the carrier.
                //    File will be encrypted with this password(key), then stego-ed into the carrier.
                //    If key entered is correct, then the correct file will be extracted.
                //    If key entered is incorrect, a corrupted file will be extracted.
                //
                // 3) File decryption not complete (SOLVED)
            }
        }