示例#1
0
        public List <Complex[]> embedWatermark(List <Complex[]> complexframes, KeyGen key)
        {
            Debug.WriteLine("User Key Generated");

            Debug.WriteLine("Before Fourier Transform Inverse");
            complexframes = key.embedWatermark(complexframes);
            return(complexframes);
        }
示例#2
0
        public List<Complex[]> readAudio(int userid, int frameLength, int mode)
        {
            //mode1 embed ; mode2 verify
            byte[] file = fileReader();
            bytestodata = KeyGen.SimpleBoyerMooreSearch(file, datasection);

            subchunksize = new byte[4];
            subchunksize[0] = file[16];
            subchunksize[1] = file[17];
            subchunksize[2] = file[18];
            subchunksize[3] = file[19];

            //converting file into double[]
            int headerLength = audioHeaderLength();
            double[] x = ByteToDouble(file, headerLength);
            Debug.WriteLine("Header Length : " + headerLength);

            //convert double[] into complex[]
            Complex[] samples = DoubleToComplex(x);
            Debug.WriteLine("samples.Length " + samples.Length);

            //split complex[] into frames

            var frames = samples.Batch(frameLength);
            int frameCount = 0;
            int remainder = 0;
            List<Complex[]> complexframes = new List<Complex[]>();
            foreach (var batch in frames)
            {
                remainder = batch.Count();
                frameCount++;
                foreach (var frame in batch)
                {
                }
                complexframes.Add(batch.ToArray());
            }

            Debug.WriteLine("Before Fourier Transform Forward");
            foreach (var frame in complexframes)
            {
                Transform.FourierForward(frame);
            }

            if (mode == 1 && userid != 0)
            {
                //embedwatermark
                KeyGen key = new KeyGen(userid, frameLength, frameCount, name);
                complexframes = embedWatermark(complexframes, key);

                //convert complex[] to byte[]
                byte[] output = new byte[((frameCount - 1) * frameLength) + remainder + headerLength];
                int bytecounter = 0;
                foreach (var frame in complexframes)
                {
                    foreach (var singlecomplex in frame)
                    {
                        output[bytecounter + headerLength] = (byte)(Convert.ToInt16(singlecomplex.Real * 32768.0));
                        bytecounter++;
                    }
                }

                for (int i = 0; i < headerLength - 1; i++)
                {
                    output[i] = file[i];
                }

                Debug.WriteLine("Writing new file : " + path + "new_" + name);
                ByteArrayToFile(path + "new_" + name, output);

                Debug.WriteLine("File Successfully written");

                DBConnect connection = new DBConnect();

                string query = "UPDATE keydata SET keydatakey=@datakey where (iduserdata=@userid) and (keysongname=@name) ";

                try
                {
                    MySqlCommand cmd = new MySqlCommand(query, connection.OpenConnection());
                    cmd.CommandText = query;
                    cmd.Prepare();
                    cmd.Parameters.AddWithValue("@datakey", KeyGen.ToHex(MD5.Create().ComputeHash(File.ReadAllBytes(path + "new_" + name)),true));
                    cmd.Parameters.AddWithValue("@userid", userid);
                    cmd.Parameters.AddWithValue("@name", name);
                    Debug.WriteLine(MD5.Create().ComputeHash(File.ReadAllBytes(path + "new_" + name)));

                    cmd.ExecuteNonQuery();

                    connection.CloseConnection();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine("SQL INSERT INTO KEYDATA FAILED!");
                }

                return null;
            }
            return complexframes;
        }
示例#3
0
        public List<Complex[]> embedWatermark(List<Complex[]> complexframes, KeyGen key)
        {
            Debug.WriteLine("User Key Generated");

            Debug.WriteLine("Before Fourier Transform Inverse");
            complexframes = key.embedWatermark(complexframes);
            return complexframes;
        }
