1 package com.quadcap.crypto; 2 3 40 41 import java.util.Random ; 42 43 import java.nio.ByteBuffer ; 44 import java.nio.IntBuffer ; 45 46 import com.quadcap.util.text.Text; 47 import com.quadcap.util.Util; 48 49 54 public class Tea extends AbstractSymmetricKey implements SymmetricKey { 55 static final int delta = 0x9E3779B9; 56 int a, b, c, d; 57 int[] v = new int[2]; 58 59 62 public void init(String s) { 63 String [] vx = Text.extractN(s, "*:*:*:*:*"); 64 a = Integer.parseInt(vx[1]); 65 b = Integer.parseInt(vx[2]); 66 c = Integer.parseInt(vx[3]); 67 d = Integer.parseInt(vx[4]); 68 } 69 70 public void init(byte[] k) { 71 a = Util.integer(k, 0); 72 b = Util.integer(k, 4); 73 c = Util.integer(k, 8); 74 d = Util.integer(k, 12); 75 } 76 77 80 public void init(Random r) { 81 a = r.nextInt(); 82 b = r.nextInt(); 83 c = r.nextInt(); 84 d = r.nextInt(); 85 } 86 87 90 public String toString() { 91 return "TEA:" + a + ":" + b + ":" + c + ":" + d; 92 } 93 94 97 public void encrypt(ByteBuffer m, ByteBuffer c) { 98 while (m.position() < m.limit()) { 99 v[0] = m.getInt(); 100 v[1] = m.getInt(); 101 encrypt(v); 102 c.putInt(v[0]); 103 c.putInt(v[1]); 104 } 105 } 106 107 110 public void decrypt(ByteBuffer c, ByteBuffer m) { 111 while ( c.position() < c.limit()) { 112 v[0] = c.getInt(); 113 v[1] = c.getInt(); 114 decrypt(v); 115 m.putInt(v[0]); 116 m.putInt(v[1]); 117 } 118 } 119 120 public int getBlockSize() { return 8; } 121 122 final void encrypt(int[] v) { 123 int y = v[0]; 124 int z = v[1]; 125 int sum = 0; 126 127 for (int n = 32; n-- > 0; ) { 128 sum += delta; 129 y += (z << 4) + a ^ z + sum ^ (z >>> 5) + b; 130 z += (y << 4) + c ^ y + sum ^ (y >>> 5) + d; 131 } 132 v[0] = y; 133 v[1] = z; 134 } 135 136 final void decrypt(int[] v) { 137 int y = v[0]; 138 int z = v[1]; 139 int sum = 0xC6EF3720; 140 for (int n = 32; n-- > 0; ) { 141 z -= (y << 4) + c ^ y + sum ^ (y >>> 5) + d; 142 y -= (z << 4) + a ^ z + sum ^ (z >>> 5) + b; 143 sum -= delta; 144 } 145 v[0] = y; 146 v[1] = z; 147 } 148 } 149
| Popular Tags
|