Encrypting and Decrypting Documents

Install gnupg in Cygwin

First step is to install gnupg in Cygwin. Run setup.exe and use all default until you get to select packages. In the search type gnupg click on install then at bottom of screen click next to install. There are two Website that are good reads on the topic of GPG .

Encrypting and decrypting documents
GPG Quick Start

Generate a private key

gpg --gen-key

You’ll have to answer a bunch of questions:

What kind and size of key you want; the defaults are probably good enough.

How long the key should be valid. You can safely choose a non-expiring key for your own use. If you plan to use a key for public signing, you might want to consider a yearly expiration.

Your real name and e-mail address; these are necessary for identifying your key in a larger set of keys.

A comment for your key, perhaps to distinguish a key used for special tasks like signing software releases. The comment can be empty.

A passphrase. Whatever you do, don’t forget it! Your key, and all your encrypted files, will be useless if you do.

Export a key

After you generated a key, export it to a file so you can send in email or save on thumb drive.

gpg --armor --output pubkey.txt --export 'your@email.com'

Import a key

Import your friend’s key, which you might have received via e-mail or on a thumb drive. If the file is named pubkey.txt, then just use the –import option to add it to your keyring:

gpg --import pubkey.txt

Encrypting

Encrypt a file called sensitive.txt. The argument to the –recipient option should be the all or part of the name you used when generating your private key. The –output option is the name you want the gpg file called. Most likely you won’t need the –output option because it default to filename.gpg.

gpg --output sensitive.txt.gpg --encrypt --recipient jtltgl@gmail.com sensitive.txt

Decrypting

If someone sends you an encrypted file, the file has typically been encrypted using your public key. Decrypting it is no different than decrypting a file you have encrypted for your own use.

gpg --output sensitive.txt --decrypt sensitive.txt.gpg

Modified Makefile

# example Makefile for viewing/editing an encrypted file

GPGID = 'your.email.com'
FILEPLAIN = sensitive.txt
FILECRYPT = $(FILEPLAIN).gpg

GPG = gpg
RM = rm -i
VI = notepad
TOUCH = touch

all:

	@echo ""
	@echo "usage:"
	@echo ""
	@echo "* make touch -- Startup"
	@echo "* make view -- to see $(FILEPLAIN)"
	@echo "* make edit -- to edit $(FILEPLAIN)"
	@echo ""

edit:

	@umask 0077;\
	$(GPG) --output $(FILEPLAIN) --decrypt $(FILECRYPT)
	@$(VI) $(FILEPLAIN)
	@umask 0077;\
	$(GPG) --encrypt --recipient $(GPGID) $(FILEPLAIN)
	@$(RM) $(FILEPLAIN)

view:
	@umask 0077; $(GPG) --decrypt $(FILECRYPT)

touch:			# Run first if FILEPLAIN doesn't exist

	@umask 0077;\
	$(TOUCH) $(FILEPLAIN)
	$(VI) $(FILEPLAIN)

Here is Makefile for download copy to your Cygwin home directory. Download Now!  You must edit and fill in email address which was enter when you made key. To run just type:

make

Output:

usage:

* make touch -- Startup
* make view -- to see sensitive.txt
* make edit -- to edit sensitive.txt

When running “make view” answer y to a couple of question when quitting.

Let’s Compile in Cygwin

First you will need to install gcc, gcc-g++, gcc-fortran, gcc-java ,gccmakedep, make and cmake. I will show you how to install gcc you will have to repeat for all the rest. Run setup.exe this is the program you ran to install Cygwin. Use all the default setting until you get to the select package screen. Type gcc in search :

search

 

Under Devel click on gcc, gcc-fortran, gcc-g++ and gcc-java. Mine say’s Keep because it’s already installed most likely your’s will say Skip, click on box to install. Now repeat steps for make, cmake and gccmakedep. Now let’s compile I am going to use Notepad for demonstration purposes because that is on all Windows machine. I won’t go into great detail about the code snippets but will show you how to compile and run program. You can cut and paste but be aware sometimes illegal or invisible characters can be inserted and compiler will give error. I will have a link to examples at end of post for download. Let’s start with a C program first. Type this code snippet and save as poem.c:

