00001
package com.quadcap.crypto;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.util.Random;
00042
00043
import java.nio.ByteBuffer;
00044
import java.nio.IntBuffer;
00045
00046
import com.quadcap.util.text.Text;
00047
import com.quadcap.util.Util;
00048
00049
00050
00051
00052
00053
00054 public class Tea extends AbstractSymmetricKey implements
SymmetricKey {
00055 static final int delta = 0x9E3779B9;
00056 int a,
b,
c,
d;
00057 int[]
v =
new int[2];
00058
00059
00060
00061
00062 public void init(String s) {
00063 String[] vx =
Text.extractN(s,
"*:*:*:*:*");
00064
a = Integer.parseInt(vx[1]);
00065
b = Integer.parseInt(vx[2]);
00066
c = Integer.parseInt(vx[3]);
00067
d = Integer.parseInt(vx[4]);
00068 }
00069
00070 public void init(byte[] k) {
00071
a =
Util.integer(k, 0);
00072
b =
Util.integer(k, 4);
00073
c =
Util.integer(k, 8);
00074
d =
Util.integer(k, 12);
00075 }
00076
00077
00078
00079
00080 public void init(Random r) {
00081
a = r.nextInt();
00082
b = r.nextInt();
00083
c = r.nextInt();
00084
d = r.nextInt();
00085 }
00086
00087
00088
00089
00090 public String
toString() {
00091
return "TEA:" +
a +
":" +
b +
":" +
c +
":" +
d;
00092 }
00093
00094
00095
00096
00097 public void encrypt(ByteBuffer m, ByteBuffer c) {
00098
while (m.position() < m.limit()) {
00099
v[0] = m.getInt();
00100
v[1] = m.getInt();
00101 encrypt(
v);
00102 c.putInt(
v[0]);
00103 c.putInt(
v[1]);
00104 }
00105 }
00106
00107
00108
00109
00110 public void decrypt(ByteBuffer c, ByteBuffer m) {
00111
while ( c.position() < c.limit()) {
00112
v[0] = c.getInt();
00113
v[1] = c.getInt();
00114 decrypt(
v);
00115 m.putInt(
v[0]);
00116 m.putInt(
v[1]);
00117 }
00118 }
00119
00120 public int getBlockSize() {
return 8; }
00121
00122 final void encrypt(
int[] v) {
00123
int y = v[0];
00124
int z = v[1];
00125
int sum = 0;
00126
00127
for (
int n = 32; n-- > 0; ) {
00128 sum +=
delta;
00129 y += (z << 4) + a ^ z + sum ^ (z >>> 5) +
b;
00130 z += (y << 4) + c ^ y + sum ^ (y >>> 5) +
d;
00131 }
00132 v[0] = y;
00133 v[1] = z;
00134 }
00135
00136 final void decrypt(
int[] v) {
00137
int y = v[0];
00138
int z = v[1];
00139
int sum = 0xC6EF3720;
00140
for (
int n = 32; n-- > 0; ) {
00141 z -= (y << 4) + c ^ y + sum ^ (y >>> 5) +
d;
00142 y -= (z << 4) + a ^ z + sum ^ (z >>> 5) +
b;
00143 sum -=
delta;
00144 }
00145 v[0] = y;
00146 v[1] = z;
00147 }
00148 }