gpgme C++ API (GnuPG)

gpgme C++ API (GnuPG)
//rm -frv encrypt_message ; g++-7 -g -fPIC -Wall -lgpgme encrypt_message.cpp -o encrypt_message -static-libstdc++; //GPG Key generieren######################################################### /* time gpg –batch –gen-key <<EOF Key-Type: RSA Key-Length: 1024 Name-Real: gpg-dev Name-Comment: Admin Name-Email: root@bit-devil.ddns.net Passphrase: 123 EOF */ //######################################################################## #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> #include <stdio.h> #include <string> #include <string.h> #include <cstring> //algorithm fürs string klein machen #include <algorithm> //Zum Casten von Char Hex Array nach string (bei md5sum) #include <iomanip> #include <iostream> #include <vector> #include <sys/time.h> #include <sstream> #include <fstream> #include <pthread.h> #include <thread> #include <time.h> #include <stdlib.h> #include <errno.h> #include <locale.h> #include <gpgme.h> #define SIZE 1024 using namespace std; int main(int argc, char *argv[]) { //### https://github.com/yaccz/code-samples-c-gpgme-encrypt/blob/master/main.c char buf[SIZE]; gpgme_check_version (NULL); gpgme_error_t err; gpgme_data_t plain, cipher; gpgme_ctx_t ctx; gpgme_key_t recp[2] = { NULL, NULL }; gpgme_encrypt_flags_t flags = GPGME_ENCRYPT_ALWAYS_TRUST; gpgme_new (&ctx); gpgme_set_armor (ctx, 1); char *fp = “6AA609E8”; //gpg –list-keys -> “pub 1024R/29EEBC53 2018-09-16” gpgme_get_key (ctx, fp, &recp[0], 0); char *plaintext = “##### BLOCK CHAIN HASHWERT #####\0”; err = gpgme_data_new_from_mem (&plain, plaintext, strlen(plaintext), 0); cout<<“gpgme_data_new_from_mem”<<err<<endl; gpgme_data_new (&cipher); gpgme_op_encrypt (ctx, recp, flags, plain, cipher); gpgme_data_seek(cipher, 0, SEEK_SET); size_t max_buflen = 2048, buflen; //buflen = gpgme_data_read(cipher, buf, max_buflen); //cout<<“buflen gpgme_data_read”<<buflen<<endl; int ret; ret = gpgme_data_seek (cipher, 0, SEEK_SET); while ((ret = gpgme_data_read (cipher, buf, max_buflen)) > 0) { // Write the contents of “buf” to “outputFile” //fwrite (buf, ret, 1, outputFile); cout<<buf; } //gpgme_data_release (cipher); //gpgme_data_release (plain); //gpgme_release (ctx); //########################################################################## /* free data */ //gpgme_data_release(data); /* free context */ //gpgme_release(ctx); return(0); }

Post Revisions:

Tags:  , ,