Пример #1
0
 private void OnInfoButtonClick(object sender, EventArgs e)
 {
     using (PackageFactory keyFactory = new PackageFactory(_keyFilePath, _container.Authority))
     {
         using (FormInfo frmInfo = new FormInfo())
         {
             frmInfo.SetInfo(keyFactory.KeyInfo());
             frmInfo.ShowDialog(this);
         }
     }
 }
Пример #2
0
        /// <remarks>
        /// This method demonstrates using a PackageFactory and StreamCipher class to
        /// both encrypt a file, and optionally sign the message with an SHA512 HMAC.
        /// See the StreamCipher and StreamMac documentation for more examples.
        /// </remarks>
        private void Encrypt()
        {
            CipherDescription keyHeader;
            KeyParams keyParam;

            try
            {
                byte[] keyId = null;
                byte[] extKey = null;

                // get the keyheader and key material from the key file
                using (PackageFactory keyFactory = new PackageFactory(_keyFilePath, _container.Authority))
                {
                    // get the key info
                    PackageInfo pki = keyFactory.KeyInfo();

                    if (!keyFactory.AccessScope.Equals(KeyScope.Creator))
                    {
                        MessageBox.Show(keyFactory.LastError);
                        return;
                    }
                    keyId = (byte[])keyFactory.NextKey(out keyHeader, out keyParam, out extKey).Clone();
                }
                // offset start position is base header + Mac size
                int hdrOffset = MessageHeader.GetHeaderSize + keyHeader.MacSize;

                // with this constructor, the StreamCipher class creates the cryptographic
                // engine using the description in the CipherDescription.
                // The (cipher and) engine are destroyed in the cipherstream dispose
                using (StreamCipher cstrm = new StreamCipher(true, keyHeader, keyParam))
                {
                    using (FileStream inStream = new FileStream(_inputPath, FileMode.Open, FileAccess.Read))
                    {
                        using (FileStream outStream = new FileStream(_outputPath, FileMode.Create, FileAccess.ReadWrite))
                        {
                            // start at an output offset equal to the message header + MAC length
                            outStream.Seek(hdrOffset, SeekOrigin.Begin);
                            // use a percentage counter
                            cstrm.ProgressPercent += new StreamCipher.ProgressDelegate(OnProgressPercent);
                            // initialize internals
                            cstrm.Initialize(inStream, outStream);
                            // write the encrypted output to file
                            cstrm.Write();

                            // write the key id to the header
                            MessageHeader.SetKeyId(outStream, keyId);
                            // write the encrypted file extension
                            MessageHeader.SetExtension(outStream, MessageHeader.GetEncryptedExtension(Path.GetExtension(_inputPath), extKey));

                            // if this is a signing key, calculate the mac
                            if (keyHeader.MacSize > 0)
                            {
                                // Get the mac for the encrypted file; Mac engine is SHA512 by default,
                                // configurable via the CipherDescription MacSize and MacEngine members.
                                // This is where you would select and initialize the correct Digest via the
                                // CipherDescription members, and initialize the corresponding digest.
                                // For expedience, this example is fixed on the default SHA512.
                                // An optional progress event is available in the StreamMac class.
                                using (StreamMac mstrm = new StreamMac(new SHA512HMAC(keyParam.IKM)))
                                {
                                    // seek to end of header
                                    outStream.Seek(hdrOffset, SeekOrigin.Begin);
                                    // initialize mac stream
                                    mstrm.Initialize(outStream);
                                    // get the hash; specify offset and adjusted size
                                    byte[] hash = mstrm.ComputeMac(outStream.Length - hdrOffset, hdrOffset);
                                    // write the keyed hash value to the message header
                                    MessageHeader.SetMessageMac(outStream, hash);
                                }
                            }
                        }
                    }
                }
                // destroy the key
                keyParam.Dispose();
            }
            catch (Exception ex)
            {
                if (File.Exists(_outputPath))
                    File.Delete(_outputPath);

                string message = ex.Message == null ? "" : ex.Message;
                MessageBox.Show("An error occured, the file could not be encrypted! " + message);
            }
            finally
            {
                Invoke(new MethodInvoker(() => { Reset(); }));
            }
        }