java简单加密代码 java加密的几种方法(13)


简单的位运算 可能出于效率的考虑把它们实现成了宏 在java中 我们把它们
实现成了private方法 名字保持了原来C中的 */
private long F(long x long y long z) {
return (xy) | ((~x)z);
}
private long G(long x long y long z) {
return (xz) | (y(~z));
}
private long H(long x long y long z) {
return x ^ y ^ z;
}
private long I(long x long y long z) {
return y ^ (x | (~z));
}
/*
FF GG HH和II将调用F G H I进行近一步变换
FF GG HH and II transformations for rounds and
Rotation is separate from addition to prevent reputation
*/
private long FF(long a long b long c long d long x long s long ac) {
a += F(b c d) + x + ac;
a = ((int) as) | ((int) a( s));
a += b;
return a;
}
private long GG(long a long b long c long d long x long s long ac) {
a += G(b c d) + x + ac;
a = ((int) as) | ((int) a( s));
a += b;
return a;
}
private long HH(long a long b long c long d long x long s long ac) {
a += H(b c d) + x + ac;
a = ((int) as) | ((int) a( s));
a += b;
return a;
}
private long II(long a long b long c long d long x long s long ac) {
a += I(b c d) + x + ac;
a = ((int) as) | ((int) a( s));
a += b;
return a;
}
/*
md Update是MD 的主计算过程 inbuf是要变换的字节串 inputlen是长度 这个
函数由getMD ofStr调用 调用之前需要调用md init 因此把它设计成private的
*/
private void md Update(byte[] inbuf int inputLen) {
int i index partLen;
byte[] block = new byte[ ];
index = (int) (count[ ])x F;
// /* Update number of bits */
if ((count[ ] += (inputLen))(inputLen))
count[ ]++;
count[ ] += (inputLen);
partLen = index;
// Transform as many times as possible
if (inputLen = partLen) {
md Memcpy(buffer inbuf index partLen);
md Transform(buffer);
for (i = partLen; i +inputLen; i += ) {
md Memcpy(block inbuf i );
md Transform(block);
}
index = ;
} else
i = ;
///* Buffer remaining input */
md Memcpy(buffer inbuf index i inputLen i);
}
/*
md Final整理和填写输出结果
*/
private void md Final() {
byte[] bits = new byte[ ];
int index padLen;
///* Save number of bits */
Encode(bits count );
///* Pad out to mod
index = (int) (count[ ])x f;
padLen = (index) ? ( index) : ( index);
md Update(PADDING padLen);
///* Append length (before padding) */
md Update(bits );
///* Store state in digest */
Encode(digest state );
}
/* md Memcpy是一个内部使用的byte数组的块拷贝函数 从input的inpos开始把len长度的
字节拷贝到output的outpos位置开始
*/
private void md Memcpy(byte[] output byte[] input int outpos int inpos int len) {
int i;
for (i = ; ilen; i++)
output[outpos + i] = input[inpos + i];
}
/*
md Transform是MD 核心变换程序 有md Update调用 block是分块的原始字节
*/
private void md Transform(byte block[]) {
long a = state[ ] b = state[ ] c = state[ ] d = state[ ];
long[] x = new long[ ];
Decode(x block );
/* Round */
a = FF(a b c d x[ ] S xd aa L); /* */
d = FF(d a b c x[ ] S xe c b L); /* */
c = FF(c d a b x[ ] S x dbL); /* */
b = FF(b c d a x[ ] S xc bdceeeL); /* */
a = FF(a b c d x[ ] S xf c fafL); /* */
d = FF(d a b c x[ ] S x c aL); /* */
c = FF(c d a b x[ ] S xa L); /* */
b = FF(b c d a x[ ] S xfd L); /* */
a = FF(a b c d x[ ] S x d L); /* */
d = FF(d a b c x[ ] S x b f afL); /* */

推荐阅读