Has anyone used a DES package or written code to use the des utility to encrypt and decrypt data? I am looking for some leads in this regard. I am writing the code in C++ on Solaris 8 platform.
initkey(key);
if ((fi = fopen(infile, "rb") != NULL) {
if ((fo = fopen(outfile, "wb") != NULL) {
while (!feof(fi)) {
memset(blk, 0, 8);
if (fread(blk, 1, 8, fi) != 0) {
if (mode[0]=='e')
encrypt(blk);
else
decrypt(blk);
fwrite(blk, 1, 8, fo);
}
}
fclose(fo);
}
fclose(fi);
}
}
/* -------- make a character odd parity ---------- */
static unsigned char oddparity(unsigned char s)
{
unsigned char c = s | 0x80;
while (s) {
if (s & 1)
c ^= 0x80;
s = (s >> 1) & 0x7f;
}
return c;
}
/* ------ make a key odd parity ------- */
void setparity(char *key)
{
int i;
for (i = 0; i < 8; i++)
*(key+i) = oddparity(*(key+i));
}
/* ------- inverse permute a 64-bit string ------- */
static void inverse_permute(long *op,long *ip,long *tbl,int n)
{
int i;
long *pt = (long *)Pmask;
*op = *(op+1) = 0;
for (i = 0; i < n; i++) {
if ((*ip & *pt) || (*(ip+1) & *(pt+1))) {
*op |= *tbl;
*(op+1) |= *(tbl+1);
}
tbl += 2;
pt += 2;
}
}
/* ------- permute a 64-bit string ------- */
static void permute(long *op, long *ip, long *tbl, int n)
{
int i;
long *pt = (long *)Pmask;
*op = *(op+1) = 0;
for (i = 0; i < n; i++) {
if ((*ip & *tbl) || (*(ip+1) & *(tbl+1))) {
*op |= *pt;
*(op+1) |= *(pt+1);
}
tbl += 2;
pt += 2;
}
}
/* ----- Key dependent computation function f(R,K) ----- */
static long f(long blk, struct ks key)
{
struct LR ir;
struct LR or;
int i;
union {
struct LR f;
struct ks kn;
} tr = {0,0}, kr = {0,0};
ir.L = blk;
ir.R = 0;
kr.kn = key;
swapbyte(&ir.L);
swapbyte(&ir.R);
permute(&tr.f.L, &ir.L, (long *)Etbl, 48);
tr.f.L ^= kr.f.L;
tr.f.R ^= kr.f.R;
/* the DES S function: ir.L = S(tr.kn); */
ir.L = 0;
for (i = 0; i < 8; i++) {
long four = fourbits(tr.kn, i);
ir.L |= four << ((7-i) * 4);
}
swapbyte(&ir.L);
/* ------- extract a 4-bit stream from the block/key ------- */
static int fourbits(struct ks k, int s)
{
int i = sixbits(k, s);
int row, col;
row = ((i >> 4) & 2) | (i & 1);
col = (i >> 1) & 0xf;
return stbl[row][col];
}
/* ---- extract 6-bit stream fr pos s of the block/key ---- */
static int sixbits(struct ks k, int s)
{
int op = 0;
int n = (s);
int i;
for (i = 0; i < 2; i++) {
int off = ex6[n][0];
unsigned char c = k.ki[off];
c >>= ex6[n][1];
c <<= ex6[n][2];
c &= ex6[n][3];
op |= c;
}
return op;
}
/* ---------- DES Key Schedule (KS) function ----------- */
static struct ks KS(int n, char *key)
{
static unsigned char cd[8];
static int its[] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
union {
struct ks kn;
struct LR filler;
} result;
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.