private void initializeOutput()
 {
     if (Source != null) {
         Output = new EncryptionMeta();
         int size = (int)Math.Sqrt(Source.Length / PIXEL_DATA_SIZE) + EXCESS_TOLERANCE;
         Output.Output = new Bitmap(size, size + DATA_ROW_NUMBER);
         Output.Key = GetString(key);
     }
 }
示例#2
0
        private void DecryptButton_Click(object sender, EventArgs e)
        {
            currentOperation = OPERATION_DECRYPTION;
            switch (algorithmList.SelectedIndex)
            {
                case CAESARS_CIPHER:
                                    {
                                        Stream stream = openFileDialog1.OpenFile();
                                        try
                                        {
                                            currentEncryption = new CaesarsCipherEncryption(stream, new Bitmap(100, 100));
                                            ((CaesarsCipherEncryption)currentEncryption).DestinationFolder = destinationFileList.Text;
                                            EncryptionMeta output = new EncryptionMeta();
                                            if (keyTextBox.Text.Length > 0)
                                            {
                                                output.Key = keyTextBox.Text;
                                                output.Output = new Bitmap(stream);
                                                stream.Close();
                                                this.currentEncryption.Output = output;
                                                EncryptAndDecryptFunction(false);
                                            }
                                            else
                                            {
                                                MessageBox.Show("Please input key.", "Error");
                                                keyTextBox.Focus();
                                            }
                                        }
                                        catch (FileNotFoundException)
                                        {
                                            MessageBox.Show("No file selected", "Error");
                                        }
                                    }
                                    break;
                case BLOWFISH:
                                {
                                    Stream stream = openFileDialog1.OpenFile();
                                    byte[] key;
                                    if (hexStringFlag.Checked)
                                        key = Encryption.GetBytesFromHexString(keyTextBox.Text);
                                    else
                                        key = Encryption.GetBytes(keyTextBox.Text);
                                    currentEncryption = new SHA256_Blowfish(key, null);
                                    ((SHA256_Blowfish)currentEncryption).DestinationFolder = destinationFileList.Text;
                                    EncryptionMeta output = new EncryptionMeta();
                                    if (keyTextBox.Text.Length > 0)
                                    {
                                        output.Key = keyTextBox.Text;
                                        output.Output = new Bitmap(stream);
                                        Color color = output.Output.GetPixel(0, 0);
                                        stream.Close();
                                        currentEncryption.Output = output;
                                        EncryptAndDecryptFunction(false);
                                    }

                                }
                                break;
                case OSIAS_ENCRYPTION:
                    {
                        if (keyTextBox.Text.Length > 0)
                        {
                            currentEncryption = new OSIASEncryption(null);
                            OSIASEncryption enc = (OSIASEncryption)currentEncryption;
                            enc.DataBitmap = new Bitmap(openFileDialog1.FileName);
                            enc.KeyBitMap = new Bitmap(openFileDialog2.FileName);
                            enc.Source = new FileStream(destinationFileList.Text + "\\output", FileMode.OpenOrCreate);
                            this.preEncryption = normalPreEncryption;
                            this.duringEncryption = normalDuringEncryption;
                            this.postEncrtypion = OsiasPostDecryption;
                            EncryptAndDecryptFunction(false);
                        }

                    }
                    break;

            }
        }
示例#3
0
 private void normalPostEncryption()
 {
     pictureBox1.Image = currentEncryption.Output.Output;
     output = currentEncryption.Output;
     if (currentOperation == OPERATION_ENCRYPTION)
         output.Output.Save(destinationFileList.Text + "\\" + output.Key.Substring(0, (int)Math.Min(output.Key.Length,8)) + ".bmp");
     keyTextBox.Text = output.Key;
     progressBar1.Value = 100;
     timer.Stop();
     MessageBox.Show("Finished " + currentOperation + "Time:" + timeToString(timerTicks));
     timerTicks = 0;
     currentOperation = null;
 }
 public override void applyEncryption()
 {
     // Problem: Size is not accurate relative to the stream length causes extra data to be encoded during
     // decryption and will result to the data being corrupted.
     // Solution: 00010101 (Current format) change to 00 11 01 11
     //                                               127 64, 32 16, 8 4, 2 1
     //                                                  R  G  B       R and B Contains data G does not
     //
     Percentage = 0;
     EncryptionMeta meta = new EncryptionMeta();
     if(key == null ||  key.Length==0)
      key  = generateKey();
     int size = (int)Math.Sqrt(Source.Length/3)+1;
     meta.Key = byteToString(key);
     meta.Output = new Bitmap(size, size);
     int x = 0;
     int y = 0;
     int[] colorsARGB = new int[4];
     byte[] buffer = new byte[bufferLength];
     long streamLength = Source.Length;
     int recordLimit = 3;
     bool stopEncryption = false;
     while (y < meta.Output.Height && stopEncryption==false) {
         if (Source.Position > Source.Length)
             break;
         while (x < meta.Output.Width && stopEncryption == false) {
             colorsARGB[0]= colorsARGB[1]= colorsARGB[2]= colorsARGB[3] = 0;
             Source.Read(buffer, 0, buffer.Length);
             if (streamLength  < buffer.Length) {
                 recordLimit = (int)streamLength;
                 stopEncryption = true;
             }
             else
                 streamLength -= buffer.Length;
             for (
                     int i = 0, maxIndicator = 16, dataFlag = 32;
                     i < recordLimit;
                     i++, maxIndicator /= 4, dataFlag /= 4
                )
             {
                 colorsARGB[i] = ((buffer[i] + key[i]) > byte.MaxValue) ? buffer[i] + key[i] - byte.MaxValue : buffer[i] + key[i];
                 if (buffer[i] == byte.MaxValue)
                     colorsARGB[3] += maxIndicator;
                 colorsARGB[3] += dataFlag;
             }
             meta.Output.SetPixel(x, y, Color.FromArgb(colorsARGB[3], colorsARGB[0], colorsARGB[1], colorsARGB[2]));
             Percentage = ((double)(meta.Output.Width*y)+x)* buffer.Length / Source.Length;
             x++;
         }
         x = 0;
         y++;
     }
     Output =  meta;
 }