Example #1
0
        public void DecryptFile(string sourceName, string targetName)
        {
            var sourceInfo = new FileInfo(sourceName);

            if (!sourceInfo.Exists)
            {
                throw new FileNotFoundException("Source File was not found or has been deleted.", sourceName);
            }

            var targetInfo = new FileInfo(targetName);

            if (targetInfo.Exists)
            {
                var message = string.Format("Target File \"{0}\" already exists.", targetName);
                throw new InvalidOperationException(message);
            }

            SBUtils.Unit.SetLicenseKey(SBUtils.Unit.BytesOfString(License));

            var privateRing = new SBPGPKeys.TElPGPKeyring();

            lock (typeof(SBPGPKeys.TElPGPKeyring))
            {
                privateRing.Load(encryptionKey, signatureKey, true);
            }

            var reader = new TElPGPReader
            {
                DecryptingKeys = privateRing,
                VerifyingKeys  = privateRing,
                KeyPassphrase  = passphrase,
            };

            try
            {
                using (var sourceStream = sourceInfo.OpenRead())
                    using (var targetStream = targetInfo.Create())
                        using (reader)
                        {
                            reader.OutputStream = targetStream;
                            reader.DecryptAndVerify(sourceStream, 0);
                        }
            }
            catch (Exception)
            {
                targetInfo.Refresh();
                if (targetInfo.Exists)
                {
                    targetInfo.Delete();
                }

                throw;
            }
        }
Example #2
0
        public void EncryptFile(string sourceName, string targetName)
        {
            var sourceInfo = new FileInfo(sourceName);

            if (!sourceInfo.Exists)
            {
                throw new FileNotFoundException("Source File was not found or has been deleted.", sourceName);
            }

            var targetInfo = new FileInfo(targetName);

            if (targetInfo.Exists)
            {
                var message = string.Format("Target File \"{0}\" already exists.", targetName);
                throw new InvalidOperationException(message);
            }

            SBUtils.Unit.SetLicenseKey(SBUtils.Unit.BytesOfString(License));

            var publicKey = new SBPGPKeys.TElPGPPublicKey();

            publicKey.LoadFromFile(encryptionKey);

            var privateKey = new SBPGPKeys.TElPGPSecretKey();

            privateKey.LoadFromFile(signatureKey);
            privateKey.Passphrase = passphrase;

            var publicRing = new SBPGPKeys.TElPGPKeyring();

            publicRing.AddPublicKey(publicKey);

            var privateRing = new SBPGPKeys.TElPGPKeyring();

            privateRing.AddSecretKey(privateKey);

            var writer = new TElPGPWriter
            {
                Armor            = true,
                Compress         = false,
                CompressionLevel = 9,
                Filename         = Path.GetFileName(sourceName),
                EncryptionType   = TSBPGPEncryptionType.etPublicKey,
                Protection       = TSBPGPProtectionType.ptNormal,
                UseNewFeatures   = false,
            };

            writer.ArmorHeaders.Add("Version: PGP 6.5.2");
            writer.Passphrases.Add(passphrase);
            writer.EncryptingKeys = publicRing;
            writer.SigningKeys    = privateRing;

            try
            {
                using (var sourceStream = sourceInfo.OpenRead())
                    using (var targetStream = targetInfo.Create())
                        using (writer)
                        {
                            writer.EncryptAndSign(sourceStream, targetStream, 0);
                        }
            }
            catch (Exception)
            {
                targetInfo.Refresh();
                if (targetInfo.Exists)
                {
                    targetInfo.Delete();
                }

                throw;
            }
        }