Exemplo n.º 1
0
        void mnuClipboardEncryptSign_Click(Object sender, System.EventArgs e)
        {
            // Check if we have some secret keys at all.
            XmlNodeList xnlSecretKeys = SharpPrivacy.SecretKeyRing.GetElementsByTagName("SecretKey");

            if (xnlSecretKeys.Count == 0)
            {
                MessageBox.Show("You cannot sign data because you do not own a private key. Please generate a new key pair, before you sign data.", "Action not possible...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            string strMessage = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);

            pksSelector.ShowDialog();

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            QueryPassphrase qpPassphrase = new QueryPassphrase();

            qpPassphrase.ShowMultiKeyDialog(SharpPrivacy.SecretKeyRing);
            ulong  lSignatureKeyID = qpPassphrase.SelectedKey;
            string strPassphrase   = qpPassphrase.Passphrase;

            try {
                SharpPrivacy.ReloadKeyRing();
                string strReturn = SharpPrivacy.Instance.EncryptAndSignText(strMessage, lTargetKeyIDs, lSignatureKeyID, strPassphrase);
                Clipboard.SetDataObject(strReturn);
            } catch (Exception ex) {
                MessageBox.Show("An error occured while encrypting the message: " + ex.Message + "\n\n" + ex.StackTrace, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }
Exemplo n.º 2
0
        void mnuFileSystemEncrypt_Click(Object sender, System.EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofdOpen = new System.Windows.Forms.OpenFileDialog();

            ofdOpen.Multiselect = true;
            ofdOpen.Filter      = "All Files (*.*)|*.*";
            ofdOpen.ShowDialog();

            if (ofdOpen.FileNames.Length == 0)
            {
                return;
            }

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);

            pksSelector.ShowDialog();

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            for (int i = 0; i < ofdOpen.FileNames.Length; i++)
            {
                string strPath = ofdOpen.FileNames[i];
                SharpPrivacy.ReloadKeyRing();
                SharpPrivacy.Instance.EncryptFile(strPath, strPath + ".asc", lTargetKeyIDs);
            }
        }
Exemplo n.º 3
0
        void mnuClipboardEncrypt_Click(Object sender, System.EventArgs e)
        {
            string strMessage = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);

            pksSelector.ShowDialog();

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            try {
                startWorking();
                SharpPrivacy.ReloadKeyRing();
                string strReturn = SharpPrivacy.Instance.EncryptText(strMessage, lTargetKeyIDs);
                Clipboard.SetDataObject(strReturn);
                stopWorking();
            } catch (Exception ex) {
                MessageBox.Show("An error occured while encrypting the message: " + ex.Message + "\n\n" + ex.StackTrace, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }
Exemplo n.º 4
0
        void mnuFileSystemEncryptSign_Click(Object sender, System.EventArgs e)
        {
            // Check if we have some secret keys at all.
            XmlNodeList xnlSecretKeys = SharpPrivacy.SecretKeyRing.GetElementsByTagName("SecretKey");

            if (xnlSecretKeys.Count == 0)
            {
                MessageBox.Show("You cannot sign data because you do not own a private key. Please generate a new key pair, before you sign data.", "Action not possible...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            System.Windows.Forms.OpenFileDialog ofdOpen = new System.Windows.Forms.OpenFileDialog();

            ofdOpen.Multiselect = true;
            ofdOpen.Filter      = "All Files (*.*)|*.*";
            ofdOpen.ShowDialog();

            if (ofdOpen.FileNames.Length == 0)
            {
                return;
            }

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);

            pksSelector.ShowDialog();

            QueryPassphrase qpPassphrase = new QueryPassphrase();

            qpPassphrase.ShowMultiKeyDialog(SharpPrivacy.SecretKeyRing);
            ulong  lSignatureKeyID = qpPassphrase.SelectedKey;
            string strPassphrase   = qpPassphrase.Passphrase;

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            for (int i = 0; i < ofdOpen.FileNames.Length; i++)
            {
                string strPath = ofdOpen.FileNames[i];
                SharpPrivacy.ReloadKeyRing();
                SharpPrivacy.Instance.EncryptAndSignFile(strPath, strPath + ".asc", lTargetKeyIDs, lSignatureKeyID, strPassphrase);
            }
        }
Exemplo n.º 5
0
        public static string EncryptText(string strMessage, PublicKeyRing pkrPublicKeyRing, SecretKeyRing skrSecretKeyRing, bool bSign)
        {
            PublicKeySelector pksSelectKeys = new PublicKeySelector(pkrPublicKeyRing);
            pksSelectKeys.ShowDialog();
            TransportableSecretKey tskKey = new TransportableSecretKey();
            string strPassphrase = "";

            if (bSign) {
                QueryPassphrase qpPassphrase = new QueryPassphrase();
                qpPassphrase.ShowMyDialog(skrSecretKeyRing);
                tskKey = qpPassphrase.SelectedKey;
                strPassphrase = qpPassphrase.Passphrase;
            }

            if (pksSelectKeys.SelectedKeys.Count == 0)
                return strMessage;

            Working wWorking = new Working();
            wWorking.Show();

            LiteralMessage lmMessage = new LiteralMessage(DataFormatTypes.Text);
            lmMessage.Text = strMessage;
            lmMessage.TimeCreated = DateTime.Now;
            lmMessage.Filename = "";

            SharpPrivacy.OpenPGP.Messages.Message mEncryptionMessage = lmMessage;

            if (bSign) {
                SignedMessage smMessage = new SignedMessage();
                smMessage.MessageSigned = lmMessage;
                SignaturePacket spPacket = new SignaturePacket();
                spPacket.Version = SignaturePacketVersionNumbers.v3;
                SecretKeyPacket skpKey = tskKey.FindKey(AsymActions.Sign);
                spPacket.KeyID = skpKey.PublicKey.KeyID;
                spPacket.HashAlgorithm = HashAlgorithms.SHA1;
                spPacket.SignatureAlgorithm = skpKey.PublicKey.Algorithm;
                spPacket.TimeCreated = DateTime.Now;
                spPacket.SignatureType = SignatureTypes.TextSignature;
                spPacket.Sign(lmMessage.Binary, skpKey, strPassphrase);
                smMessage.Signature = spPacket;
                mEncryptionMessage = smMessage;
            }

            CompressedMessage cmMessage = new CompressedMessage();
            cmMessage.Compress(mEncryptionMessage);

            wWorking.Progress(20);

            SymAlgorithms saAlgo = GetSymAlgorithmPreferences(pksSelectKeys.SelectedKeys);

            SymmetricallyEncryptedDataPacket sedpEncrypted = new SymmetricallyEncryptedDataPacket();
            SymmetricAlgorithm saEncrypt = CipherHelper.CreateSymAlgorithm(saAlgo);
            saEncrypt.Mode = CipherMode.OpenPGP_CFB;
            saEncrypt.GenerateKey();
            byte[] bKey = saEncrypt.Key;

            wWorking.Progress(10);
            ESKSequence esksKeys = new ESKSequence();
            try {
                 esksKeys = CreateESKSequence(pksSelectKeys.SelectedKeys, AsymActions.Encrypt, saAlgo, bKey);
            } catch (Exception e) {
                wWorking.Hide();
                MessageBox.Show("The following error occured: " + e.Message, "Error...");
                return strMessage;
            }

            wWorking.Progress(50);

            ICryptoTransform ictEncryptor = saEncrypt.CreateEncryptor();
            byte[] bMessage = cmMessage.GetEncoded();
            byte[] bOutput = new byte[bMessage.Length];
            ictEncryptor.TransformBlock(bMessage, 0, bMessage.Length, ref bOutput, 0);
            bKey.Initialize();

            wWorking.Progress(10);

            int iOutLength = (saEncrypt.BlockSize >> 3) + 2 + bMessage.Length;
            sedpEncrypted.Body = new byte[iOutLength];
            Array.Copy(bOutput, 0, sedpEncrypted.Body, 0, iOutLength);

            byte[] bESK = esksKeys.GetEncoded();
            byte[] bEncrypted = sedpEncrypted.Generate();

            byte[] bReturn = new byte[bESK.Length + bEncrypted.Length];
            bESK.CopyTo(bReturn, 0);
            bEncrypted.CopyTo(bReturn, bESK.Length);

            wWorking.Progress(10);
            string strReturn = Radix64.Encode(bReturn, true);

            strReturn = Armor.WrapMessage(strReturn);

            wWorking.Hide();
            return strReturn;
        }
Exemplo n.º 6
0
        public static void EncryptFiles(String[] strFiles, PublicKeyRing pkrPublicKeyRing, SecretKeyRing skrSecretKeyRing, bool bEncrypt, bool bSign)
        {
            PublicKeySelector pksSelectKeys = new PublicKeySelector(pkrPublicKeyRing);
            if (bEncrypt) {
                pksSelectKeys.ShowDialog();
                if (pksSelectKeys.SelectedKeys.Count == 0) {
                    MessageBox.Show("You did not select a public key to encrypt to. Doing nothing...", "Nothing Done...");
                    return;
                }
            }

            TransportableSecretKey tskKey = new TransportableSecretKey();
            string strPassphrase = "";

            if (bSign) {
                QueryPassphrase qpPassphrase = new QueryPassphrase();
                qpPassphrase.ShowMyDialog(skrSecretKeyRing);
                tskKey = qpPassphrase.SelectedKey;
                strPassphrase = qpPassphrase.Passphrase;
            }

            Working wWorking = new Working();
            wWorking.Show();

            for (int i=0; i<strFiles.Length; i++) {
                byte[] bFileContent = new byte[0];
                try {
                    System.IO.FileStream fsFile = new FileStream(strFiles[i], FileMode.Open);
                    BinaryReader brReader = new BinaryReader(fsFile);
                    bFileContent = brReader.ReadBytes((int)fsFile.Length);
                    brReader.Close();
                    fsFile.Close();
                } catch (Exception e) {
                    wWorking.Hide();
                    MessageBox.Show("An error occured while opening the file " + strFiles[i] + ": " + e.Message, "Error...");
                    return;
                }

                LiteralMessage lmMessage = new LiteralMessage(DataFormatTypes.Binary);
                lmMessage.Binary = bFileContent;
                lmMessage.TimeCreated = DateTime.Now;
                int iLastBackslash = strFiles[i].LastIndexOf("\\");
                lmMessage.Filename = strFiles[i].Substring(iLastBackslash + 1, strFiles[i].Length - iLastBackslash - 1);

                SharpPrivacy.OpenPGP.Messages.Message mEncryptionMessage = lmMessage;

                if (bSign) {
                    SignedMessage smMessage = new SignedMessage();
                    smMessage.MessageSigned = lmMessage;
                    SignaturePacket spPacket = new SignaturePacket();
                    spPacket.Version = SignaturePacketVersionNumbers.v3;
                    SecretKeyPacket skpKey = tskKey.FindKey(AsymActions.Sign);
                    spPacket.KeyID = skpKey.PublicKey.KeyID;
                    spPacket.HashAlgorithm = HashAlgorithms.SHA1;
                    spPacket.SignatureAlgorithm = skpKey.PublicKey.Algorithm;
                    spPacket.TimeCreated = DateTime.Now;
                    spPacket.SignatureType = SignatureTypes.TextSignature;
                    spPacket.Sign(lmMessage.Binary, skpKey, strPassphrase);
                    smMessage.Signature = spPacket;
                    mEncryptionMessage = smMessage;
                }

                CompressedMessage cmMessage = new CompressedMessage();
                cmMessage.Compress(mEncryptionMessage);

                wWorking.Progress(20/strFiles.Length);

                SymAlgorithms saAlgo = GetSymAlgorithmPreferences(pksSelectKeys.SelectedKeys);

                wWorking.Progress(10/strFiles.Length);
                byte[] bReturn = new byte[0];
                if (bEncrypt) {
                    SymmetricallyEncryptedDataPacket sedpEncrypted = new SymmetricallyEncryptedDataPacket();
                    SymmetricAlgorithm saEncrypt = CipherHelper.CreateSymAlgorithm(saAlgo);
                    saEncrypt.Mode = CipherMode.OpenPGP_CFB;
                    saEncrypt.GenerateKey();
                    byte[] bKey = saEncrypt.Key;

                    ESKSequence esksKeys = new ESKSequence();
                    try {
                         esksKeys = CreateESKSequence(pksSelectKeys.SelectedKeys, AsymActions.Encrypt, saAlgo, bKey);
                    } catch (Exception e) {
                        wWorking.Hide();
                        MessageBox.Show("The following error occured: " + e.Message, "Error...");
                        return;
                    }

                    wWorking.Progress(50/strFiles.Length);

                    ICryptoTransform ictEncryptor = saEncrypt.CreateEncryptor();
                    byte[] bMessage = cmMessage.GetEncoded();
                    byte[] bOutput = new byte[bMessage.Length];
                    ictEncryptor.TransformBlock(bMessage, 0, bMessage.Length, ref bOutput, 0);
                    bKey.Initialize();

                    wWorking.Progress(10/strFiles.Length);

                    int iOutLength = (saEncrypt.BlockSize >> 3) + 2 + bMessage.Length;
                    sedpEncrypted.Body = new byte[iOutLength];
                    Array.Copy(bOutput, 0, sedpEncrypted.Body, 0, iOutLength);

                    byte[] bESK = esksKeys.GetEncoded();
                    byte[] bEncrypted = sedpEncrypted.Generate();

                    bReturn = new byte[bESK.Length + bEncrypted.Length];
                    bESK.CopyTo(bReturn, 0);
                    bEncrypted.CopyTo(bReturn, bESK.Length);
                } else {
                    wWorking.Progress(60/strFiles.Length);
                    bReturn = cmMessage.GetEncoded();
                }

                wWorking.Progress(10/strFiles.Length);

                try {
                    FileStream fsOut = new FileStream(strFiles[i] + ".asc", FileMode.CreateNew);
                    BinaryWriter bwWrite = new BinaryWriter(fsOut);

                    bwWrite.Write(bReturn);
                    bwWrite.Close();
                    fsOut.Close();
                } catch (IOException io) {
                    MessageBox.Show("Could not write to file. The following error occured: " + io.Message, "Error...");
                }
            }

            wWorking.Hide();
        }
Exemplo n.º 7
0
        void mnuFileSystemEncrypt_Click(Object sender, System.EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofdOpen = new System.Windows.Forms.OpenFileDialog();

            ofdOpen.Multiselect = true;
            ofdOpen.Filter = "All Files (*.*)|*.*";
            ofdOpen.ShowDialog();

            if (ofdOpen.FileNames.Length == 0)
                return;

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);
            pksSelector.ShowDialog();

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            for (int i=0; i<ofdOpen.FileNames.Length; i++) {
                string strPath = ofdOpen.FileNames[i];
                SharpPrivacy.ReloadKeyRing();
                SharpPrivacy.Instance.EncryptFile(strPath, strPath + ".asc", lTargetKeyIDs);
            }
        }
Exemplo n.º 8
0
        void mnuFileSystemEncryptSign_Click(Object sender, System.EventArgs e)
        {
            // Check if we have some secret keys at all.
            XmlNodeList xnlSecretKeys = SharpPrivacy.SecretKeyRing.GetElementsByTagName("SecretKey");

            if (xnlSecretKeys.Count == 0) {
                MessageBox.Show("You cannot sign data because you do not own a private key. Please generate a new key pair, before you sign data.", "Action not possible...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            System.Windows.Forms.OpenFileDialog ofdOpen = new System.Windows.Forms.OpenFileDialog();

            ofdOpen.Multiselect = true;
            ofdOpen.Filter = "All Files (*.*)|*.*";
            ofdOpen.ShowDialog();

            if (ofdOpen.FileNames.Length == 0)
                return;

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);
            pksSelector.ShowDialog();

            QueryPassphrase qpPassphrase = new QueryPassphrase();
            qpPassphrase.ShowMultiKeyDialog(SharpPrivacy.SecretKeyRing);
            ulong lSignatureKeyID = qpPassphrase.SelectedKey;
            string strPassphrase = qpPassphrase.Passphrase;

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            for (int i=0; i<ofdOpen.FileNames.Length; i++) {
                string strPath = ofdOpen.FileNames[i];
                SharpPrivacy.ReloadKeyRing();
                SharpPrivacy.Instance.EncryptAndSignFile(strPath, strPath + ".asc", lTargetKeyIDs, lSignatureKeyID, strPassphrase);
            }
        }
Exemplo n.º 9
0
        void mnuClipboardEncrypt_Click(Object sender, System.EventArgs e)
        {
            string strMessage = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);
            pksSelector.ShowDialog();

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            try {
                startWorking();
                SharpPrivacy.ReloadKeyRing();
                string strReturn = SharpPrivacy.Instance.EncryptText(strMessage, lTargetKeyIDs);
                Clipboard.SetDataObject(strReturn);
                stopWorking();
            } catch (Exception ex) {
                MessageBox.Show("An error occured while encrypting the message: " + ex.Message + "\n\n" + ex.StackTrace, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }
Exemplo n.º 10
0
        void mnuClipboardEncryptSign_Click(Object sender, System.EventArgs e)
        {
            // Check if we have some secret keys at all.
            XmlNodeList xnlSecretKeys = SharpPrivacy.SecretKeyRing.GetElementsByTagName("SecretKey");

            if (xnlSecretKeys.Count == 0) {
                MessageBox.Show("You cannot sign data because you do not own a private key. Please generate a new key pair, before you sign data.", "Action not possible...", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            string strMessage = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();

            PublicKeySelector pksSelector = new PublicKeySelector(SharpPrivacy.PublicKeyRing);
            pksSelector.ShowDialog();

            ulong[] lTargetKeyIDs = (ulong[])pksSelector.SelectedKeys.ToArray(Type.GetType("System.UInt64"));

            QueryPassphrase qpPassphrase = new QueryPassphrase();
            qpPassphrase.ShowMultiKeyDialog(SharpPrivacy.SecretKeyRing);
            ulong lSignatureKeyID = qpPassphrase.SelectedKey;
            string strPassphrase = qpPassphrase.Passphrase;

            try {
                SharpPrivacy.ReloadKeyRing();
                string strReturn = SharpPrivacy.Instance.EncryptAndSignText(strMessage, lTargetKeyIDs, lSignatureKeyID, strPassphrase);
                Clipboard.SetDataObject(strReturn);
            } catch (Exception ex) {
                MessageBox.Show("An error occured while encrypting the message: " + ex.Message + "\n\n" + ex.StackTrace, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }