public static void Decrypt(byte[] input, byte[] output, BeaufortKey key)
 {
     Debug.Assert(input.Length == output.Length);
     for (var i = 0; i < input.Length; i++)
     {
         if (input[i] == '\n' || input[i] == '\r')
         {
             output[i] = input[i];
             continue;
         }
         var inputNormalized = input[i] - Constants.AsciiOffset;
         var plain = (byte) inputNormalized + key.Current;
         var outputNormalized =
             (byte) (plain < Constants.AlphabetLength ? plain : (plain - Constants.AlphabetLength));
         output[i] = (byte) (outputNormalized + Constants.AsciiOffset);
         key.MoveNext();
     }
 }
예제 #2
0
        void LoadDecryptedButton_Click(object sender, EventArgs e)
        {
            if (_fileDialog.ShowDialog() == DialogResult.OK)
            {
                var fInfo = new FileInfo(_fileDialog.FileName);

                if (fInfo.Length > MaxFileSize)
                {
                    // file too big to fit into the textbox, offer to encrypt to file directly
                    var message = "File too big to show in this window\nDo you want to encrypt directly to file?";
                    if (MessageBox.Show(message, "File too big", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    {
                        return;
                    }
                    if (_saveDialog.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    using (var writer = new StreamWriter(new BufferedStream(_saveDialog.OpenFile())))
                    using (var reader = new StreamReader(new BufferedStream(_fileDialog.OpenFile())))
                    {
                        var inBuffer = new char[Constants.BufferSize];
                        var outBuffer = new char[Constants.BufferSize];
                        var key = new BeaufortKey(Key);
                        int read;

                        while ((read = reader.Read(inBuffer, 0, Constants.BufferSize)) > 0)
                        {
                            BeaufortCipher.Encrypt(inBuffer, outBuffer, key);
                            writer.Write(outBuffer, 0, read);
                        }
                    }
                }
                else
                {
                    using (var reader = new StreamReader(_fileDialog.OpenFile()))
                    {
                        DecryptedText = reader.ReadToEnd();
                    }
                }
            }
        }
 public static void Encrypt(char[] input, char[] output, BeaufortKey key)
 {
     Encoding.ASCII.GetBytes(input, 0, input.Length, DecryptedBuffer, 0);
     Encrypt(DecryptedBuffer, EncryptedBuffer, key);
     Encoding.ASCII.GetChars(EncryptedBuffer, 0, Constants.BufferSize, output, 0);
 }