Encryption in Phobos?

kinghajj kinghajj_member at pathlink.com
Thu Mar 9 19:16:01 PST 2006


In article <op.s54vc4mspo9bzi at moe>, Chris Miller says...
>
>I guess it'd be ok to add encryption.
>
>I wrote a RC4 module in D a long time ago but didn't release it mainly  
>becuase I'm not sure how legal it is. Like  
>http://en.wikipedia.org/wiki/Rc4 says `The name "RC4" is trademarked,  
>however. The current status seems to be that "unofficial" implementations  
>are legal, but cannot use the RC4 name. RC4 is often referred to as  
>"ARCFOUR" (Alleged-RC4, because RSA has never officially released the  
>algorithm), to avoid possible trademark problems.` and `RC4 (or ARCFOUR)  
>is the most widely-used software stream cipher and is used in popular  
>protocols`.

Here's an XTEA implementation I made.

/*
Implementation of the XTEA encryption algorithm in D.
By Samuel Fredrickson.
Based on public domain C code by David Wheeler and Roger Needham.
See: http://en.wikipedia.org/wiki/XTEA
*/

/* This module is part of the public domain. Enjoy :) */

/*
Encrypts a block of data.
*/
private void encipherBlock(uint[2] v, uint[4] k)
{
uint v0 = v[0], v1 = v[1], i;
uint sum = 0, delta = 0x9E3779B9;

for(i = 0; i < 32; i++)
{
v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
sum += delta;
v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
}
v[0] = v0;
v[1] = v1;
}

/*
Decrypts a block of data.
*/
private void decipherBlock(uint[2] v, uint[4] k)
{
uint v0 = v[0], v1 = v[1], i;
uint sum = 0xC6EF3720, delta = 0x9E3779B9;

for(i = 0; i < 32; i++)
{
v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
sum -= delta;
v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
}
v[0] = v0;
v[1] = v1;
}

/*
Encrypts a block of data.
Unlike encipherBlock, this function takes char[] arrays, much more "D-like."
*/
void encryptBlock(char[8] data, char[16] key)
{
uint[2] v;
uint[4] k;

// place data into v
v[0] = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]);
v[1] = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | (data[7]);

// place key into k
k[0] = (key[0] << 24) | (key[1] << 16) | (key[2] << 8) | (key[3]);
k[1] = (key[4] << 24) | (key[5] << 16) | (key[6] << 8) | (key[7]);
k[2] = (key[8] << 24) | (key[9] << 16) | (key[10] << 8) | (key[11]);
k[3] = (key[12] << 24) | (key[13] << 16) | (key[14] << 8) | (key[15]);

// encrypt
encipherBlock(v, k);

data[0] = v[0];
data[1] = v[0] >> 8;
data[2] = v[0] >> 16;
data[3] = v[0] >> 24;
data[4] = v[1];
data[5] = v[1] >> 8;
data[6] = v[1] >> 16;
data[7] = v[1] >> 24;
}

/*
Decrypts a block of data.
Unlike decipherBlock, this function takes char[] arrays, much more "D-like."
*/
void decryptBlock(char[8] data, char[16] key)
{
uint[2] v;
uint[4] k;

// place data into v
v[0] = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]);
v[1] = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | (data[7]);

// place key into k
k[0] = (key[0] << 24) | (key[1] << 16) | (key[2] << 8) | (key[3]);
k[1] = (key[4] << 24) | (key[5] << 16) | (key[6] << 8) | (key[7]);
k[2] = (key[8] << 24) | (key[9] << 16) | (key[10] << 8) | (key[11]);
k[3] = (key[12] << 24) | (key[13] << 16) | (key[14] << 8) | (key[15]);

// encrypt
decipherBlock(v, k);

data[3] = v[0];
data[2] = v[0] >> 8;
data[1] = v[0] >> 16;
data[0] = v[0] >> 24;
data[7] = v[1];
data[6] = v[1] >> 8;
data[5] = v[1] >> 16;
data[4] = v[1] >> 24;
}





More information about the Digitalmars-d mailing list