XTEA加密的一种JAVA实现,64轮,CBC模式,0填充
/**
* An implementation of the XTEA block cipher algorithm. 64 rounds, mode CBC, zero padding.
* by zahuopuboss
*/
public class XTEA {
private static final int BLOCK_ALIGN = 8;
private static final int KEY_LENGTH = 16;
private static final int DELTA = 0x9E3779B9;
private static final int NUM_ROUNDS = 64;
private int[] keys = new int[NUM_ROUNDS];
private byte[] ivtmp = new byte[BLOCK_ALIGN];
private void setKey(byte[] b) {
int[] key = new int[4];
for (int i = 0; i < KEY_LENGTH;) {
key[i / 4] = (b[i++] << 24) + ((b[i++] & 255) << 16) + ((b[i++] & 255) << 8) + (b[i++] & 255);
}
for (int i = 0, sum = 0; i < NUM_ROUNDS;) {
keys[i++] = sum + key[sum & 3];
sum += DELTA;
keys[i++] = sum + key[(sum >>> 11) & 3];
}
}
private void setIV(byte[] b) {
System.arraycopy(b, 0, ivtmp, 0, BLOCK_ALIGN);
}
private byte[] padding(byte[] in, int len) {
int lennew = len;
int mod = len % BLOCK_ALIGN;
if (mod != 0) {
lennew = ((len + BLOCK_ALIGN) / BLOCK_ALIGN) * BLOCK_ALIGN;