TEA Encrypt To PB
Posted: Tue Nov 17, 2009 9:08 am
Hello, I'm j50501313 and I'm new to PureBasic. While I really enjoy what I see so far,
I stumbled across this piece of C code that I'd like to convert to PB.
I wonder if someone could please help me out. Thank you in advance.
I stumbled across this piece of C code that I'd like to convert to PB.
Code: Select all
#include <winsock.h>
#define ROUNDS 16
#define DELTA 0x9e3779b9
void Encrypt(long* v, long* k, long* out);
void Decrypt(long* v, long* k, long* out);
int main(int argc, char* argv[])
{
long v[2], k[4], out[2];
v[0]=0x97849FC5;
v[1]=0xC663ADEA;
k[0]=0x3A9DAA0A;
k[1]=0xCE16881A;
k[2]=0xFD045661;
k[3]=0xBCF5C5D4;
QQEncrypt(v,k,out);
QQDecrypt(out,k,out);
return 0;
}
void Encrypt(long* v, long* k, long* out)
{
unsigned long y=v[0],z=v[1],x[4],sum=0;
long limit=ROUNDS;
long test1;
y=ntohl(y);
z=ntohl(z);
for (int i=0;i<4;i++)
x[i]= ntohl(k[i]);
while (limit-->0)
{
sum += DELTA ;
test1=(z>>5);
y += (z<<4)+x[0] ^ z+sum ^ (z>>5)+x[1] ;
z += (y<<4)+x[2] ^ y+sum ^ (y>>5)+x[3] ;
}
out[0]=ntohl(y) ;
out[1]=ntohl(z) ;
}
void Decrypt(long* v, long* k, long* out)
{
unsigned long y=v[0],z=v[1],x[4],sum=0xe3779b90;
long limit=ROUNDS;
y=ntohl(y);
z=ntohl(z);
for (int i=0;i<4;i++)
x[i]= ntohl(k[i]);
while (limit-->0)
{
z -= (y<<4)+x[2] ^ y+sum ^ (y>>5)+x[3] ;
y -= (z<<4)+x[0] ^ z+sum ^ (z>>5)+x[1] ;
sum -= DELTA ;
}
out[0]=ntohl(y) ;
out[1]=ntohl(z) ;
}