Exemplo n.º 1
0
        /// <summary>
        /// Decrypts WAV file with SDS.
        /// </summary>
        /// <param name="EncryptedWAVFile">WAV file to decrypt</param>
        /// <param name="DecryptedWAVFile">Decrypted WAV file</param>
        /// <param name="key">Decryption key - seed value for random number generator (same value should have been used for encryption)</param>
        public static void DecryptWAVFile(WAVFile EncryptedWAVFile, WAVFile DecryptedWAVFile, int key)
        {
            // SDS values x1[i], x1[i+1], x1[i+2], c3 (contains encrypted sample value)
            double x1i, x1i1, x1i2, c3;
            // Normally distributed random variable z and white noise value n
            double z, n;
            // Decrypted sample value
            short sample;

            // Seed a new random (seed value is our key)
            Random rnd = new Random(key);

            using (FileStream fsEncrypted = new FileStream(EncryptedWAVFile.FileName, FileMode.Open, FileAccess.Read))
            using (BinaryReader br = new BinaryReader(fsEncrypted))
            using (FileStream fsDecrypted = new FileStream(DecryptedWAVFile.FileName, FileMode.Append, FileAccess.Write))
            using (BinaryWriter bw = new BinaryWriter(fsDecrypted))
            {
                // Move to data position right after the header (header size is 44 bytes)
                br.BaseStream.Position = 44;

                int samplesCount = 0;

                while (br.BaseStream.Length > br.BaseStream.Position)
                {
                    // We need to read 3 consecutive SDS values x1[i], x1[i+1] and x1[i+2]
                    x1i = br.ReadDouble();
                    // We could reach EOF after reading
                    if (!(br.BaseStream.Length > br.BaseStream.Position))
                        break;
                    x1i1 = br.ReadDouble();
                    // We could reach EOF after reading
                    if (!(br.BaseStream.Length > br.BaseStream.Position))
                        break;
                    x1i2 = br.ReadDouble();

                    // Move 2 double positions back because we have read 3 values instead of 1
                    br.BaseStream.Seek(-2 * sizeof(double), SeekOrigin.Current);

                    // Calculate white noise value
                    z = Z1 + Z2 * rnd.NextDouble();
                    n = z * Math.Sqrt(N0 / DT);

                    // Calculate decrypted c3 parameter value
                    c3 = GetC3Decrypted(x1i, x1i1, x1i2, n);

                    // TODO: Several decrypted c3s are false. Should throw an exception as sample will be out of 'short' range
                    /*
                    //sample = Convert.ToInt16(c3);

                    if ((samplesCount == 48598) || (samplesCount == 48600))
                    {
                        //System.Diagnostics.Debugger.Break();
                        Console.WriteLine("Count {0}: {1}", samplesCount, c3);
                    }

                    if ((c3 > 1.0f) || (c3 < -1.0f))
                    {
                        //System.Diagnostics.Debugger.Break();
                        Console.WriteLine("{0} - {1}", c3, samplesCount);
                    }
                    */

                    // Denormalize c3 value from [-1; 1] range to [-32768; 32767]
                    if (c3 < 0)
                    {
                        c3 *= Modif;
                    }
                    else
                    {
                        c3 *= Modif - 1;
                    }

                    // Convert to 16-bit sample (rounding is important when converting from floating point!)
                    sample = (short)Math.Round(c3);

                    // Decrypted sample is written to file
                    bw.Write(sample);
                    samplesCount++;
                }

                Console.WriteLine("Finished decrypting {0} samples.", samplesCount);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encrypts WAV file with SDS.
        /// </summary>
        /// <param name="OriginalWAVFile">WAV file to encrypt</param>
        /// <param name="EncryptedWAVFile">Encrypted WAV file</param>
        /// <param name="key">Encryption key - seed value for random number generator (same value should be used for decryption)</param>
        public static void EncryptWAVFile(WAVFile OriginalWAVFile, WAVFile EncryptedWAVFile, int key)
        {
            // SDS values and white noise
            double x1, x2, n;
            // SDS values from previous step, required for calculations
            double x1Prev, x2Prev, nPrev;
            // Normally distributed random variable z and SDS input parameter c3 - normalized sample
            double z, c3;
            // Sound sample from WAV file
            short sample;

            // Seed a new random (seed value is our key)
            Random rnd = new Random(key);

            // Filling starting values
            z = Z1 + Z2 * rnd.NextDouble();
            nPrev = z * Math.Sqrt(N0 / DT);
            x1Prev = X10;
            x2Prev = X20;

            using (FileStream fsOriginal = new FileStream(OriginalWAVFile.FileName, FileMode.Open, FileAccess.Read))
            using (BinaryReader br = new BinaryReader(fsOriginal))
            using (FileStream fsEncrypted = new FileStream(EncryptedWAVFile.FileName, FileMode.Append, FileAccess.Write))
            using (BinaryWriter bw = new BinaryWriter(fsEncrypted))
            {
                // Move to data position right after the header (header size is 44 bytes)
                br.BaseStream.Position = 44;

                // x1 starting value is written as the first sample into encrypted file
                bw.Write(x1Prev);

                int samplesCount = 0;

                while (br.BaseStream.Length > br.BaseStream.Position)
                {
                    // Read 16-bit sample from WAV file
                    sample = br.ReadInt16();

                    // Normalize sample to [-1; 1]
                    if (sample < 0)
                    {
                        c3 = ((double)sample) / Modif;
                    }
                    else
                    {
                        c3 = ((double)sample) / (Modif - 1);
                    }

                    // TODO: Bug in decryption
                    /*
                    if ((samplesCount == 48598) || (samplesCount == 48600))
                    {
                        //System.Diagnostics.Debugger.Break();
                        Console.WriteLine("Count {0}: {1}", samplesCount, c3);
                    }

                    if ((samplesCount == 48599) || (samplesCount == 50954) || (samplesCount == 76648) || (samplesCount == 98925) || (samplesCount == 123705))
                    {
                        //System.Diagnostics.Debugger.Break();
                        Console.WriteLine("{0} - {1}", c3, samplesCount);
                    }
                    */

                    // Calculate white noise and SDS values
                    z = Z1 + Z2 * rnd.NextDouble();
                    n = z * Math.Sqrt(N0 / DT);
                    x1 = GetX1Encrypted(x1Prev, x2Prev);
                    x2 = GetX2Encrypted(c3, x1Prev, x2Prev, nPrev);

                    // x1 is the encrypted sample so it's written to file
                    bw.Write(x1);
                    samplesCount++;

                    // Set current values as previous for the next iteration
                    x1Prev = x1;
                    x2Prev = x2;
                    nPrev = n;
                }

                Console.WriteLine("Finished encrypting {0} samples.", samplesCount);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                string fileNameOriginal = args[0];

                if (fileNameOriginal.Contains(".wav"))
                {
                    // Set encrypted file name to "%fileNameOriginal% (encrypted).wav"
                    string fileNameEncrypted = fileNameOriginal.Insert(fileNameOriginal.IndexOf(".wav"), " (encrypted)");
                    // Set decrypted file name to "%fileNameOriginal% (decrypted).wav"
                    string fileNameDecrypted = fileNameOriginal.Insert(fileNameOriginal.IndexOf(".wav"), " (decrypted)");

                    //
                    // Input
                    //

                    WAVFile WAVFileOriginal = new WAVFile(fileNameOriginal);

                    // Read WAV file header
                    WAVFileOriginal.ReadWAVHeader();

                    // Output basic WAV file info
                    WAVFileOriginal.GetWAVFileInfo();

                    //
                    // Encryption
                    //

                    WAVFile WAVFileEncrypted = new WAVFile(fileNameEncrypted);

                    // Copy original WAV file header
                    WAVFileEncrypted.Header = WAVFileOriginal.Header;

                    // Fix WAV header for encrypted file and write it
                    WAVFileEncrypted.CreateEncryptedWAVFile();

                    // Encrypt WAV file with SDS and seed value 1
                    SDS.EncryptWAVFile(WAVFileOriginal, WAVFileEncrypted, 1);

                    //
                    // Decryption
                    //

                    WAVFile WAVFileDecrypted = new WAVFile(fileNameDecrypted);

                    // Copy original WAV file header
                    WAVFileDecrypted.Header = WAVFileOriginal.Header;

                    // Fix WAV header for decrypted file and write it
                    WAVFileDecrypted.CreateDecryptedWAVFile();

                    // Decrypt WAV file with SDS and seed value 1
                    SDS.DecryptWAVFile(WAVFileEncrypted, WAVFileDecrypted, 1);
                }
                else
                {
                    Console.WriteLine("Please supply a correct WAV file for encryption.");
                }
            }
            else
            {
                Console.WriteLine("Please supply only a WAV file for encryption.");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Encrypts WAV file with SDS.
        /// </summary>
        /// <param name="OriginalWAVFile">WAV file to encrypt</param>
        /// <param name="EncryptedWAVFile">Encrypted WAV file</param>
        /// <param name="key">Encryption key - seed value for random number generator (same value should be used for decryption)</param>
        public static void EncryptWAVFile(WAVFile OriginalWAVFile, WAVFile EncryptedWAVFile, int key)
        {
            // SDS values and white noise
            double x1, x2, n;
            // SDS values from previous step, required for calculations
            double x1Prev, x2Prev, nPrev;
            // Normally distributed random variable z and SDS input parameter c3 - normalized sample
            double z, c3;
            // Sound sample from WAV file
            short sample;

            // Seed a new random (seed value is our key)
            Random rnd = new Random(key);

            // Filling starting values
            z      = Z1 + Z2 * rnd.NextDouble();
            nPrev  = z * Math.Sqrt(N0 / DT);
            x1Prev = X10;
            x2Prev = X20;

            using (FileStream fsOriginal = new FileStream(OriginalWAVFile.FileName, FileMode.Open, FileAccess.Read))
                using (BinaryReader br = new BinaryReader(fsOriginal))
                    using (FileStream fsEncrypted = new FileStream(EncryptedWAVFile.FileName, FileMode.Append, FileAccess.Write))
                        using (BinaryWriter bw = new BinaryWriter(fsEncrypted))
                        {
                            // Move to data position right after the header (header size is 44 bytes)
                            br.BaseStream.Position = 44;

                            // x1 starting value is written as the first sample into encrypted file
                            bw.Write(x1Prev);

                            int samplesCount = 0;

                            while (br.BaseStream.Length > br.BaseStream.Position)
                            {
                                // Read 16-bit sample from WAV file
                                sample = br.ReadInt16();

                                // Normalize sample to [-1; 1]
                                if (sample < 0)
                                {
                                    c3 = ((double)sample) / Modif;
                                }
                                else
                                {
                                    c3 = ((double)sample) / (Modif - 1);
                                }

                                // TODO: Bug in decryption

                                /*
                                 * if ((samplesCount == 48598) || (samplesCount == 48600))
                                 * {
                                 *  //System.Diagnostics.Debugger.Break();
                                 *  Console.WriteLine("Count {0}: {1}", samplesCount, c3);
                                 * }
                                 *
                                 * if ((samplesCount == 48599) || (samplesCount == 50954) || (samplesCount == 76648) || (samplesCount == 98925) || (samplesCount == 123705))
                                 * {
                                 *  //System.Diagnostics.Debugger.Break();
                                 *  Console.WriteLine("{0} - {1}", c3, samplesCount);
                                 * }
                                 */

                                // Calculate white noise and SDS values
                                z  = Z1 + Z2 * rnd.NextDouble();
                                n  = z * Math.Sqrt(N0 / DT);
                                x1 = GetX1Encrypted(x1Prev, x2Prev);
                                x2 = GetX2Encrypted(c3, x1Prev, x2Prev, nPrev);

                                // x1 is the encrypted sample so it's written to file
                                bw.Write(x1);
                                samplesCount++;

                                // Set current values as previous for the next iteration
                                x1Prev = x1;
                                x2Prev = x2;
                                nPrev  = n;
                            }

                            Console.WriteLine("Finished encrypting {0} samples.", samplesCount);
                        }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Decrypts WAV file with SDS.
        /// </summary>
        /// <param name="EncryptedWAVFile">WAV file to decrypt</param>
        /// <param name="DecryptedWAVFile">Decrypted WAV file</param>
        /// <param name="key">Decryption key - seed value for random number generator (same value should have been used for encryption)</param>
        public static void DecryptWAVFile(WAVFile EncryptedWAVFile, WAVFile DecryptedWAVFile, int key)
        {
            // SDS values x1[i], x1[i+1], x1[i+2], c3 (contains encrypted sample value)
            double x1i, x1i1, x1i2, c3;
            // Normally distributed random variable z and white noise value n
            double z, n;
            // Decrypted sample value
            short sample;

            // Seed a new random (seed value is our key)
            Random rnd = new Random(key);

            using (FileStream fsEncrypted = new FileStream(EncryptedWAVFile.FileName, FileMode.Open, FileAccess.Read))
                using (BinaryReader br = new BinaryReader(fsEncrypted))
                    using (FileStream fsDecrypted = new FileStream(DecryptedWAVFile.FileName, FileMode.Append, FileAccess.Write))
                        using (BinaryWriter bw = new BinaryWriter(fsDecrypted))
                        {
                            // Move to data position right after the header (header size is 44 bytes)
                            br.BaseStream.Position = 44;

                            int samplesCount = 0;

                            while (br.BaseStream.Length > br.BaseStream.Position)
                            {
                                // We need to read 3 consecutive SDS values x1[i], x1[i+1] and x1[i+2]
                                x1i = br.ReadDouble();
                                // We could reach EOF after reading
                                if (!(br.BaseStream.Length > br.BaseStream.Position))
                                {
                                    break;
                                }
                                x1i1 = br.ReadDouble();
                                // We could reach EOF after reading
                                if (!(br.BaseStream.Length > br.BaseStream.Position))
                                {
                                    break;
                                }
                                x1i2 = br.ReadDouble();

                                // Move 2 double positions back because we have read 3 values instead of 1
                                br.BaseStream.Seek(-2 * sizeof(double), SeekOrigin.Current);

                                // Calculate white noise value
                                z = Z1 + Z2 * rnd.NextDouble();
                                n = z * Math.Sqrt(N0 / DT);

                                // Calculate decrypted c3 parameter value
                                c3 = GetC3Decrypted(x1i, x1i1, x1i2, n);

                                // TODO: Several decrypted c3s are false. Should throw an exception as sample will be out of 'short' range

                                /*
                                 * //sample = Convert.ToInt16(c3);
                                 *
                                 * if ((samplesCount == 48598) || (samplesCount == 48600))
                                 * {
                                 *  //System.Diagnostics.Debugger.Break();
                                 *  Console.WriteLine("Count {0}: {1}", samplesCount, c3);
                                 * }
                                 *
                                 * if ((c3 > 1.0f) || (c3 < -1.0f))
                                 * {
                                 *  //System.Diagnostics.Debugger.Break();
                                 *  Console.WriteLine("{0} - {1}", c3, samplesCount);
                                 * }
                                 */

                                // Denormalize c3 value from [-1; 1] range to [-32768; 32767]
                                if (c3 < 0)
                                {
                                    c3 *= Modif;
                                }
                                else
                                {
                                    c3 *= Modif - 1;
                                }

                                // Convert to 16-bit sample (rounding is important when converting from floating point!)
                                sample = (short)Math.Round(c3);

                                // Decrypted sample is written to file
                                bw.Write(sample);
                                samplesCount++;
                            }

                            Console.WriteLine("Finished decrypting {0} samples.", samplesCount);
                        }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                string fileNameOriginal = args[0];

                if (fileNameOriginal.Contains(".wav"))
                {
                    // Set encrypted file name to "%fileNameOriginal% (encrypted).wav"
                    string fileNameEncrypted = fileNameOriginal.Insert(fileNameOriginal.IndexOf(".wav"), " (encrypted)");
                    // Set decrypted file name to "%fileNameOriginal% (decrypted).wav"
                    string fileNameDecrypted = fileNameOriginal.Insert(fileNameOriginal.IndexOf(".wav"), " (decrypted)");

                    //
                    // Input
                    //

                    WAVFile WAVFileOriginal = new WAVFile(fileNameOriginal);

                    // Read WAV file header
                    WAVFileOriginal.ReadWAVHeader();

                    // Output basic WAV file info
                    WAVFileOriginal.GetWAVFileInfo();

                    //
                    // Encryption
                    //

                    WAVFile WAVFileEncrypted = new WAVFile(fileNameEncrypted);

                    // Copy original WAV file header
                    WAVFileEncrypted.Header = WAVFileOriginal.Header;

                    // Fix WAV header for encrypted file and write it
                    WAVFileEncrypted.CreateEncryptedWAVFile();

                    // Encrypt WAV file with SDS and seed value 1
                    SDS.EncryptWAVFile(WAVFileOriginal, WAVFileEncrypted, 1);

                    //
                    // Decryption
                    //

                    WAVFile WAVFileDecrypted = new WAVFile(fileNameDecrypted);

                    // Copy original WAV file header
                    WAVFileDecrypted.Header = WAVFileOriginal.Header;

                    // Fix WAV header for decrypted file and write it
                    WAVFileDecrypted.CreateDecryptedWAVFile();

                    // Decrypt WAV file with SDS and seed value 1
                    SDS.DecryptWAVFile(WAVFileEncrypted, WAVFileDecrypted, 1);
                }
                else
                {
                    Console.WriteLine("Please supply a correct WAV file for encryption.");
                }
            }
            else
            {
                Console.WriteLine("Please supply only a WAV file for encryption.");
            }
        }