![]() |
New Mexico Supercomputing Challenge | ||||||||||
|
|||||||||||
|
|
/* slant.c
This is an implementation of a simple transposition cipher.
See "Cryptography and Secure Communications" pp. 2, 3.
The basic premise behind this cipher is to take a phrase
like:
CRYPTOGRAPHY AND DATA SECURITY
and rearrange it in the following form:
C P G P A D A C I
R T R H N A S U T
Y O A Y D T E R Y
The top row is then put at the beginning of the enciphered
text, the next row follows, and so forth:
CPGPADACI RTRHNASUT YOAYDTERY -> CPGPADACIRTRHNASUTYOAYDTERY
This program will take a line of clear-text and output the
enciphered result.
FYI - This is one of the oldest cryptosystems. It was used
by the Greeks as early as 400 BC.
*/
/* This is the main part of the program */
#include <iostream.h>
void getCleartext(char[]);
void encrypt(char[], char[], int);
void printString(char[]);
int main (void)
{
char cleartext[41]; /* This is where we store the
clear-text that the user inputs.
It is an array of characters
that will store a string forty
characters long. (Don't forget
about the '\0'!) */
char ciphertext[41]; /* This is where we will store
our enciphered text. */
int key; /* This is the key to use to
encrypt the text. */
for (int i = 0; i < 40; i++)
ciphertext[i] = '!';
ciphertext[40] = '\0';
getCleartext(cleartext); /* This function will
get the cleartext
and store it in
our array. */
cout << "Enter the key to encrypt [integer less than the length of the message]: ";
cin >> key;
encrypt(cleartext, ciphertext, key); /* This will take the
text in cleartext,
encipher it, and
store the results in
ciphertext. */
printString(ciphertext); /* This will output the
text stored in
ciphertext. */
cout << "\n"; /* Output newline. */
return 0; /* Signal to the operating system that this
progam has finished running. */
}
/* This function will get the cleartext and store it in
our array. */
void getCleartext(char text[])
{
/* This prompts the user to enter information. */
cout << "Please enter some cleartext to encipher\n"
<< "(40 characters or less): ";
/* This gets the text and stores it in the text array... */
cin.getline(text, 40);
/* Signal that this function is finished. */
return;
}
/* This will take the text in cleartext, encipher it, and
store the results in ciphertext. */
void encrypt(char cleartext[], char ciphertext[], int slant)
{
/* In this example, we are going to only use a
slanting pattern three characters high. */
int j;
int length; /* Length of cleartext */
int level; /* Length of the largest row */
/*
<---------level--------->
C P G P A D A C I
R T R H N A S U T
Y O A Y D T E R Y
*/
int i; /* Used for various counting tasks */
/* This will go through the string and count until
it reaches the string terminator. */
for (length = 0; cleartext[length] != '\0'; length++);
/* If all rows will be the same length.... */
if (( (length) % slant) == 0)
level = (length) / slant;
/* If not, add one extra to level. */
else
level = ( (length) / slant ) + 1;
/* Now we do the bulk of the work. This part will
take the cleartext and encipher it by placing
it in the ciphertext array. */
for (i = 0; cleartext[i] != '\0'; i++)
{
ciphertext[ ((i % slant) * level) + (i / slant) ]
= cleartext[i];
}
ciphertext[i+slant] = '\0';
/* Removing empty spots in the code. */
for (i = 0; ciphertext[i] != '\0'; i++)
{
if (ciphertext[i] == '!')
{
ciphertext[i] = ' ';
}
}
ciphertext[i] = '\0';
/* Signal that function is complete. */
return;
}
/* This function outputs the string contained in ciphertext. */
void printString(char ciphertext[])
{
for (int i = 0; ciphertext[i] != '\0'; i++)
cout << ciphertext[i];
return;
}
| ||||||||||
|
For questions about the Supercomputing Challenge, a 501(c)3 organization, contact us at: consult @ challenge.nm.org New Mexico Supercomputing Challenge, Inc. Post Office Box 30102 Albuquerque, New Mexico 87190 (505) 667-2864 Supercomputing Challenge Board of Directors Board page listing meetings and agendas If you have volunteered for the Challenge, please fill out our In Kind form. |
|