1 package com.knowgate.misc; 2 3 import java.math.*; 4 5 102 103 public class TEA 104 { 105 private int _key[]; private byte _keyBytes[]; private int _padding; 109 112 public static void main(String args[]) 113 { 114 116 byte key[] = new BigInteger("39e858f86df9b909a8c87cb8d9ad599", 16).toByteArray(); 117 TEA t = new TEA(key); 118 119 String src = "hello world!"; 120 System.out.println("input = " + src); 121 byte plainSource[] = src.getBytes(); 122 int enc[] = t.encode(plainSource, plainSource.length); 123 System.out.println(t.padding() + " bytes added as padding."); 124 byte dec[] = t.decode(enc); 125 System.out.println("output = " + new String (dec)); 126 } 127 128 135 public TEA(byte key[]) 136 { 137 int klen = key.length; 138 _key = new int[4]; 139 140 if (klen != 16) 142 throw new ArrayIndexOutOfBoundsException (this.getClass().getName() + ": Key is not 16 bytes"); 143 144 int j, i; 145 for (i = 0, j = 0; j < klen; j += 4, i++) 146 _key[i] = (key[j] << 24 ) | (((key[j+1])&0xff) << 16) | (((key[j+2])&0xff) << 8) | ((key[j+3])&0xff); 147 148 _keyBytes = key; } 150 151 154 public String toString() 155 { 156 String tea = this.getClass().getName(); 157 tea += ": Tiny Encryption Algorithm (TEA) key: " + dumpBytes(_keyBytes); 158 return tea; 159 } 160 161 178 public int [] encipher(int v[]) 179 { 180 int y=v[0]; 181 int z=v[1]; 182 int sum=0; 183 int delta=0x9E3779B9; 184 int a=_key[0]; 185 int b=_key[1]; 186 int c=_key[2]; 187 int d=_key[3]; 188 int n=32; 189 190 while(n-->0) 191 { 192 sum += delta; 193 y += (z << 4)+a ^ z+sum ^ (z >> 5)+b; 194 z += (y << 4)+c ^ y+sum ^ (y >> 5)+d; 195 } 196 197 v[0] = y; 198 v[1] = z; 199 200 return v; 201 } 202 203 225 public int [] decipher(int v[]) 226 { 227 int y=v[0]; 228 int z=v[1]; 229 int sum=0xC6EF3720; 230 int delta=0x9E3779B9; 231 int a=_key[0]; 232 int b=_key[1]; 233 int c=_key[2]; 234 int d=_key[3]; 235 int n=32; 236 237 239 while(n-->0) 240 { 241 z -= (y << 4)+c ^ y+sum ^ (y >> 5) + d; 242 y -= (z << 4)+a ^ z+sum ^ (z >> 5) + b; 243 sum -= delta; 244 } 245 246 v[0] = y; 247 v[1] = z; 248 249 return v; 250 } 251 252 259 public byte [] encipher(byte v[]) 260 { 261 byte y=v[0]; 262 byte z=v[1]; 263 int sum=0; 264 int delta=0x9E3779B9; 265 int a=_key[0]; 266 int b=_key[1]; 267 int c=_key[2]; 268 int d=_key[3]; 269 int n=32; 270 271 while(n-->0) 272 { 273 sum += delta; 274 y += (z << 4)+a ^ z+sum ^ (z >> 5)+b; 275 z += (y << 4)+c ^ y+sum ^ (y >> 5)+d; 276 } 277 278 v[0] = y; 279 v[1] = z; 280 281 return v; 282 } 283 284 291 public byte [] decipher(byte v[]) 292 { 293 byte y=v[0]; 294 byte z=v[1]; 295 int sum=0xC6EF3720; 296 int delta=0x9E3779B9; 297 int a=_key[0]; 298 int b=_key[1]; 299 int c=_key[2]; 300 int d=_key[3]; 301 int n=32; 302 303 305 while(n-->0) 306 { 307 z -= (y << 4)+c ^ y+sum ^ (y >> 5)+d; 308 y -= (z << 4)+a ^ z+sum ^ (z >> 5)+b; 309 sum -= delta; 310 } 311 312 v[0] = y; 313 v[1] = z; 314 315 return v; 316 } 317 318 331 public int [] encode(byte b[], int count) 332 { 333 int j ,i; 334 int bLen = count; 335 byte bp[] = b; 336 337 _padding = bLen % 8; 338 if (_padding != 0) { 340 _padding = 8 - (bLen % 8); 341 bp = new byte[bLen + _padding]; 342 System.arraycopy(b, 0, bp, 0, bLen); 343 bLen = bp.length; 344 } 345 346 int intCount = bLen / 4; 347 int r[] = new int[2]; 348 int out[] = new int[intCount]; 349 350 for (i = 0, j = 0; j < bLen; j += 8, i += 2) 351 { 352 r[0] = (bp[j] << 24 ) | (((bp[j+1])&0xff) << 16) | (((bp[j+2])&0xff) << 8) | ((bp[j+3])&0xff); 355 r[1] = (bp[j+4] << 24 ) | (((bp[j+5])&0xff) << 16) | (((bp[j+6])&0xff) << 8) | ((bp[j+7])&0xff); 356 encipher(r); 357 out[i] = r[0]; 358 out[i+1] = r[1]; 359 } 360 361 return out; 362 } 363 364 371 public int padding() 372 { 373 return _padding; 374 } 375 376 386 public byte [] decode(byte b[], int count) 387 { 388 int i, j; 389 390 int intCount = count / 4; 391 int ini[] = new int[intCount]; 392 for (i = 0, j = 0; i < intCount; i += 2, j += 8) 393 { 394 ini[i] = (b[j] << 24 ) | (((b[j+1])&0xff) << 16) | (((b[j+2])&0xff) << 8) | ((b[j+3])&0xff); 395 ini[i+1] = (b[j+4] << 24 ) | (((b[j+5])&0xff) << 16) | (((b[j+6])&0xff) << 8) | ((b[j+7])&0xff); 396 } 397 return decode(ini); 398 } 399 400 410 public byte [] decode(int b[]) 411 { 412 int intCount = b.length; 414 415 byte outb[] = new byte[intCount * 4]; 416 int tmp[] = new int[2]; 417 418 int i, j; 420 for (j = 0, i = 0; i < intCount; i += 2, j += 8) 421 { 422 tmp[0] = b[i]; 423 tmp[1] = b[i+1]; 424 decipher(tmp); 425 outb[j] = (byte)(tmp[0] >>> 24); 426 outb[j+1] = (byte)(tmp[0] >>> 16); 427 outb[j+2] = (byte)(tmp[0] >>> 8); 428 outb[j+3] = (byte)(tmp[0]); 429 outb[j+4] = (byte)(tmp[1] >>> 24); 430 outb[j+5] = (byte)(tmp[1] >>> 16); 431 outb[j+6] = (byte)(tmp[1] >>> 8); 432 outb[j+7] = (byte)(tmp[1]); 433 } 434 435 return outb; 436 } 437 438 439 private String dumpBytes(byte b[]) 442 { 443 StringBuffer r = new StringBuffer (); 444 final String hex = "0123456789ABCDEF"; 445 446 for (int i = 0; i < b.length; i++) 447 { 448 int c = ((b[i]) >>> 4) & 0xf; 449 r.append(hex.charAt(c)); 450 c = ((int)b[i] & 0xf); 451 r.append(hex.charAt(c)); 452 } 453 454 return r.toString(); 455 } 456 457 } 458 | Popular Tags |