TEA Encryption in JAVA -
i want encrypt 4 number, far 2 of them being encrypted. have tried placing encrypt method loop doesnt encrypt , decrypt more 2 number. able on this?
public class tea { private static int delta = 0x9e3779b9; /* key schedule constant */ private static int[] key = { 78945677, 87678687, 234234, 234234 }; public void encrypt(int[] v, int[] k) { int v0 = v[0], v1 = v[1], sum = 0, n = 32; int k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3]; /* cache key */ while (n-- > 0) { sum += delta; v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >>> 5) + k1); v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >>> 5) + k3); } v[0] = v0; v[1] = v1; system.out.println(v0 + "," + v1); } public void decrypt(int[] v, int[] k) { int v0 = v[0], v1 = v[1], sum = 0xc6ef3720, n = 32; /* set */ int k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3]; /* cache key */ while (n-- > 0) { v1 -= ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >>> 5) + k3); v0 -= ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >>> 5) + k1); sum -= delta; } v[0] = v0; v[1] = v1; system.out.println(v0 + "," + v1); } public static void main(string[] args) throws ioexception { tea tea = new tea(); int n = 0; int cc[] = new int[100]; scanner input = new scanner(system.in); (int = 0; < 4; i++) { system.out.println("enter 4 number encrypt: "); n = input.nextint(); cc[i] = n; } tea.encrypt(cc, key); tea.decrypt(cc, key); } }
both encrypt() , decrypt() work first 2 elements.
so have either shift arrays in calling method in cycle, or introduce cycle on consecutive pairs in encrypt/decrypt method like
for (int idx = 0; idx < v.length; idx *= 2) { int v0 = v[idx], v1 = v[idx + 1], sum = 0, n = 32; ... } update according wikipedia example, method expects 2 integers (and not length array). need pass numbers pairs, like
for (int idx = 0; idx < 4; idx =* 2) { int[] tmp = {cc[idx], cc[idx + 1}; tea.encrypt(tmp, key); cc[idx] = tmp[0]; cc[idx + 1] = tmp[1]; } as result, each pair of integers receive pair of integers encrypted.
Comments
Post a Comment