📄 des.java
字号:
/****************************************************************************** * * Copyright (c) 1999-2003 AppGate Network Security AB. All Rights Reserved. * * This file contains Original Code and/or Modifications of Original Code as * defined in and that are subject to the MindTerm Public Source License, * Version 2.0, (the 'License'). You may not use this file except in compliance * with the License. * * You should have received a copy of the MindTerm Public Source License * along with this software; see the file LICENSE. If not, write to * AppGate Network Security AB, Otterhallegatan 2, SE-41118 Goteborg, SWEDEN * *****************************************************************************/package com.mindbright.security.cipher;import com.mindbright.jca.security.InvalidKeyException;public final class DES extends BlockCipher { private final static int BLOCK_SIZE = 8; // bytes in a data-block int[] key_schedule; public DES() { key_schedule = new int[32]; } public int getBlockSize() { return BLOCK_SIZE; } public synchronized void initializeKey(byte[] key) throws InvalidKeyException { int i, c, d, t, s, shifts; c = getIntLSBO(key, 0); d = getIntLSBO(key, 4); t = ((d >>> 4) ^ c) & 0x0f0f0f0f; c ^= t; d ^= t << 4; t = (((c << (16 - (-2))) ^ c) & 0xcccc0000); c = c ^ t ^ (t >>> (16 - (-2))); t = (((d << (16 - (-2))) ^ d) & 0xcccc0000); d = d ^ t ^ (t >>> (16 - (-2))); t = ((d >>> 1) ^ c) & 0x55555555; c ^= t; d ^= t << 1; t = ((c >>> 8) ^ d) & 0x00ff00ff; d ^= t; c ^= t << 8; t = ((d >>> 1) ^ c) & 0x55555555; c ^= t; d ^= t << 1; d = ((d & 0xff) << 16) | (d & 0xff00) | ((d >>> 16) & 0xff) | ((c >>> 4) & 0xf000000); c &= 0x0fffffff; shifts = 0x7efc; for(i = 0; i < 16; i++) { if((shifts & 1) != 0) { c = ((c >>> 2) | (c << 26)); d = ((d >>> 2) | (d << 26)); } else { c = ((c >>> 1) | (c << 27)); d = ((d >>> 1) | (d << 27)); } shifts >>>= 1; c &= 0x0fffffff; d &= 0x0fffffff; s = des_skb[0][ (c) & 0x3f] | des_skb[1][((c >>> 6 ) & 0x03)|((c >>> 7 ) & 0x3c)] | des_skb[2][((c >>> 13) & 0x0f)|((c >>> 14) & 0x30)] | des_skb[3][((c >>> 20) & 0x01)|((c >>> 21) & 0x06) | ((c >>> 22) & 0x38)]; t = des_skb[4][ (d) & 0x3f] | des_skb[5][((d >>> 7 ) & 0x03) | ((d >>> 8 ) & 0x3c)] | des_skb[6][ (d >>> 15) & 0x3f ] | des_skb[7][((d >>> 21) & 0x0f) | ((d >>> 22) & 0x30)]; key_schedule[i * 2] = ((t << 16) | (s & 0xffff)); s = ((s >>> 16) | (t & 0xffff0000)); key_schedule[(i * 2) + 1] = (s << 4) | (s >>> 28); } } public void blockEncrypt(byte[] in, int inOffset, byte[] out, int outOffset) { int t, i; int[] lr = new int[2]; lr[0] = getIntLSBO(in, inOffset); lr[1] = getIntLSBO(in, inOffset + 4); initPerm(lr); t = (lr[1] << 1) | (lr[1] >>> 31); lr[1] = (lr[0] << 1) | (lr[0] >>> 31); lr[0] = t; for (i = 0; i < 32; i += 4) { desCipher1(lr, i); desCipher2(lr, i + 2); } lr[0] = (lr[0] >>> 1) | (lr[0] << 31); lr[1] = (lr[1] >>> 1) | (lr[1] << 31); finalPerm(lr); putIntLSBO(lr[0], out, outOffset); putIntLSBO(lr[1], out, outOffset + 4); } public void blockDecrypt(byte[] in, int inOffset, byte[] out, int outOffset) { int t, i; int[] lr = new int[2]; lr[0] = getIntLSBO(in, inOffset); lr[1] = getIntLSBO(in, inOffset + 4); initPerm(lr); t = (lr[1] << 1) | (lr[1] >>> 31); lr[1] = (lr[0] << 1) | (lr[0] >>> 31); lr[0] = t; for (i = 30; i > 0; i -= 4) { desCipher1(lr, i); desCipher2(lr, i - 2); } lr[0] = (lr[0] >>> 1) | (lr[0] << 31); lr[1] = (lr[1] >>> 1) | (lr[1] << 31); finalPerm(lr); putIntLSBO(lr[0], out, outOffset); putIntLSBO(lr[1], out, outOffset + 4); } final void desCipher1(int[] lr, int i) { int u = lr[1] ^ key_schedule[i ]; int t = lr[1] ^ key_schedule[i + 1]; t = ((t >>> 4) + (t << 28)); lr[0] ^= (des_SPtrans[1][(t ) & 0x3f] | des_SPtrans[3][(t >>> 8 ) & 0x3f] | des_SPtrans[5][(t >>> 16) & 0x3f] | des_SPtrans[7][(t >>> 24) & 0x3f] | des_SPtrans[0][(u ) & 0x3f] | des_SPtrans[2][(u >>> 8 ) & 0x3f] | des_SPtrans[4][(u >>> 16) & 0x3f] | des_SPtrans[6][(u >>> 24) & 0x3f]); } final void desCipher2(int[] lr, int i) { int u = lr[0] ^ key_schedule[i ]; int t = lr[0] ^ key_schedule[i + 1]; t = ((t >>> 4) + (t << 28)); lr[1] ^= (des_SPtrans[1][(t ) & 0x3f] | des_SPtrans[3][(t >>> 8 ) & 0x3f] | des_SPtrans[5][(t >>> 16) & 0x3f] | des_SPtrans[7][(t >>> 24) & 0x3f] | des_SPtrans[0][(u ) & 0x3f] | des_SPtrans[2][(u >>> 8 ) & 0x3f] | des_SPtrans[4][(u >>> 16) & 0x3f] | des_SPtrans[6][(u >>> 24) & 0x3f]); } final static void initPerm(int[] lr) { int t = ((lr[1] >>> 4) ^ lr[0]) & 0x0f0f0f0f; lr[0] ^= t; lr[1] ^= t << 4; t = ((lr[0] >>> 16) ^ lr[1]) & 0x0000ffff; lr[1] ^= t; lr[0] ^= t << 16; t = ((lr[1] >>> 2) ^ lr[0]) & 0x33333333; lr[0] ^= t; lr[1] ^= t << 2; t = ((lr[0] >>> 8) ^ lr[1]) & 0x00ff00ff; lr[1] ^= t; lr[0] ^= t << 8; t = ((lr[1] >>> 1) ^ lr[0]) & 0x55555555; lr[0] ^= t; lr[1] ^= t << 1; } final static void finalPerm(int[] lr) { int t = ((lr[1] >>> 1) ^ lr[0]) & 0x55555555; lr[0] ^= t; lr[1] ^= t << 1; t = ((lr[0] >>> 8) ^ lr[1]) & 0x00ff00ff; lr[1] ^= t; lr[0] ^= t << 8; t = ((lr[1] >>> 2) ^ lr[0]) & 0x33333333; lr[0] ^= t; lr[1] ^= t << 2; t = ((lr[0] >>> 16) ^ lr[1]) & 0x0000ffff; lr[1] ^= t; lr[0] ^= t << 16; t = ((lr[1] >>> 4) ^ lr[0]) & 0x0f0f0f0f; lr[0] ^= t; lr[1] ^= t << 4; } /* Table for key generation. This used to be in sk.h. * Copyright (C) 1993 Eric Young - see README for more details */ final static int des_skb[][] = { /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ { 0x00000000,0x00000010,0x20000000,0x20000010, 0x00010000,0x00010010,0x20010000,0x20010010, 0x00000800,0x00000810,0x20000800,0x20000810, 0x00010800,0x00010810,0x20010800,0x20010810, 0x00000020,0x00000030,0x20000020,0x20000030, 0x00010020,0x00010030,0x20010020,0x20010030, 0x00000820,0x00000830,0x20000820,0x20000830, 0x00010820,0x00010830,0x20010820,0x20010830, 0x00080000,0x00080010,0x20080000,0x20080010, 0x00090000,0x00090010,0x20090000,0x20090010, 0x00080800,0x00080810,0x20080800,0x20080810, 0x00090800,0x00090810,0x20090800,0x20090810, 0x00080020,0x00080030,0x20080020,0x20080030, 0x00090020,0x00090030,0x20090020,0x20090030, 0x00080820,0x00080830,0x20080820,0x20080830, 0x00090820,0x00090830,0x20090820,0x20090830 }, /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ { 0x00000000,0x02000000,0x00002000,0x02002000, 0x00200000,0x02200000,0x00202000,0x02202000, 0x00000004,0x02000004,0x00002004,0x02002004, 0x00200004,0x02200004,0x00202004,0x02202004, 0x00000400,0x02000400,0x00002400,0x02002400, 0x00200400,0x02200400,0x00202400,0x02202400, 0x00000404,0x02000404,0x00002404,0x02002404, 0x00200404,0x02200404,0x00202404,0x02202404, 0x10000000,0x12000000,0x10002000,0x12002000, 0x10200000,0x12200000,0x10202000,0x12202000, 0x10000004,0x12000004,0x10002004,0x12002004, 0x10200004,0x12200004,0x10202004,0x12202004, 0x10000400,0x12000400,0x10002400,0x12002400, 0x10200400,0x12200400,0x10202400,0x12202400, 0x10000404,0x12000404,0x10002404,0x12002404, 0x10200404,0x12200404,0x10202404,0x12202404 }, /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ { 0x00000000,0x00000001,0x00040000,0x00040001, 0x01000000,0x01000001,0x01040000,0x01040001, 0x00000002,0x00000003,0x00040002,0x00040003, 0x01000002,0x01000003,0x01040002,0x01040003, 0x00000200,0x00000201,0x00040200,0x00040201, 0x01000200,0x01000201,0x01040200,0x01040201, 0x00000202,0x00000203,0x00040202,0x00040203, 0x01000202,0x01000203,0x01040202,0x01040203, 0x08000000,0x08000001,0x08040000,0x08040001, 0x09000000,0x09000001,0x09040000,0x09040001, 0x08000002,0x08000003,0x08040002,0x08040003, 0x09000002,0x09000003,0x09040002,0x09040003, 0x08000200,0x08000201,0x08040200,0x08040201, 0x09000200,0x09000201,0x09040200,0x09040201, 0x08000202,0x08000203,0x08040202,0x08040203, 0x09000202,0x09000203,0x09040202,0x09040203 }, /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ { 0x00000000,0x00100000,0x00000100,0x00100100, 0x00000008,0x00100008,0x00000108,0x00100108,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -