public string Encrypt(string inText)
        {
            //Console.WriteLine("Encrypting " + inText + " using string keyring.");
            List <PgpPublicKey> encKeys = new List <PgpPublicKey>();

            encKeys.Add(PgpPublicKeyGetter.ReadPublicKey(this.publicKeyRing));

            Encryptor encryptor = new Encryptor(armor, withIntegrityCheck);
            string    outText   = encryptor.Encrypt(encKeys, inText);

            Console.WriteLine("Encryption done.");
            return(outText);
        }
        public void Encrypt(string inFile, string outFile)
        {
            Console.WriteLine("Encrypting " + inFile + " to " + outFile + " using string keyring.");
            List <PgpPublicKey> encKeys = new List <PgpPublicKey>();

            encKeys.Add(PgpPublicKeyGetter.ReadPublicKey(this.publicKeyRing));

            Stream inputStream  = File.OpenRead(inFile);
            Stream outputStream = File.OpenWrite(outFile);

            Encryptor encryptor = new Encryptor(armor, withIntegrityCheck);

            encryptor.Encrypt(encKeys, inputStream, outputStream);
            Console.WriteLine("Encryption done.");
        }
示例#3
0
        public static void DoIt()
        {
            string identity = "*****@*****.**";

            char[] passphrase = "my_passphrase".ToCharArray();

            PgpKeyPairGenerator generator =
                new PgpKeyPairGenerator(identity, passphrase, PublicKeyAlgorithm.RSA, PublicKeyLength.BITS_2048);
            PgpKeyPairHolder pgpKeyPairHolder = generator.Generate();

            String privateKeyring = pgpKeyPairHolder.PrivateKeyRing;
            String publicKeyring  = pgpKeyPairHolder.PublicKeyRing;

            Console.WriteLine(privateKeyring);
            Console.WriteLine(publicKeyring);

            PgpPublicKey        pgpPublicKey = PgpPublicKeyGetter.ReadPublicKey(publicKeyring);
            List <PgpPublicKey> encKeys      = new List <PgpPublicKey>();

            encKeys.Add(pgpPublicKey);

            string rootDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            string inFile  = rootDir + "\\safester_samples\\koala.jpg";
            string outFile = rootDir + "\\safester_samples\\koala.jpg.pgp";

            // This sample runs on Windows.
            // Use System.IO.File to open the in and out streams
            Stream inputStream  = File.OpenRead(inFile);
            Stream outputStream = File.OpenWrite(outFile);

            bool armor = false;
            bool withIntegrityCheck = true;

            // Create an Encryptor instance and pass the public keys and streams
            Encryptor encryptor = new Encryptor(armor, withIntegrityCheck);

            encryptor.Encrypt(encKeys, inputStream, outputStream);
            Console.WriteLine("Encryption done.");

            string inFileEncrypted  = rootDir + "\\safester_samples\\koala.jpg.pgp";;
            string outFileDecrypted = rootDir + "\\safester_samples\\koala_2.jpg";

            inputStream  = File.OpenRead(inFileEncrypted);
            outputStream = File.OpenWrite(outFileDecrypted);

            Decryptor decryptor = new Decryptor(privateKeyring, passphrase);

            decryptor.Decrypt(inputStream, outputStream);
            Console.WriteLine("Decryption integrity check status: " + decryptor.Verify);
            Console.WriteLine("Decryption done.");

            String inText = "For a long time I would go to bed early. Sometimes, " +
                            "the candle barely out, my eyes close so quickly that I did not have " +
                            "time to tell myself \"I’m falling asleep.\"";

            encryptor = new Encryptor(armor, withIntegrityCheck);
            string outText = encryptor.Encrypt(encKeys, inText);

            Console.WriteLine("Encryption done.");
            Console.WriteLine(outText);

            decryptor = new Decryptor(privateKeyring, passphrase);
            string decryptText = decryptor.Decrypt(outText);

            Console.WriteLine("Decryption integrity check status: " + decryptor.Verify);
            Console.WriteLine(decryptText);

            decryptText = decryptor.Decrypt(outText);
            Console.WriteLine(decryptText);

            decryptText = decryptor.Decrypt(outText);
            Console.WriteLine(decryptText);

            Console.WriteLine();
            Console.WriteLine("Press enter to close....");
            Console.ReadLine();
        }
        public static void DoIt()
        {
            Console.WriteLine(DateTime.Now + " Generate key pair...");
            PgpKeyPairGenerator pgpKeyPairGenerator = new PgpKeyPairGenerator(identity, passphrase.ToCharArray(), pgpAsymAlgo, pgpAsymKeyLength);
            PgpKeyPairHolder    pgpKeyPairHolder    = pgpKeyPairGenerator.Generate();

            List <PgpPublicKey> encKeys = new List <PgpPublicKey>();

            encKeys.Add(PgpPublicKeyGetter.ReadPublicKey(pgpKeyPairHolder.PublicKeyRing));

            Encryptor encryptor = new Encryptor(true, false);

            List <String> encryptedvalues = new List <string>();

            const int cpt = 200;

            Console.WriteLine(DateTime.Now + " Text encryption... In loop " + cpt + " times:");

            for (int i = 0; i < cpt; i++)
            {
                string clearValue     = "This_is_a_full_plain_subject_for_an_email_text_" + i;
                string encryptedValue = encryptor.Encrypt(encKeys, clearValue);
                encryptedvalues.Add(encryptedValue);
            }
            Console.WriteLine(DateTime.Now + " Text encryption done!");
            Console.WriteLine();

            Decryptor decryptor = new Decryptor(pgpKeyPairHolder.PrivateKeyRing, passphrase.ToCharArray());

            Console.WriteLine(DateTime.Now + " Starting decryption... In loop " + cpt + " times:");

            DateTime dtBegin = DateTime.Now;

            List <String> decryptedValues = new List <string>();

            for (int i = 0; i < cpt; i++)
            {
                string decryptedValue = decryptor.Decrypt(encryptedvalues[i]);
                decryptedValues.Add(decryptedValue);
            }

            DateTime dtEnd = DateTime.Now;
            TimeSpan span  = dtEnd - dtBegin;
            int      ms    = (int)span.TotalMilliseconds;

            Console.WriteLine(DateTime.Now + " Text decryption done!");
            Console.WriteLine(DateTime.Now + " Decryption Elasped in Milliseconds: " + ms);

            Console.WriteLine("Press enter to continue....");
            Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine("Display decrypted values: ");

            for (int i = 0; i < cpt; i++)
            {
                Console.WriteLine(decryptedValues[i]);
            }

            Console.WriteLine();
            Console.WriteLine("Done!");
            Console.WriteLine("Press enter to close....");
            Console.ReadLine();
        }