public EncryptedImage Encrypt(byte[] inputImage) { Stopwatch s = new Stopwatch(); s.Start(); int partsNum = GetPartsNumber(inputImage); int partSize = inputImage.Length / partsNum; byte[][] imageParts = inputImage.Split(partSize).Select(sl => sl.ToArray()).ToArray(); partsNum = imageParts.Length; EncryptedImage output = new EncryptedImage(partsNum); Parallel.For(0, partsNum, i => { BigInteger tempNumber = new BigInteger(imageParts[i]); int sign = tempNumber.Sign; if (sign == -1) { tempNumber *= -1; } tempNumber = BigInteger.ModPow(tempNumber, e, n); output.AddEncryptedNumber(tempNumber, sign, imageParts[i].Length, i); }); output.PrivateKey = Tuple.Create(d, n); s.Stop(); initTime = s.ElapsedMilliseconds; return(output); }
public byte[] Decrypt(EncryptedImage image) { Stopwatch s = new Stopwatch(); s.Start(); byte[][] output = new byte[image.EncryptedData.Count][]; Parallel.For(0, image.EncryptedData.Count, i => { BigInteger tempNumber = image.EncryptedData[i].Item1; tempNumber = BigInteger.ModPow(tempNumber, image.PrivateKey.Item1, image.PrivateKey.Item2); if (image.EncryptedData[i].Item2 == -1) { tempNumber *= -1; } var tempArray = tempNumber.ToByteArray().ToList(); if (tempArray.Count != image.EncryptedData[i].Item3) { tempArray.Add((image.EncryptedData[i].Item2 == -1) ? (byte)0xFF : (byte)0x00); } output[i] = tempArray.ToArray(); }); s.Stop(); initTime = s.ElapsedMilliseconds; return(output.SelectMany(sm => sm).ToArray()); }
private void Btn_EncryptImage_Click(object sender, RoutedEventArgs e) { if (EncryptionCore != null) { StatusLabel.Text = "Encryption in progress..."; Task.Factory.StartNew(() => { encryptedImage = EncryptionCore.Encrypt(InputImage); StatusLabel.Dispatcher.Invoke(() => { StatusLabel.Text = string.Format("Encryption done in {0} ms!", EncryptionCore.initTime); Btn_SaveImage.IsEnabled = true; }); }); } else { System.Windows.MessageBox.Show("Please wait for encryption core initialization!", "Please wait", MessageBoxButton.OK, MessageBoxImage.Asterisk); } }
private void Btn_LoadImage_Click(object sender, RoutedEventArgs e) { Btn_EncryptImage.IsEnabled = false; Btn_DecryptImage.IsEnabled = false; Btn_SaveImage.IsEnabled = false; encrypting = true; OpenFileDialog imageDialog = new OpenFileDialog() { InitialDirectory = "G:\\", Filter = "jpg images (*.jpg)|*.jpg|Encrypted image (*.eims)|*.eims", RestoreDirectory = true, AddExtension = true }; if (imageDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (imageDialog.FileName.Split('.').Last() == "jpg") { InputImage = File.ReadAllBytes(imageDialog.FileName); Btn_EncryptImage.IsEnabled = true; encrypting = true; } if (imageDialog.FileName.Split('.').Last() == "eims") { IFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream(imageDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.None)) { encryptedImage = (EncryptedImage)formatter.Deserialize(stream); } if (encryptedImage != null) { encrypting = false; Btn_DecryptImage.IsEnabled = true; } } } }