DBPro Implementation of XXTEA (Tiny Encryption Algorithm) by The Sab19th Oct 2011 8:32
|
---|
Summary XXTEA is a low profile encryption algorithm created by Roger Needham and David Wheeler. It was expanded upon by Chris Veness in a java script to encrypt strings instead of just bin Description To encrypt a file call the 'encrypt( Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com function encrypt(plaintext as string, password as string) if len(plaintext) = 0 exitfunction "" endif ciphertext as string ciphertext = "" n as dword z as dword y as dword delta as dword mx as dword e as dword q as dword sum as dword if ceil(len(plaintext)/4.0) < 2 dim v(1) as dword else dim v(ceil(len(plaintext)/4.0) - 1) as dword endif dim k(ceil(16.0/4.0) - 1) as dword for i = 0 to array count(v()) v(i) = asc(mid$(plaintext, i*4+1)) + (asc(mid$(plaintext, i*4+2))<<8) + (asc(mid$(plaintext, i*4+3))<<16) + (asc(mid$(plaintext, i*4+4))<<24) next i for i = 0 to array count(k()) k(i) = asc(mid$(password, i*4+1)) + (asc(mid$(password, i*4+2))<<8) + (asc(mid$(password, i*4+3))<<16) + (asc(mid$(password, i*4+4))<<24) next i n = array count(v()) + 1 z = v(n-1) y = v(0) delta = 0x9E3779B9 q = floor(6.0 + 52.0/n) sum = 0 while q > 0 inc sum, delta e = sum>>2 && 3 for p = 0 to n-1 y = v((p+1) mod n) mx = (z>>5 ~~ y<<2) + (y>>3 ~~ z<<4) ~~ (sum~~y) + (k(p&&3 ~~ e) ~~ z) z = v(p) + mx v(p) = v(p) + mx next p dec q endwhile for i = 0 to array count(v()) ciphertext = ciphertext + chr$(v(i) && 0xFF) + chr$(v(i)>>8 && 0xFF) + chr$(v(i)>>16 && 0xFF) + chr$(v(i)>>24 && 0xFF) next i result$ = base64encode(ciphertext) undim v() undim k() endfunction result$ function decrypt(ciphertext as string, password as string) plaintext as string plaintext = "" n as dword z as dword y as dword delta as dword mx as dword e as dword q as dword sum as dword cipher as string cipher = base64decode(ciphertext) dim v(ceil(len(cipher)/4.0) - 1) as dword dim k(ceil(16/4.0) - 1) as dword for i = 0 to array count(v()) v(i) = asc(mid$(cipher, i*4+1)) + (asc(mid$(cipher, i*4+2))<<8) + (asc(mid$(cipher, i*4+3))<<16) + (asc(mid$(cipher, i*4+4))<<24) next i for i = 0 to array count(k()) k(i) = asc(mid$(password, i*4+1)) + (asc(mid$(password, i*4+2))<<8) + (asc(mid$(password, i*4+3))<<16) + (asc(mid$(password, i*4+4))<<24) next i n = array count(v()) + 1 z = v(n-1) y = v(0) delta = 0x9E3779B9 q = floor(6.0 + 52.0/n) sum = q*delta while sum <> 0 e = sum>>2 && 3 for p = n-1 to 0 step -1 if p > 0 z = v(p-1) else z = v(n-1) endif mx = (z>>5 ~~ y<<2) + (y>>3 ~~ z<<4) ~~ (sum~~y) + (k(p&&3 ~~ e) ~~ z) y = v(p) - mx v(p) = v(p) - mx next p dec sum, delta endwhile for i = 0 to array count(v()) plaintext = plaintext + chr$(v(i) && 0xFF) + chr$(v(i)>>8 && 0xFF) + chr$(v(i)>>16 && 0xFF) + chr$(v(i)>>24 && 0xFF) next i undim v() undim k() endfunction plaintext function base64encode(s as string) base64code$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" pad as string plain as string coded as string o1 as dword o2 as dword o3 as dword bits as dword h1 as dword h2 as dword h3 as dword h4 as dword plain = s pad = "" c = len(plain) mod 3 if c > 0 while c < 3 pad = pad + "=" plain = plain + "" inc c endwhile endif coded = "" for c = 1 to len(plain) step 3 o1 = asc(mid$(plain, c)) o2 = asc(mid$(plain, c+1)) o3 = asc(mid$(plain, c+2)) bits = o1<<16 || o2<<8 || o3 h1 = bits>>18 && 0x3f h2 = bits>>12 && 0x3f h3 = bits>>6 && 0x3f h4 = bits && 0x3f coded = coded + mid$(base64code$, h1 + 1) + mid$(base64code$, h2 + 1) + mid$(base64code$, h3 + 1) + mid$(base64code$, h4 + 1) next c coded = left$(coded, len(coded)-len(pad)) + pad endfunction coded function base64decode(s as string) base64code$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" plain as string coded as string o1 as dword o2 as dword o3 as dword bits as dword h1 as dword h2 as dword h3 as dword h4 as dword coded = s plain = "" for c = 1 to len(coded) step 4 h1 = find sub string$(base64code$, mid$(coded, c)) - 1 h2 = find sub string$(base64code$, mid$(coded, c+1)) - 1 h3 = find sub string$(base64code$, mid$(coded, c+2)) - 1 h4 = find sub string$(base64code$, mid$(coded, c+3)) - 1 bits = h1<<18 || h2<<12 || h3<<6 || h4 o1 = bits>>16 && 0xff o2 = bits>>8 && 0xff o3 = bits && 0xff temp$ = chr$(o1) + chr$(o2) + chr$(o3) if (h4 = 0x40) temp$ = chr$(o1) + chr$(o2) endif if (h3 = 0x40) temp$ = chr$(o1) endif plain = plain + temp$ next c endfunction plain |