Original Post
Hi, I was trying to convert the code here to C heres the simple code I don't really understand the technicalities behind the algorithm but the output ive been getting is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void encipher(const unsigned long *const v,unsigned long *const w,
const unsigned long * const k)
{
register unsigned long y=v[0],z=v[1],sum=0,delta=0x9E3779B9,n=32;
while(n-->0)
{
y += (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
sum += delta;
z += (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
}
w[0]=y; w[1]=z;
}
void decipher(const unsigned long *const v,unsigned long *const w,
const unsigned long * const k)
{
register unsigned long y=v[0],z=v[1],sum=0xC6EF3720,
delta=0x9E3779B9,n=32;
/* sum = delta<<5, in general sum = delta * n */
while(n-->0)
{
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
sum -= delta;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
}
w[0]=y; w[1]=z;
}
unsigned long ConvertStringToUInt(char *input)
{
unsigned long output=0;
output = ((unsigned long)input[0]);
output += ((unsigned long)input[1] << 8);
output += ((unsigned long)input[2] << 16);
output += ((unsigned long)input[3] << 24);
return output;
}
void ConvertUIntToString(unsigned long input,char *szOutput)
{
char tempChar;
char szReturn[64]={0};
tempChar = (char)input & 0xFF;
szReturn[0]=tempChar;
tempChar = (char)((input >> 8) & 0xFF);
szReturn[1]=tempChar;
tempChar = (char)((input >> 16) & 0xFF);
szReturn[2]=tempChar;
tempChar = (char)((input >> 24) & 0xFF);
szReturn[3]=tempChar;
sprintf(szOutput,"%s",szReturn);
}
void Decrypt(char* data, char *key, char*decrypted)
{
unsigned long tempData[2];
unsigned long returnData[2];
char szTemp[128]={0};
unsigned long longKey[64];
unsigned int i;
//printf(">>>>>>%d %s\n",strlen(key),key);
for(i=0;i<64;i++){longKey=0;}
for(i=0;i<strlen(key);i++)
{
longKey=key;
}
for(i=0;i<strlen(data);i+=8)
{
strncpy(szTemp,data+i,4);
tempData[0]=ConvertStringToUInt(szTemp);
strncpy(szTemp,data+i+4,4);
tempData[1]=ConvertStringToUInt(szTemp);
printf("> (%c-%c)\n",tempData[0],tempData[1]);
decipher(tempData,returnData,longKey);
printf("> (%c-%c)\n",returnData[0],returnData[1]);
}
}
void Encrypt(char* data,char *key,char* encrypted)
{
unsigned long tempData[2];
unsigned long tempData2[2];
unsigned long returnData[2];
char szTemp[128]={0};
unsigned long longKey[64];
unsigned int i;
//printf(">>>>>>%d %s\n",strlen(key),key);
if(strlen(data)%2!=0)
{
data[strlen(data)]='\0';
}
for(i=0;i<64;i++){longKey=0;}
for(i=0;i<strlen(key);i++)
{
longKey=key;
}
for(i=0;i<strlen(data);i+=2)
{
tempData[0]=data;
tempData[1]=data[i+1];
encipher(tempData,returnData,longKey);
printf("> (%c-%c)\n",returnData[0],returnData[1]);
decipher(returnData,tempData2,longKey);
printf("> (%c-%c)\n",tempData2[0],tempData2[1]);
ConvertUIntToString(returnData[0],szTemp);
strcat(encrypted,szTemp);
ConvertUIntToString(returnData[1],szTemp);
strcat(encrypted,szTemp);
}
}
void main()
{
char szEncrypted[128]={0};
char szDecrypted[128]={0};
char szString[128]={0};
char szKey[64]="thekey";
scanf("%s",szString);
Encrypt(szString,szKey,szEncrypted);
printf("Encrypted:%s\n",szEncrypted);
Decrypt(szEncrypted,szKey,szDecrypted);
printf("Decrypted:%s\n",szDecrypted);
}
Quote:as you can see, in the Encrypt function, i encipher the string, and decipher it straight away for debug purposes In the Decrypt function i print out the data being passed into the decipher function for debug purposes as shown above, the data being passed into the decipher function in function Encrypt and Decrypt is the same " -+" but the output is different My only guesses were, the key im passing in differs, or the encoding is related to variable address? Why does the decipher function behave so? any help would be appreciated Thanks in advance encipher and decipher functions taken from http://www.simonshepherd.supanet.com/tea.htm
hi > ( -+) > (h-i) Encrypted: S►}+ö¼♦ > ( -+) > (┤-°) Decrypted: