Beispiel #1
0
        private void output_keys(RopBind rop)
        {
            int alt = rop.tagging();

            try {
                // initialize
                RopSession ses = rop.create_session(RopBind.KEYSTORE_GPG, RopBind.KEYSTORE_GPG);

                RopInput keyfile = null;
                try {
                    // load keyrings
                    keyfile = rop.create_input("pubring.pgp");
                    // actually, we may exclude the public  to not check key types
                    ses.load_keys_public(RopBind.KEYSTORE_GPG, keyfile);
                } catch (RopError ex) {
                    Console.WriteLine("Failed to read pubring");
                    throw ex;
                } finally {
                    rop.drop(keyfile);
                }

                keyfile = null;
                try {
                    keyfile = rop.create_input("secring.pgp");
                    ses.load_keys_secret(RopBind.KEYSTORE_GPG, keyfile);
                } catch (RopError ex) {
                    Console.WriteLine("Failed to read secring");
                    throw ex;
                } finally {
                    rop.drop(keyfile);
                }

                try {
                    // print armored keys to the stdout
                    print_key(rop, ses, "rsa@key", false);
                    print_key(rop, ses, "rsa@key", true);
                    print_key(rop, ses, "25519@key", false);
                    print_key(rop, ses, "25519@key", true);
                } catch (Exception ex) {
                    Console.WriteLine("Failed to print armored key(s)");
                    throw ex;
                }

                try {
                    // write armored keys to the files, named key-<keyid>-pub.asc/named key-<keyid>-sec.asc
                    export_key(rop, ses, "rsa@key", false);
                    export_key(rop, ses, "rsa@key", true);
                    export_key(rop, ses, "25519@key", false);
                    export_key(rop, ses, "25519@key", true);
                } catch (Exception ex) {
                    Console.WriteLine("Failed to write armored key(s) to file");
                    throw ex;
                }
            } finally {
                rop.drop_from(alt);
            }
        }
Beispiel #2
0
        private void encrypt(RopBind rop)
        {
            int alt = rop.tagging();

            try {
                // initialize
                RopSession ses = rop.create_session(RopBind.KEYSTORE_GPG, RopBind.KEYSTORE_GPG);

                RopInput keyfile = null;
                try {
                    // load public keyring - we do not need secret for encryption
                    keyfile = rop.create_input("pubring.pgp");
                    // we may use secret=True and public=True as well
                    ses.load_keys_public(RopBind.KEYSTORE_GPG, keyfile);
                } catch (RopError ex) {
                    Console.WriteLine("Failed to read pubring");
                    throw ex;
                } finally {
                    rop.drop(keyfile);
                }

                try {
                    // create memory input and file output objects for the message and encrypted message
                    RopInput  input  = rop.create_input(new RopData(message), false);
                    RopOutput output = rop.create_output("encrypted.asc");
                    // create encryption operation
                    RopOpEncrypt encrpt = ses.op_encrypt_create(input, output);

                    // setup encryption parameters
                    encrpt.set_armor(true);
                    encrpt.set_file_name("message.txt");
                    encrpt.set_file_mtime(DateTime.Now);
                    encrpt.set_compression("ZIP", 6);
                    encrpt.set_cipher(RopBind.ALG_SYMM_AES_256);
                    encrpt.set_aead("None");

                    // locate recipient's key and add it to the operation context. While we search by userid
                    // (which is easier), you can search by keyid, fingerprint or grip.
                    RopKey key = ses.locate_key("userid", "rsa@key");
                    encrpt.add_recipient(key);
                    // add encryption password as well
                    encrpt.add_password("encpassword", RopBind.ALG_HASH_SHA256, 0, RopBind.ALG_SYMM_AES_256);

                    // execute encryption operation
                    encrpt.execute();

                    Console.WriteLine("Encryption succeded. Encrypted message written to file encrypted.asc");
                } catch (RopError ex) {
                    Console.WriteLine("Encryption failed");
                    throw ex;
                }
            } finally {
                rop.drop_from(alt);
            }
        }