null cipher

You are currently browsing articles tagged null cipher.

Ah Yes. The Null Cipher. This method of enciphering a secret is obviously not safe, but it is still an effective steganographic method of hiding information.

The following is a C++ implementation of a basic Null Cipher. Read on to find out how to use it.

/*-----Greymask.com--------*/

#include <iostream>
#include <cstdlib>
#include <cstring>

/* usage message */
int printUsage(int argc, char **argv)
{
	std::cerr << "\nUsage : " << argv[0] 
		<< " [-e] [-d] [null] < filename" 
		<< "\n\n-e:\tencrypt message\n-d:\tdecrypt message\nnull:\t" 
		<< "number of null characters to pad encyphered message\n" 
		<< std::endl;
	return 1;
}

// generate random characters
char genRandomChar()
{
	char ch;

	int n = (std::rand() % 52);
	if (n < 26) {
		ch = (char) ('a' + n);
	} else {
		ch = (char) ('A' + n - 26);
	}

	return ch;
}

int main(int argc, char *argv[])
{
	
// initialize random seed
	std::srand(time(0));

	int null_pad = 0;

	if (argc < 3) {

	/* print Usage message upon incorrect input */
		printUsage(argc, argv);

	} else {

	/* store null shift for later use */
		null_pad = atoi(argv[2]);

	/* decrypt cipher text */
		if (strcmp(argv[1], "-d") == 0) {

		/* keep character count, ignore first character */
			int lc = -1;
			do {
				int c = std::cin.get();
				++lc;

		/* print every N character, where N = null_pad */
				if (lc == null_pad) {
					std::cout.put(c);
			/* reset character count */
					lc = -1;
				}

			} while (!std::cin.eof());
		}

	/* encrypt cipher text */
		if (strcmp(argv[1], "-e") == 0) {
			char c = std::cin.get();

			while (!std::cin.eof()) {
				for (int i = -1; i < (null_pad - 1); ++i) {
					std::cout << genRandomChar();
				}
				std::cout << c;
				c = std::cin.get();
			}
		}
	}
	return 0;
}

Usage:

Usage : ./a.out [-e] [-d] [null] < filename

-e:	encrypt message
-d:	decrypt message
null:	number of null characters to pad encyphered message

When you are running this program, you will have to indicate the number of characters that will “pad” your plaintext (n).

You will also need to redirect your plaintext (ie: secret message) file (filename) into the programs input stream to get a valid result back.

note: this program does NOT take in standard input, rather, the plaintext message is taken from an existing file.

(Alternatively, you can redirect the output from another program by using standard unix redirection like < or pipe |)

Let’s see some sample output. If we have the following plaintext: (a quote by Hyman G. Rickover)

(plaintext)


Sit down before fact with an open mind.
Be prepared to give up every preconceived notion.
Follow humbly wherever and to whatever abyss Nature leads or you learn nothing.
Don’t push out figures when facts are going in the opposite direction.

Then..I could encipher this message using our Null Cipher program by using n=3 like this:

./a.out -e 3 < plaintext_quote.txt

Which will result in the following:

uNWSvXdiiUntGlG qKjdBCPouJLwFKonGxr cbwbGHueybkfYMhotEtrgdSeMlB nxxfvNUaHNlcrwtthIL JaRwYrRiuXptGFIhbcu
QqIallenZLJ fjNoBEapWgfedHEnyUz QpRmuaBiBWInZlTdWpb.EXW hSLBNwueuRg iNbplssrNOYekrGpUHraVUbrjJzeqMOdYbh tBZtPkFomjt BPrgFuXicZXvesmecYW daiuChspKgO KydekONvzhveQTRrriDyJnh
lsupyUFrXfdemMtcgKKobjUnDHPcrypeZQeiRuHvGkNeIfVdauy zwanoHyoNwKtvHFitlJoZFwnXRt.lHK 
yfnFXCRoEmPlnrUlywkoiflwhta ZxjhiJkuvgTmlGkbKxUlpxlysLy CUowWjrhGlweJQMrVoveNaBvVlNeCvAryyS 
WgMakaRnLGmdeAU kputqSioHqx kKFwnHrhAaOarMltHYOegULvfKdeLhorYFM SjdaiwQbKNAywUhstoHsaLo 
KrINCCqahGxtmaSutlZrNoTemUK lTylreleRfjazMgdkcEsjqQ KNjomRSrzbg GhbypjPoXdOuMuZ JpYlguMeeTZaCeHrRQlnoGy 
RivnfLUoKEktvPghMCviMeGnWEGgdhw.vfU HsJDGruosmSnSVm�NSf�EKM�vavtNCE VVKpaolukdfsWQthHRz vNsoeEluaWXtMmq
 oEPfmwVijjVgAOVuvflrlvteSsPsEyV OLgwdoohzXvenpfnQLv wtCfzjnaJVscRJRtDDZsQQs EaSaxdbrDYweYRW 
PoMgucPoLcuiDuCnKVmgGPe UYLiztUnDSh btAtDiYhCKCeCgr pjVofYKpnuppiYeoOWWsonBiFjWtcMCeIoh 
UujdMOTiFJCrJIWedzMclHRtsWVixUWoHrEnhGn.bRM

As you can see..by issuing n=3, we have “padded” our original plaintext with 3 random characters to make the message unreadable. As always, we can decrypt it be reversing the process like this:

./a.out -d 3 < encrypted_quote.txt

Which will result in..


Sit down before fact with an open mind.
Be prepared to give up every preconceived notion.
Follow humbly wherever and to whatever abyss Nature leads or you learn nothing.
Don’t push out figures when facts are going in the opposite direction.

This may seem difficult to read, so let me encipher the same message with n=1..

./a.out -e 1 < plaintext_quote.txt

The first part of the result will look like …
GSMiEtv wdhoUwFnM pbceafaoIrYeM ……..

Take a close look.. I have bolded some letters from the first three words of the quote (“Sit down before..”) so you could see it easily. Notice how there is one extra character (n=1) in-between the actual message.

That’s what the Null Cipher is all about.

Neat huh?