示例#1
0
        static int Encrypt(EncryptParameters p)
        {
            _arguments = p;

            EnsureCanOverwriteDestination(p);

            using (var decryptedStream = new FileStream(p.SourceFileName, FileMode.Open, FileAccess.Read, FileShare.None))
                using (var encryptedStream = new FileStream(p.DestinationFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    using (var cipher = new EncryptedNewKeyCipher(p.Certificate))
                    {
                        cipher.Base64Encoded = p.Base64Encoded;
                        cipher.Encrypt(decryptedStream, encryptedStream);
                    }

            return(0);
        }
示例#2
0
        static int Decrypt(DecryptParameters p)
        {
            _arguments = p;

            EnsureCanOverwriteDestination(p);

            var retryWithBase64 = false;

            do
            {
                try
                {
                    using (var encryptedStream = new FileStream(p.SourceFileName, FileMode.Open, FileAccess.Read, FileShare.None))
                        using (var decryptedStream = new FileStream(p.DestinationFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                            using (var cipher = new EncryptedNewKeyCipher(p.Certificate))
                            {
                                cipher.Base64Encoded = retryWithBase64;
                                cipher.Decrypt(encryptedStream, decryptedStream);
                                retryWithBase64 = false;
                            }
                }
                catch (ArgumentException x) when(x.ParamName == "encryptedStream")
                {
                    if (retryWithBase64)
                    {
                        throw;
                    }

                    using (var reader = new StreamReader(p.SourceFileName, Encoding.ASCII))
                    {
                        for (var i = 0; i < 5 && !reader.EndOfStream; i++)
                        {
                            var line = reader.ReadLine();
                            if (!Regex.IsMatch(line, _base64Regex))
                            {
                                throw;
                            }
                        }
                        retryWithBase64 = true;
                    }
                }
            }while (retryWithBase64);

            return(0);
        }
示例#3
0
文件: Program.cs 项目: gkowieski/vm
        static void Main(string[] args)
        {
            if (ParseArguments(args))
            {
                try
                {
                    var cert = GetCert(_certSubject, _certThumbprint);

                    using (var cipher = new EncryptedNewKeyCipher(cert))
                    {
                        if (_base64)
                        {
                            cipher.Base64Encoded = true;
                        }

                        if (_encrypt)
                        {
                            using (var decryptedStream = new FileStream(_clearText, FileMode.Open, FileAccess.Read, FileShare.None))
                                using (var encryptedStream = new FileStream(_encryptedText, FileMode.Create, FileAccess.Write, FileShare.None))
                                    cipher.Encrypt(decryptedStream, encryptedStream);
                        }

                        if (_decrypt)
                        {
                            using (var encryptedStream = new FileStream(_encryptedText, FileMode.Open, FileAccess.Read, FileShare.None))
                                using (var decryptedStream = new FileStream(_decryptedText, FileMode.Create, FileAccess.Write, FileShare.None))
                                    cipher.Decrypt(encryptedStream, decryptedStream);
                        }
                    }
                }
                catch (Exception x)
                {
                    Console.WriteLine(x.ToString());
                    Usage();
                }
            }

#if DEBUG
            Console.WriteLine(Resources.PressAnyKey);
            Console.ReadKey(true);
#endif
        }