示例#4
0
        public static string verifyWatermark(List <Complex[]> complexframes1, List <Complex[]> complexframes2, int userid, string filename, string watermarkpath)
        {
            int shorter;

            if (complexframes1[0].Length >= complexframes2[0].Length)
            {
                shorter = complexframes2[0].Length;
            }
            else
            {
                shorter = complexframes1[0].Length;
            }

            string    storedkey  = "";
            string    name       = "";
            string    regno      = "";
            string    address    = "";
            string    contact    = "";
            string    keyinfo    = "";
            string    datakey    = "";
            DBConnect connection = new DBConnect();

            string query = "SELECT keydata.keydatadata, userdata.userdataname, userdata.userdataregno, userdata.userdataaddress, userdata.userdatacontact, keydata.keydatainfo, keydata.keydatakey FROM keydata INNER JOIN userdata ON keydata.iduserdata = userdata.iduserdata WHERE (keydata.iduserdata = @userid) AND (keydata.keysongname = @songname)";

            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection.OpenConnection());
                cmd.CommandText = query;
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@userid", userid);
                cmd.Parameters.AddWithValue("@songname", filename);
                MySqlDataReader Reader = cmd.ExecuteReader();
                if (!Reader.HasRows)
                {
                    return("No Matches found in Database!");
                }
                while (Reader.Read())
                {
                    name      = KeyGen.GetDBString("userdataname", Reader);
                    regno     = KeyGen.GetDBString("userdataregno", Reader);
                    storedkey = KeyGen.GetDBString("keydatadata", Reader);
                    address   = KeyGen.GetDBString("userdataaddress", Reader);
                    contact   = KeyGen.GetDBString("userdatacontact", Reader);
                    keyinfo   = KeyGen.GetDBString("keydatainfo", Reader);
                    datakey   = KeyGen.GetDBString("keydatakey", Reader);
                }
                Reader.Close();
                connection.CloseConnection();
            }
            catch (Exception ex)
            {
            }

            if (!storedkey.Equals("") && (storedkey != null))
            {
                double[] d = KeyGen.StringToDoubleArr(storedkey);


                double[] weight = new double[5];
                double   avg    = 0;


                for (int h = 0; h < complexframes1.Count; h++)
                {
                    Complex[] difference = new Complex[shorter];
                    for (int i = 0; i < 5; i++)
                    {
                        difference[i] = complexframes2[h][i] - complexframes1[h][i];
                        weight[i]     = difference[i].Real / d[i];
                    }
                    avg = avg + weight.Average();
                }
                avg = avg / complexframes1.Count;


                StringBuilder strOutputInfo = new StringBuilder();
                //if (Math.Abs(avg) >= 0.7 && Math.Abs(avg) <= 1.51)
                Debug.WriteLine(datakey);
                Debug.WriteLine(KeyGen.ToHex(MD5.Create().ComputeHash(File.ReadAllBytes(watermarkpath)), true));
                byte[] computedkeydata = MD5.Create().ComputeHash(File.ReadAllBytes(watermarkpath));
                if (datakey.Equals(KeyGen.ToHex(MD5.Create().ComputeHash(File.ReadAllBytes(watermarkpath)), true)))
                {
                    strOutputInfo.Append("Watermark is Verified<br />");
                    strOutputInfo.Append("Registered User : "******"<br />");
                    strOutputInfo.Append("Registration # : " + regno + "<br />");
                    strOutputInfo.Append("User's Contact : " + contact + "<br />");
                    strOutputInfo.Append("User's Address : " + address + "<br />");
                    strOutputInfo.Append("Watermark Info : " + keyinfo + "<br />");
                    return(strOutputInfo.ToString());
                }
                else
                {
                    strOutputInfo.Append("Watermark is Rejected");
                    return(strOutputInfo.ToString());
                }
            }
            else
            {
                return("No Matches found in Database!");
            }
        }
示例#5
0
        public List <Complex[]> readAudio(int userid, int frameLength, int mode)
        {
            //mode1 embed ; mode2 verify
            byte[] file = fileReader();
            bytestodata = KeyGen.SimpleBoyerMooreSearch(file, datasection);

            subchunksize    = new byte[4];
            subchunksize[0] = file[16];
            subchunksize[1] = file[17];
            subchunksize[2] = file[18];
            subchunksize[3] = file[19];

            //converting file into double[]
            int headerLength = audioHeaderLength();

            double[] x = ByteToDouble(file, headerLength);
            Debug.WriteLine("Header Length : " + headerLength);

            //convert double[] into complex[]
            Complex[] samples = DoubleToComplex(x);
            Debug.WriteLine("samples.Length " + samples.Length);


            //split complex[] into frames


            var frames     = samples.Batch(frameLength);
            int frameCount = 0;
            int remainder  = 0;
            List <Complex[]> complexframes = new List <Complex[]>();

            foreach (var batch in frames)
            {
                remainder = batch.Count();
                frameCount++;
                foreach (var frame in batch)
                {
                }
                complexframes.Add(batch.ToArray());
            }


            Debug.WriteLine("Before Fourier Transform Forward");
            foreach (var frame in complexframes)
            {
                Transform.FourierForward(frame);
            }



            if (mode == 1 && userid != 0)
            {
                //embedwatermark
                KeyGen key = new KeyGen(userid, frameLength, frameCount, name);
                complexframes = embedWatermark(complexframes, key);



                //convert complex[] to byte[]
                byte[] output      = new byte[((frameCount - 1) * frameLength) + remainder + headerLength];
                int    bytecounter = 0;
                foreach (var frame in complexframes)
                {
                    foreach (var singlecomplex in frame)
                    {
                        output[bytecounter + headerLength] = (byte)(Convert.ToInt16(singlecomplex.Real * 32768.0));
                        bytecounter++;
                    }
                }

                for (int i = 0; i < headerLength - 1; i++)
                {
                    output[i] = file[i];
                }

                Debug.WriteLine("Writing new file : " + path + "new_" + name);
                ByteArrayToFile(path + "new_" + name, output);

                Debug.WriteLine("File Successfully written");


                DBConnect connection = new DBConnect();

                string query = "UPDATE keydata SET keydatakey=@datakey where (iduserdata=@userid) and (keysongname=@name) ";

                try
                {
                    MySqlCommand cmd = new MySqlCommand(query, connection.OpenConnection());
                    cmd.CommandText = query;
                    cmd.Prepare();
                    cmd.Parameters.AddWithValue("@datakey", KeyGen.ToHex(MD5.Create().ComputeHash(File.ReadAllBytes(path + "new_" + name)), true));
                    cmd.Parameters.AddWithValue("@userid", userid);
                    cmd.Parameters.AddWithValue("@name", name);
                    Debug.WriteLine(MD5.Create().ComputeHash(File.ReadAllBytes(path + "new_" + name)));

                    cmd.ExecuteNonQuery();

                    connection.CloseConnection();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("SQL INSERT INTO KEYDATA FAILED!");
                }



                return(null);
            }
            return(complexframes);
        }