/***********************************************************/
/* Short Poem                                              */
/***********************************************************/

#include <stdio.h>

/***********************************************************/

   main ()                    /* Poem */

   {
   printf ("Astronomy is %dderful \n",1);
   printf ("And interesting %d \n",2);
   printf ("The ear%d volves around the sun \n",3);
   printf ("And makes a year %d you \n",4);
   printf ("The moon affects the sur %d heard \n",5);
   printf ("By law of phy%d great \n",6);
   printf ("It %d when the the stars so bright \n",7);
   printf ("Do nightly scintill%d \n",8);
   printf ("If watchful providence be%d \n",9);
   printf ("With good intentions fraught \n");
   printf ("Should not keep up her watch divine \n");
   printf ("We soon should come to %d \n",0);
   }

If everything was install right all you have to do is type:

gcc -o poem.exe poem.c

To run it type:

./poem.exe

Output:

Astronomy is 1derful \n"
And interesting 2
The ear3 volves around the sun
And makes a year 4 you
The moon affects the sur 5 heard
By law of phy6d great
It 7 when the the stars so bright
Do nightly scintill8
If watchful providence be9
With good intentions fraught
Should not keep up her watch divine
We soon should come to 0

Notice in this code snippet has an assigned number appears at the %d. Thats just part of the printf function. You can read up on printf this is only an example. C++ is almost the same except you use g++ to compile. Type :

// 'Hello World!' program 

#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Save as hello.cpp and compile :

g++ -o hello.exe hello.cpp

To run it type:

./hello.exe

Output :

Hello World!

Now let’s try fortran:

program count

integer :: upto = 10 ! count to 10 by default
integer :: i ! iterator
integer :: retval ! return value stores program status
character(len=32) :: arg ! buffer for command line argument

call getarg(1, arg) ! get argv[0] (how does this differ from c++?)
print*,'Inside program ',arg

if (iargc() .gt. 0) then ! use first argument
call getarg(1, arg)
read(arg, FMT='(I5)') upto
endif

! perform my count
do i = 1,upto
print*,i
end do

if (i .gt. 20) then
retval = 1
else
retval = 0
endif

call exit(retval)

end program count

Save as count.f90 and compile:

gfortran.exe -o count.exe count.f90

To run it type:

./count.exe

Output :

Inside program
           1
           2
           3
           4
           5
           6
           7
           8
           9
          10

In this code snippet you can see that it’s looks for arg1 so while running you can put a number as the first argument. like ./count 20 , this will count to 20. The program defaults to 10 if no argument are given.

CMake is a family of tools designed to build, test and package software. You will need make installed to use this program.

Example file called count.cxx:

 

 

#include  <iostream.h>
#include  <stdio.h> 
using namespace std; 

int main (int argc, char** argv) {
	int upto = 10;  // count to 10 by default
	int i;          // iterator

	// Notice the use of argv[0] (how do we know its the program name?)
	cout << "Inside program " << argv[0] << endl; 	if (argc > 1) { // use first argument
		upto = atoi(argv[1]);
	}

	// perform my count
	for (i = 1; i <= upto; i++) {
		cout << i << endl; 	} 	if (i > 20) {
		return 1;  // what effect does this have? 
	}

	return 0; // successful execution
}

You will also need a file called CMakeList.txt:

PROJECT(countTo10 CXX) 

ADD_EXECUTABLE(count_cxx count.cxx)

To compile type:

Step 1) cmake .
Step 2) make

To run it type:

./count.cxx.exe

Output :

Inside program ./count_cxx
1
2
3
4
5
6
7
8
9
10

There are a lot more programs like PHP, Ruby, Perl, Tcl and Python all can be run from Cygwin console. I would recommend looking at an old article under cmdcenter categories called Recommended installs for use with CMDCenter. Just follow examples but instead of running with CMDCenter program just type in commands in Cygwin console. Hope you have as much fun as I do working with code and compilers. Here are the examples for download. Download Now! One note when you download from another computer Windows Blocks the file. You must right click on file under Properties and then click on Unblock. A better way is using this command “streams -s -d name_of_directory” without quotes. This is a tool I recommended in Sysinternals Suite. It will Unblock all files in the subdirectory.