Encrypt or decrypt a string
#include <unistd.h>
void encrypt( char block[64],
int flag );
- block
- A 64-character array of binary values to encrypt.
The function stores the encrypted value in the same array.
- flag
- 0 if you want to encrypt block, nonzero to decrypt it.
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
 |
This function is in libc.a, but not in libc.so
(in order to save space). |
The encrypt() function uses
the NBS Data Encryption Standard (DES) algorithm
and the key you specify by calling
setkey()
to encrypt (if flags is zero) or decrypt (if flags is
nonzero) the given block of data.
 |
For more secure encryption, use OpenSSL instead. |
/* Run the program with argument -rw */
/* Run a different binary variant with -r */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#define ENCRYPT_DATA 0 /* 0 to encrypt block */
#define DECRYPT_DATA 1 /* nonzero to decrypt block */
#define MYFILE "/tmp/myfile"
/* Note: You must use a key, or else a new, random key will be selected
every day, thus making correct file decryption highly unlikely. */
const char ElsterKey[64] =
{ 0,0,1,0, 0,1,0,1, 0,1,0,1, 0,1,0,0,
0,0,1,1, 0,0,1,1, 0,0,1,1, 0,1,0,0,
0,0,1,0, 0,1,0,1, 0,0,1,1, 0,0,1,0,
0,0,1,0, 0,1,0,1, 0,1,0,1, 0,1,0,0 };
/*
* in == 8-byte string (expanded version of the 56-bit key)
* out == 64-byte string where each byte is either 1 or 0
* Note that the low-order "bit" is always ignored by by setkey()
*/
static void Expand(u_char *in, u_char *out)
{
int j, c;
int i;
for (i = 0; i < 64; in++){
c = *in;
for (j = 7; j >= 0; j--)
*out++ = (c >> j) & 01;
i += 8;
}
}
/* The inverse of Expand */
static void Collapse(u_char *in, u_char *out)
{
int j;
int i;
unsigned int c;
for (i = 0; i < 64; i += 8, out++) {
c = 0;
for (j = 7; j >= 0; j--, in++)
c |= *in << j;
*out = c & 0xff;
}
}
int main(int argc, char *argv[])
{
char crypt_file[64];
/* char orig[]={1,1,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1, *
* 0,0,0,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0}; */
char orig[]={"Welcome"};
char orig_bin[64], buf[8];
int opt, write_mode=0, read_mode=0, i;
FILE *fp;
while((opt = getopt(argc, argv, "wr")) != -1)
switch(opt)
{
case 'w':
write_mode = 1;
break;
case 'r':
read_mode = 1;
break;
default: ;
}
if ( write_mode )
printf( "Write mode\n" );
if ( read_mode )
printf( "Read mode\n" );
if (!read_mode && !write_mode){
printf( "Use arg -w or/and -r\n" );
exit(1);
}
setkey(ElsterKey);
if (write_mode)
{
printf("Original string: %s\n", orig);
Expand(orig, orig_bin);
encrypt(orig_bin, ENCRYPT_DATA);
unlink(MYFILE);
fp = fopen( MYFILE, "w" );
if( fp != NULL ) {
i = fwrite( orig_bin, sizeof( orig_bin ), 1, fp );
printf( "Successfully wrote %d records to %s\n", i, MYFILE );
fclose( fp );
}
encrypt(orig_bin, DECRYPT_DATA);
Collapse(orig_bin, buf);
printf("decypted %s\n", buf);
if (strcmp(orig, buf) == 0)
printf( "Strings are equal\n" );
else
printf( "Strings are NOT equal\n" );
}
if (read_mode)
{
fp = fopen( MYFILE, "r" );
if( fp != NULL ) {
i = fread( crypt_file, sizeof( crypt_file ), 1, fp );
fclose( fp );
encrypt(crypt_file, DECRYPT_DATA);
Collapse(crypt_file, buf);
printf("decypted from file: %s\n", buf);
if (strcmp(orig, buf) == 0)
printf( "Strings (file/mem) are equal\n" );
else
printf( "Strings (file/mem) are NOT equal\n" );
} else
printf( "No file %s\n", MYFILE );
}
return EXIT_SUCCESS;
}
POSIX 1003.1 XSI
Safety: | |
Cancellation point |
No |
Interrupt handler |
No |
Signal handler |
No |
Thread |
No |
crypt(),
setkey()