#include <unistd.h>
|
void
encrypt (char block[64], int edflag); |
#include <stdlib.h>
|
void
setkey (const char *key); |
setkey() is invoked to set the key for the DES machine. setkey()’ s key parameter is an array of 64 bytes, and the numerical value of each byte in this array is either 0 or 1. The 56-bit key for the DES algorithm is computed from the key parameter.
encrypt function either encrypts or decrypts the data block. The exact operation depends on the value of edflag parameter. block is encrypted if edflag parameter is 0, and decrypted if 1 is being passed as the value of edflag parameter. block is an array of 64 bytes, wherein the numerical value of each byte is either 0 or 1. Like the setkey’ s key parameter, block is a bit vector representation of the actual value that is encoded. It is modified in place to return the result.
encrypt and setkey are not reentrant as the key is stored statically.
#include <stdlib.h> #include <unistd.h> void encrypt_user() { /* bit vector containing the key */ char key[64] = { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0 }; /* bit vector containing the data block to be encrypted */ char block[64] = { 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0 }; setkey(key); /* Set the key for DES encryption */ /* Perform encryption/decryption of the message block */ encrypt(block, 0); /* Encryption. The input block is modified in place * to return the output to the user */ encrypt(block, 1); /* Decryption. The input block is modified in place * to return the result of the decryption operation */ }
© 2005-2007 Nokia |