예제 #1
0
        public static bool CheckZipFileEncryption(string path)
        {
            if (File.Exists(path))
            {
                using (FileStream fileStream = new FileStream(path, FileMode.Open))
                    using (ZipArchive archive = new ZipArchive(fileStream, ZipArchiveMode.Read))
                    {
                        ZipArchiveEntry zipEntry = null;
                        byte[]          buffer   = new byte[encryptionSignature.Length];

                        foreach (var entry in archive.Entries)
                        {
                            if (entry.FullName.EndsWith(".eimg"))
                            {
                                zipEntry = entry;
                                break;
                            }
                        }

                        if (zipEntry != null)
                        {
                            using (var zipEntryStream = zipEntry.Open())
                            {
                                zipEntryStream.Read(buffer, 0, encryptionSignature.Length);
                            }

                            if (NativeDiskWrapper.ByteArrayCompare(buffer, encryptionSignature))
                            {
                                return(true);
                            }
                        }
                    }
            }
            return(false);
        }
예제 #2
0
        public static bool CheckZipFilePassword(string path, string password)
        {
            using (FileStream fileStream = new FileStream(path, FileMode.Open))
                using (ZipArchive archive = new ZipArchive(fileStream, ZipArchiveMode.Read))
                {
                    Stream zipEntryStream = null;
                    byte[] passwordBytes  = Encoding.UTF8.GetBytes(password);
                    byte[] salt           = new byte[32];

                    foreach (var entry in archive.Entries)
                    {
                        if (entry.FullName.EndsWith(".eimg"))
                        {
                            zipEntryStream = entry.Open();
                            break;
                        }
                    }

                    zipEntryStream.Read(salt, 0, encryptionSignature.Length);
                    zipEntryStream.Read(salt, 0, salt.Length);

                    using (RijndaelManaged rijndael = new RijndaelManaged())
                    {
                        rijndael.KeySize   = 256;
                        rijndael.BlockSize = 128;
                        using (var key = new Rfc2898DeriveBytes(passwordBytes, salt, Crypto_Iterations))
                        {
                            rijndael.Key = key.GetBytes(rijndael.KeySize / 8);
                            rijndael.IV  = key.GetBytes(rijndael.BlockSize / 8);
                        }
                        rijndael.Padding = PaddingMode.Zeros;
                        rijndael.Mode    = CipherMode.CFB;

                        try
                        {
                            using (CryptoStream cryptoStream = new CryptoStream(zipEntryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
                            {
                                int passwordLength = cryptoStream.ReadByte();

                                if (passwordBytes.Length != passwordLength)
                                {
                                    return(false);
                                }

                                byte[] buff = new byte[passwordLength];
                                cryptoStream.Read(buff, 0, passwordLength);
                                return(NativeDiskWrapper.ByteArrayCompare(buff, passwordBytes));
                            }
                        }
                        catch (CryptographicException)
                        {
                            return(false);
                        }
                    }
                }
        }
예제 #3
0
        public static bool CheckRawFilePassword(string path, string password)
        {
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            byte[] salt          = new byte[32];

            using (FileStream fsCrypt = new FileStream(path, FileMode.Open))
            {
                fsCrypt.Read(salt, 0, encryptionSignature.Length);
                fsCrypt.Read(salt, 0, salt.Length);

                using (RijndaelManaged rijndael = new RijndaelManaged())
                {
                    rijndael.KeySize   = 256;
                    rijndael.BlockSize = 128;
                    using (var key = new Rfc2898DeriveBytes(passwordBytes, salt, Crypto_Iterations))
                    {
                        rijndael.Key = key.GetBytes(rijndael.KeySize / 8);
                        rijndael.IV  = key.GetBytes(rijndael.BlockSize / 8);
                    }
                    rijndael.Padding = PaddingMode.Zeros;
                    rijndael.Mode    = CipherMode.CFB;

                    try
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(fsCrypt, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
                        {
                            int passwordLength = cryptoStream.ReadByte();

                            if (passwordBytes.Length != passwordLength)
                            {
                                return(false);
                            }

                            byte[] buff = new byte[passwordLength];
                            cryptoStream.Read(buff, 0, passwordLength);
                            return(NativeDiskWrapper.ByteArrayCompare(buff, passwordBytes));
                        }
                    }
                    catch (CryptographicException)
                    {
                        return(false);
                    }
                }
            }
        }
예제 #4
0
        public static bool CheckRawFileEncryption(string path)
        {
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    byte[] buffer = new byte[encryptionSignature.Length];

                    fs.Read(buffer, 0, encryptionSignature.Length);

                    if (NativeDiskWrapper.ByteArrayCompare(buffer, encryptionSignature))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
예제 #5
0
        protected async Task <bool> VerifyImageAndDeviceCryptoWorkerAsync(IntPtr fileHandle, ulong sectorSize, ulong numSectors)
        {
            byte[]              fileData                = new byte[1024 * sectorSize];
            Stopwatch           msStopwatch             = new Stopwatch();
            Stopwatch           percentStopwatch        = new Stopwatch();
            ulong               totalBytesVerified      = 0;
            ulong               bytesVerified           = 0;
            ulong               bytesToVerify           = sectorSize * numSectors;
            ulong               bytesVerifiedPerPercent = 0;
            int                 lastProgress            = 0;
            int                 progress                = 0;
            int                 readed   = 0;
            List <Task <bool> > taskList = new List <Task <bool> >(deviceHandles.Length);

            byte[][] deviceData        = new byte[deviceHandles.Length][];
            int      failedDeviceIndex = 0;

            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            byte[] salt          = new byte[32];

            for (int i = 0; i < deviceHandles.Length; i++)
            {
                deviceData[i] = new byte[1024 * sectorSize];
            }

            msStopwatch.Start();
            percentStopwatch.Start();

            using (FileStream inputFile = new FileStream(new SafeFileHandle(fileHandle, false), FileAccess.Read))
                using (RijndaelManaged rijndael = new RijndaelManaged())
                {
                    inputFile.Seek(0, SeekOrigin.Begin);
                    inputFile.Read(salt, 0, encryptionSignature.Length);
                    inputFile.Read(salt, 0, salt.Length);

                    rijndael.KeySize   = 256;
                    rijndael.BlockSize = 128;

                    using (var key = new Rfc2898DeriveBytes(passwordBytes, salt, 1000))
                    {
                        rijndael.Key = key.GetBytes(rijndael.KeySize / 8);
                        rijndael.IV  = key.GetBytes(rijndael.BlockSize / 8);
                    }

                    rijndael.Padding = PaddingMode.Zeros;
                    rijndael.Mode    = CipherMode.CFB;

                    using (CryptoStream cs = new CryptoStream(inputFile, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        int passLen = cs.ReadByte();
                        cs.Read(fileData, 0, passLen);

                        for (ulong i = 0; i < numSectors; i += 1024)
                        {
                            taskList.Clear();

                            if (cancelPending)
                            {
                                return(false);
                            }

                            readed = cs.Read(fileData, 0, fileData.Length);

                            for (int x = 0; x < deviceHandles.Length; x++)
                            {
                                int index = x;
                                taskList.Add(Task.Run(() =>
                                {
                                    NativeDiskWrapper.ReadSectorDataFromHandle(deviceHandles[index], deviceData[index], i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);

                                    if (!NativeDiskWrapper.ByteArrayCompare(fileData, deviceData[index]))
                                    {
                                        failedDeviceIndex = index;
                                        return(false);
                                    }
                                    else
                                    {
                                        return(true);
                                    }
                                }));
                            }

                            await Task.WhenAll(taskList);

                            foreach (var task in taskList)
                            {
                                if (!task.Result)
                                {
                                    for (ulong x = 0; x < 1024 * sectorSize; x++)
                                    {
                                        if (deviceData[failedDeviceIndex][x] != fileData[x])
                                        {
                                            throw new Exception(string.Format("Verify found different data. Device {0}:\\ at byte {1:n0}, file data: 0x{2:X2}, device data: 0x{3:X2}", DriveLetters[failedDeviceIndex], i * sectorSize + x, deviceData[failedDeviceIndex][x], fileData[x]));
                                        }
                                    }
                                    return(false);
                                }
                            }

                            totalBytesVerified      += (ulong)readed;
                            bytesVerified           += (ulong)readed;
                            bytesVerifiedPerPercent += (ulong)readed;
                            bytesToVerify           -= (ulong)readed;

                            progress = (int)(i / (numSectors / 100.0)) + 1;

                            if (progress != lastProgress)
                            {
                                ulong averageBps = (ulong)(bytesVerifiedPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                                OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                                lastProgress            = progress;
                                bytesVerifiedPerPercent = 0;
                                percentStopwatch.Restart();
                            }

                            if (msStopwatch.ElapsedMilliseconds >= 1000)
                            {
                                ulong averageBps = (ulong)(bytesVerified / (msStopwatch.ElapsedMilliseconds / 1000.0));
                                OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesVerified, bytesToVerify));
                                bytesVerified = 0;
                                msStopwatch.Restart();
                            }
                        }
                    }
                }

            return(true);
        }
예제 #6
0
        protected override async Task <bool> VerifyImageAndDeviceWorkerAsync(IntPtr fileHandle, ulong sectorSize, ulong numSectors)
        {
            byte[]              fileData                = new byte[1024 * sectorSize];
            Stopwatch           msStopwatch             = new Stopwatch();
            Stopwatch           percentStopwatch        = new Stopwatch();
            ulong               totalBytesVerified      = 0;
            ulong               bytesVerified           = 0;
            ulong               bytesToVerify           = sectorSize * numSectors;
            ulong               bytesVerifiedPerPercent = 0;
            int                 lastProgress            = 0;
            int                 progress                = 0;
            int                 readed   = 0;
            List <Task <bool> > taskList = new List <Task <bool> >(deviceHandles.Length);

            byte[][] deviceData        = new byte[deviceHandles.Length][];
            int      failedDeviceIndex = 0;

            for (int i = 0; i < deviceHandles.Length; i++)
            {
                deviceData[i] = new byte[1024 * sectorSize];
            }

            msStopwatch.Start();
            percentStopwatch.Start();

            for (ulong i = 0; i < numSectors; i += 1024)
            {
                taskList.Clear();

                if (cancelPending)
                {
                    return(false);
                }

                readed = NativeDiskWrapper.ReadSectorDataFromHandle(fileHandle, fileData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);
                for (int x = 0; x < deviceHandles.Length; x++)
                {
                    int index = x;
                    taskList.Add(Task.Run(() =>
                    {
                        NativeDiskWrapper.ReadSectorDataFromHandle(deviceHandles[index], deviceData[index], i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);

                        if (!NativeDiskWrapper.ByteArrayCompare(fileData, deviceData[index]))
                        {
                            failedDeviceIndex = index;
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    }));
                }

                await Task.WhenAll(taskList);

                foreach (var task in taskList)
                {
                    if (!task.Result)
                    {
                        for (ulong x = 0; x < 1024 * sectorSize; x++)
                        {
                            if (deviceData[failedDeviceIndex][x] != fileData[x])
                            {
                                throw new Exception(string.Format("Verify found different data. Device {0}:\\ at byte {1:n0}, file data: 0x{2:X2}, device data: 0x{3:X2}", DriveLetters[failedDeviceIndex], i * sectorSize + x, deviceData[failedDeviceIndex][x], fileData[x]));
                            }
                        }
                        return(false);
                    }
                }

                totalBytesVerified      += (ulong)readed;
                bytesVerified           += (ulong)readed;
                bytesVerifiedPerPercent += (ulong)readed;
                bytesToVerify           -= (ulong)readed;

                progress = (int)(i / (numSectors / 100.0)) + 1;

                if (progress != lastProgress)
                {
                    ulong averageBps = (ulong)(bytesVerifiedPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                    OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                    lastProgress            = progress;
                    bytesVerifiedPerPercent = 0;
                    percentStopwatch.Restart();
                }

                if (msStopwatch.ElapsedMilliseconds >= 1000)
                {
                    ulong averageBps = (ulong)(bytesVerified / (msStopwatch.ElapsedMilliseconds / 1000.0));
                    OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesVerified, bytesToVerify));
                    bytesVerified = 0;
                    msStopwatch.Restart();
                }
            }

            return(true);
        }