public static void RC5_32_ecb_encrypt(byte[] inBytes, byte[] outBytes, RC5_32_KEY ks, int encrypt, CryptContext cryptContext) { ulong l = 0; ulong[] d = new ulong[2]; int c2l_inBytes_startIdx = 0; int l2c_outBytes_startIdx = 0; CryptUtil.c2l(inBytes, ref l, ref c2l_inBytes_startIdx); d[0] = l; CryptUtil.c2l(inBytes, ref l, ref c2l_inBytes_startIdx); d[1] = l; if (encrypt > 0) { RC5_32_encrypt(ref d, ks); } else { RC5_32_decrypt(ref d, ks); } l = d[0]; CryptUtil.l2c(l, outBytes, ref l2c_outBytes_startIdx); l = d[1]; CryptUtil.l2c(l, outBytes, ref l2c_outBytes_startIdx); l = d[0] = d[1] = 0; }
public static void DES_ecb_encrypt_one(byte[] inBytesTotal, uint startPos, uint inLen, ref byte[] outBytesTotal, DES_key_schedule ks, int enc, CryptContext cryptContext) { int len_ = (((int)inLen + 7) / 8) * 8; outBytesTotal = new byte[len_]; byte[] inBytes = new byte[8]; byte[] outBytes = new byte[8]; int idx = 0; for (idx = 0; idx <= inLen - 8; idx += 8) { //if (idx % 16 == 0) // 如果是 16 的整数倍 //{ // 保证 8 个字节 CryptUtil.get8Byte(inBytesTotal, (int)startPos + idx, inBytes); DES_ecb_encrypt(inBytes, outBytes, ks, enc); Array.Copy(outBytes, 0, outBytesTotal, idx, 8); //} //else //{ // Array.Copy(inBytesTotal, idx, outBytesTotal, idx, 8); //} } }
public static void D_ENCRYPT(ref ulong LL, ulong R, int S, ulong[] s, ref ulong u, ref ulong t) { LOAD_DATA_tmp(R, S, s, ref u, ref t); t = CryptUtil.ROTATE(t, 4); LL ^= DES_SPtrans[0, (u >> (int)2L) & 0x3f] ^ DES_SPtrans[2, (u >> (int)10L) & 0x3f] ^ DES_SPtrans[4, (u >> (int)18L) & 0x3f] ^ DES_SPtrans[6, (u >> (int)26L) & 0x3f] ^ DES_SPtrans[1, (t >> (int)2L) & 0x3f] ^ DES_SPtrans[3, (t >> (int)10L) & 0x3f] ^ DES_SPtrans[5, (t >> (int)18L) & 0x3f] ^ DES_SPtrans[7, (t >> (int)26L) & 0x3f]; }
public static void RC5_32_ecb_encrypt_one(byte[] inBytesTotal, uint startPos, uint inLen, ref byte[] outBytesTotal, RC5_32_KEY ks, int encrypt, CryptContext cryptContext) { int len_ = (((int)inLen + 7) / 8) * 8; outBytesTotal = new byte[len_]; byte[] inBytes = new byte[8]; byte[] outBytes = new byte[8]; int idx = 0; for (idx = 0; idx <= inLen - 8; idx += 8) { // ±£Ö¤ 8 ¸ö×Ö½Ú CryptUtil.get8Byte(inBytesTotal, (int)startPos + idx, inBytes); RC5_32_ecb_encrypt(inBytes, outBytes, ks, encrypt, cryptContext); Array.Copy(outBytes, 0, outBytesTotal, idx, 8); } }
public static void DES_ecb_encrypt(byte[] input, byte[] output, DES_key_schedule ks, int enc) { ulong l = 0; ulong[] ll = new ulong[2]; byte[] in_input = input; byte[] out_output = output; int c2l_in_input_startIdx = 0; int l2c_out_output_startIdx = 0; CryptUtil.c2l(in_input, ref l, ref c2l_in_input_startIdx); ll[0] = l; CryptUtil.c2l(in_input, ref l, ref c2l_in_input_startIdx); ll[1] = l; DES_encrypt1(ll, ks, enc); l = ll[0]; CryptUtil.l2c(l, out_output, ref l2c_out_output_startIdx); l = ll[1]; CryptUtil.l2c(l, out_output, ref l2c_out_output_startIdx); l = ll[0] = ll[1] = 0; }
public static void DES_encrypt1(ulong[] data, DES_key_schedule ks, int enc) { ulong l = 0, r = 0, t = 0, u = 0; //int i = 0; ulong[] s = null; r = data[0]; l = data[1]; CryptUtil.IP(ref r, ref l); /* * Things have been modified so that the initial rotate is done outside * the loop. This required the DES_SPtrans values in sp.h to be rotated * 1 bit to the right. One perl script later and things have a 5% speed * up on a sparc2. Thanks to Richard Outerbridge * <*****@*****.**> for pointing this out. */ /* clear the top bits on machines with 8byte longs */ /* shift left by 2 */ r = CryptUtil.ROTATE(r, 29) & 0xffffffffL; l = CryptUtil.ROTATE(l, 29) & 0xffffffffL; //s = ks.ks[0].deslong; /* * I don't know if it is worth the effort of loop unrolling the inner * loop */ if (enc != 0) { s = ks.ks[0].deslong; D_ENCRYPT(ref l, r, 0, s, ref u, ref t); /* 1 */ s = ks.ks[1].deslong; D_ENCRYPT(ref r, l, 2, s, ref u, ref t); /* 2 */ s = ks.ks[2].deslong; D_ENCRYPT(ref l, r, 4, s, ref u, ref t); /* 3 */ s = ks.ks[3].deslong; D_ENCRYPT(ref r, l, 6, s, ref u, ref t); /* 4 */ s = ks.ks[4].deslong; D_ENCRYPT(ref l, r, 8, s, ref u, ref t); /* 5 */ s = ks.ks[5].deslong; D_ENCRYPT(ref r, l, 10, s, ref u, ref t); /* 6 */ s = ks.ks[6].deslong; D_ENCRYPT(ref l, r, 12, s, ref u, ref t); /* 7 */ s = ks.ks[7].deslong; D_ENCRYPT(ref r, l, 14, s, ref u, ref t); /* 8 */ s = ks.ks[8].deslong; D_ENCRYPT(ref l, r, 16, s, ref u, ref t); /* 9 */ s = ks.ks[9].deslong; D_ENCRYPT(ref r, l, 18, s, ref u, ref t); /* 10 */ s = ks.ks[10].deslong; D_ENCRYPT(ref l, r, 20, s, ref u, ref t); /* 11 */ s = ks.ks[11].deslong; D_ENCRYPT(ref r, l, 22, s, ref u, ref t); /* 12 */ s = ks.ks[12].deslong; D_ENCRYPT(ref l, r, 24, s, ref u, ref t); /* 13 */ s = ks.ks[13].deslong; D_ENCRYPT(ref r, l, 26, s, ref u, ref t); /* 14 */ s = ks.ks[14].deslong; D_ENCRYPT(ref l, r, 28, s, ref u, ref t); /* 15 */ s = ks.ks[15].deslong; D_ENCRYPT(ref r, l, 30, s, ref u, ref t); /* 16 */ } else { s = ks.ks[15].deslong; D_ENCRYPT(ref l, r, 30, s, ref u, ref t); /* 16 */ s = ks.ks[14].deslong; D_ENCRYPT(ref r, l, 28, s, ref u, ref t); /* 15 */ s = ks.ks[13].deslong; D_ENCRYPT(ref l, r, 26, s, ref u, ref t); /* 14 */ s = ks.ks[12].deslong; D_ENCRYPT(ref r, l, 24, s, ref u, ref t); /* 13 */ s = ks.ks[11].deslong; D_ENCRYPT(ref l, r, 22, s, ref u, ref t); /* 12 */ s = ks.ks[10].deslong; D_ENCRYPT(ref r, l, 20, s, ref u, ref t); /* 11 */ s = ks.ks[9].deslong; D_ENCRYPT(ref l, r, 18, s, ref u, ref t); /* 10 */ s = ks.ks[8].deslong; D_ENCRYPT(ref r, l, 16, s, ref u, ref t); /* 9 */ s = ks.ks[7].deslong; D_ENCRYPT(ref l, r, 14, s, ref u, ref t); /* 8 */ s = ks.ks[6].deslong; D_ENCRYPT(ref r, l, 12, s, ref u, ref t); /* 7 */ s = ks.ks[5].deslong; D_ENCRYPT(ref l, r, 10, s, ref u, ref t); /* 6 */ s = ks.ks[4].deslong; D_ENCRYPT(ref r, l, 8, s, ref u, ref t); /* 5 */ s = ks.ks[3].deslong; D_ENCRYPT(ref l, r, 6, s, ref u, ref t); /* 4 */ s = ks.ks[2].deslong; D_ENCRYPT(ref r, l, 4, s, ref u, ref t); /* 3 */ s = ks.ks[1].deslong; D_ENCRYPT(ref l, r, 2, s, ref u, ref t); /* 2 */ s = ks.ks[0].deslong; D_ENCRYPT(ref r, l, 0, s, ref u, ref t); /* 1 */ } /* rotate and clear the top bits on machines with 8byte longs */ l = CryptUtil.ROTATE(l, 3) & 0xffffffffL; r = CryptUtil.ROTATE(r, 3) & 0xffffffffL; CryptUtil.FP(ref r, ref l); data[0] = l; data[1] = r; l = r = t = u = 0; }
public static void DES_set_key_unchecked(byte[] key, DES_key_schedule schedule) { ulong c = 0, d = 0, t = 0, s = 0, t2 = 0; byte[] in_Key; ulong[] k; int i; in_Key = key; int c2l_in_Key_startIdx = 0; CryptUtil.c2l(in_Key, ref c, ref c2l_in_Key_startIdx); CryptUtil.c2l(in_Key, ref d, ref c2l_in_Key_startIdx); /* * do PC1 in 47 simple operations :-) Thanks to John Fletcher * ([email protected]) for the inspiration. :-) */ CryptUtil.PERM_OP(ref d, ref c, ref t, 4, 0x0f0f0f0fL); CryptUtil.HPERM_OP(ref c, t, -2, 0xcccc0000L); CryptUtil.HPERM_OP(ref d, t, -2, 0xcccc0000L); CryptUtil.PERM_OP(ref d, ref c, ref t, 1, 0x55555555L); CryptUtil.PERM_OP(ref c, ref d, ref t, 8, 0x00ff00ffL); CryptUtil.PERM_OP(ref d, ref c, ref t, 1, 0x55555555L); d = (((d & 0x000000ffL) << (int)16L) | (d & 0x0000ff00L) | ((d & 0x00ff0000L) >> (int)16L) | ((c & 0xf0000000L) >> (int)4L)); c &= 0x0fffffffL; for (i = 0; i < ITERATIONS; i++) { k = schedule.ks[i].deslong; if (shifts2[i] != 0) { c = ((c >> (int)2L) | (c << (int)26L)); d = ((d >> (int)2L) | (d << (int)26L)); } else { c = ((c >> (int)1L) | (c << (int)27L)); d = ((d >> (int)1L) | (d << (int)27L)); } c &= 0x0fffffffL; d &= 0x0fffffffL; /* * could be a few less shifts but I am to lazy at this point in time * to investigate */ s = des_skb[0, (c) & 0x3f] | des_skb[1, ((c >> (int)6L) & 0x03) | ((c >> (int)7L) & 0x3c)] | des_skb[2, ((c >> (int)13L) & 0x0f) | ((c >> (int)14L) & 0x30)] | des_skb[3, ((c >> (int)20L) & 0x01) | ((c >> (int)21L) & 0x06) | ((c >> (int)22L) & 0x38)]; t = des_skb[4, (d) & 0x3f] | des_skb[5, ((d >> (int)7L) & 0x03) | ((d >> (int)8L) & 0x3c)] | des_skb[6, (d >> (int)15L) & 0x3f] | des_skb[7, ((d >> (int)21L) & 0x0f) | ((d >> (int)22L) & 0x30)]; /* table contained 0213 4657 */ t2 = ((t << (int)16L) | (s & 0x0000ffffL)) & 0xffffffffL; k[0] = CryptUtil.ROTATE(t2, 30) & 0xffffffffL; t2 = ((s >> (int)16L) | (t & 0xffff0000L)); k[1] = CryptUtil.ROTATE(t2, 26) & 0xffffffffL; } }
public static void RC5_32_set_key(RC5_32_KEY key, int len, byte[] data, int rounds) { int c2l_data_startIdx = 0; ulong[] L = new ulong[64]; ulong l = 0, ll = 0, A = 0, B = 0, k = 0; ulong[] S = null; int i = 0, j = 0, m = 0, c = 0, t = 0, ii = 0, jj = 0; if ((rounds != RC5_16_ROUNDS) && (rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS)) { rounds = RC5_16_ROUNDS; } key.rounds = rounds; S = key.data; j = 0; for (i = 0; i <= (len - 8); i += 8) { CryptUtil.c2l(data, ref l, ref c2l_data_startIdx); L[j++] = l; CryptUtil.c2l(data, ref l, ref c2l_data_startIdx); L[j++] = l; } ii = len - i; if (ii != 0) { k = (ulong)(len & 0x07); CryptUtil.c2ln(data, ref l, ref ll, (long)k, ref c2l_data_startIdx); L[j + 0] = l; L[j + 1] = ll; } c = (len + 3) / 4; t = (rounds + 1) * 2; S[0] = RC5_32_P; for (i = 1; i < t; i++) { S[i] = (S[i - 1] + RC5_32_Q) & RC5_32_MASK; } j = (t > c) ? t : c; j *= 3; ii = jj = 0; A = B = 0; for (i = 0; i < j; i++) { k = (S[ii] + A + B) & RC5_32_MASK; A = S[ii] = ROTATE_l32(k, 3); m = (int)(A + B); k = (L[jj] + A + B) & RC5_32_MASK; B = L[jj] = ROTATE_l32(k, (ulong)m); if (++ii >= t) { ii = 0; } if (++jj >= c) { jj = 0; } } }
public static void RC5_32_cbc_encrypt(byte[] inBytes, byte[] outBytes, long length, RC5_32_KEY ks, byte[] iv, int encrypt) { ulong tin0 = 0, tin1 = 0; ulong tout0 = 0, tout1 = 0, xor0 = 0, xor1 = 0; long l = length; ulong[] tin = new ulong[2]; int c2l_iv_startIdx = 0; int c2l_inBytes_startIdx = 0; int l2c_outBytes_startIdx = 0; int l2c_iv_startIdx = 0; if (encrypt > 0) { CryptUtil.c2l(iv, ref tout0, ref c2l_iv_startIdx); CryptUtil.c2l(iv, ref tout1, ref c2l_iv_startIdx); c2l_iv_startIdx -= 8; for (l -= 8; l >= 0; l -= 8) { CryptUtil.c2l(inBytes, ref tin0, ref c2l_inBytes_startIdx); CryptUtil.c2l(inBytes, ref tin1, ref c2l_inBytes_startIdx); tin0 ^= tout0; tin1 ^= tout1; tin[0] = tin0; tin[1] = tin1; RC5_32_encrypt(ref tin, ks); tout0 = tin[0]; CryptUtil.l2c(tout0, outBytes, ref l2c_outBytes_startIdx); tout1 = tin[1]; CryptUtil.l2c(tout1, outBytes, ref l2c_outBytes_startIdx); } if (l != -8) { CryptUtil.c2ln(inBytes, ref tin0, ref tin1, l + 8, ref c2l_inBytes_startIdx); tin0 ^= tout0; tin1 ^= tout1; tin[0] = tin0; tin[1] = tin1; RC5_32_encrypt(ref tin, ks); tout0 = tin[0]; CryptUtil.l2c(tout0, outBytes, ref l2c_outBytes_startIdx); tout1 = tin[1]; CryptUtil.l2c(tout1, outBytes, ref l2c_outBytes_startIdx); } CryptUtil.l2c(tout0, iv, ref l2c_iv_startIdx); CryptUtil.l2c(tout1, iv, ref l2c_iv_startIdx); } else { CryptUtil.c2l(iv, ref xor0, ref c2l_iv_startIdx); CryptUtil.c2l(iv, ref xor1, ref c2l_iv_startIdx); c2l_iv_startIdx -= 8; for (l -= 8; l >= 0; l -= 8) { CryptUtil.c2l(inBytes, ref tin0, ref c2l_inBytes_startIdx); tin[0] = tin0; CryptUtil.c2l(inBytes, ref tin1, ref c2l_inBytes_startIdx); tin[1] = tin1; RC5_32_decrypt(ref tin, ks); tout0 = tin[0] ^ xor0; tout1 = tin[1] ^ xor1; CryptUtil.l2c(tout0, outBytes, ref l2c_outBytes_startIdx); CryptUtil.l2c(tout1, outBytes, ref l2c_outBytes_startIdx); xor0 = tin0; xor1 = tin1; } if (l != -8) { CryptUtil.c2l(inBytes, ref tin0, ref c2l_inBytes_startIdx); tin[0] = tin0; CryptUtil.c2l(inBytes, ref tin1, ref c2l_inBytes_startIdx); tin[1] = tin1; RC5_32_decrypt(ref tin, ks); tout0 = tin[0] ^ xor0; tout1 = tin[1] ^ xor1; CryptUtil.l2cn(tout0, tout1, outBytes, l + 8, ref l2c_outBytes_startIdx); xor0 = tin0; xor1 = tin1; } CryptUtil.l2c(xor0, iv, ref l2c_iv_startIdx); CryptUtil.l2c(xor1, iv, ref l2c_iv_startIdx); } tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; tin[0] = tin[1] = 0; }