📄 des4.java
字号:
package test.des2.tt;
// ----------------------------------------------------------------------------
// $Id: DES.java,v 1.4 2005/10/06 04:24:14 rsdio Exp $
//
// Copyright (C) 2002, 2003 Free Software Foundation, Inc.
//
// This file is part of GNU Crypto.
//
// GNU Crypto is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// GNU Crypto is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to the
//
// Free Software Foundation Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301
// USA
//
// Linking this library statically or dynamically with other modules is
// making a combined work based on this library. Thus, the terms and
// conditions of the GNU General Public License cover the whole
// combination.
//
// As a special exception, the copyright holders of this library give
// you permission to link this library with independent modules to
// produce an executable, regardless of the license terms of these
// independent modules, and to copy and distribute the resulting
// executable under terms of your choice, provided that you also meet,
// for each linked independent module, the terms and conditions of the
// license of that module. An independent module is a module which is
// not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the
// library, but you are not obligated to do so. If you do not wish to
// do so, delete this exception statement from your version.
//
// --------------------------------------------------------------------------
import java.security.InvalidKeyException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import sun.misc.BASE64Encoder;
/**
* <p>The Data Encryption Standard. DES is a 64-bit block cipher with a 56-bit
* key, developed by IBM in the 1970's for the standardization process begun by
* the National Bureau of Standards (now NIST).</p>
*
* <p>New applications should not use DES except for compatibility.</p>
*
* <p>This version is based upon the description and sample implementation in
* [1].</p>
*
* <p>References:</p>
* <ol>
* <li>Bruce Schneier, <i>Applied Cryptography: Protocols, Algorithms, and
* Source Code in C, Second Edition</i>. (1996 John Wiley and Sons) ISBN
* 0-471-11709-9. Pages 265--301, 623--632.</li>
* </ol>
*
* @version $Revision: 1.4 $
*/
public class DES4 extends BaseCipher {
// Constants and variables
// -------------------------------------------------------------------------
/** DES operates on 64 bit blocks. */
public static final int BLOCK_SIZE = 8;
/** DES uses 56 bits of a 64 bit parity-adjusted key. */
public static final int KEY_SIZE = 8;
// S-Boxes 1 through 8.
private static final int[] SP1 = new int[] {
0x01010400, 0x00000000, 0x00010000, 0x01010404,
0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400,
0x01000404, 0x01010004, 0x01000000, 0x00000004,
0x00000404, 0x01000400, 0x01000400, 0x00010400,
0x00010400, 0x01010000, 0x01010000, 0x01000404,
0x00010004, 0x01000004, 0x01000004, 0x00010004,
0x00000000, 0x00000404, 0x00010404, 0x01000000,
0x00010000, 0x01010404, 0x00000004, 0x01010000,
0x01010400, 0x01000000, 0x01000000, 0x00000400,
0x01010004, 0x00010000, 0x00010400, 0x01000004,
0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404,
0x01000004, 0x00000404, 0x00010404, 0x01010400,
0x00000404, 0x01000400, 0x01000400, 0x00000000,
0x00010004, 0x00010400, 0x00000000, 0x01010004
};
private static final int[] SP2 = new int[] {
0x80108020, 0x80008000, 0x00008000, 0x00108020,
0x00100000, 0x00000020, 0x80100020, 0x80008020,
0x80000020, 0x80108020, 0x80108000, 0x80000000,
0x80008000, 0x00100000, 0x00000020, 0x80100020,
0x00108000, 0x00100020, 0x80008020, 0x00000000,
0x80000000, 0x00008000, 0x00108020, 0x80100000,
0x00100020, 0x80000020, 0x00000000, 0x00108000,
0x00008020, 0x80108000, 0x80100000, 0x00008020,
0x00000000, 0x00108020, 0x80100020, 0x00100000,
0x80008020, 0x80100000, 0x80108000, 0x00008000,
0x80100000, 0x80008000, 0x00000020, 0x80108020,
0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020,
0x00100020, 0x80008020, 0x80000020, 0x00100020,
0x00108000, 0x00000000, 0x80008000, 0x00008020,
0x80000000, 0x80100020, 0x80108020, 0x00108000
};
private static final int[] SP3 = new int[] {
0x00000208, 0x08020200, 0x00000000, 0x08020008,
0x08000200, 0x00000000, 0x00020208, 0x08000200,
0x00020008, 0x08000008, 0x08000008, 0x00020000,
0x08020208, 0x00020008, 0x08020000, 0x00000208,
0x08000000, 0x00000008, 0x08020200, 0x00000200,
0x00020200, 0x08020000, 0x08020008, 0x00020208,
0x08000208, 0x00020200, 0x00020000, 0x08000208,
0x00000008, 0x08020208, 0x00000200, 0x08000000,
0x08020200, 0x08000000, 0x00020008, 0x00000208,
0x00020000, 0x08020200, 0x08000200, 0x00000000,
0x00000200, 0x00020008, 0x08020208, 0x08000200,
0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208,
0x00000008, 0x00020208, 0x00020200, 0x08000008,
0x08020000, 0x08000208, 0x00000208, 0x08020000,
0x00020208, 0x00000008, 0x08020008, 0x00020200
};
private static final int[] SP4 = new int[] {
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802080, 0x00800081, 0x00800001, 0x00002001,
0x00000000, 0x00802000, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00800080, 0x00800001,
0x00000001, 0x00002000, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002001, 0x00002080,
0x00800081, 0x00000001, 0x00002080, 0x00800080,
0x00002000, 0x00802080, 0x00802081, 0x00000081,
0x00800080, 0x00800001, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00000000, 0x00802000,
0x00002080, 0x00800080, 0x00800081, 0x00000001,
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000,
0x00800001, 0x00002001, 0x00802080, 0x00800081,
0x00002001, 0x00002080, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002000, 0x00802080
};
private static final int[] SP5 = new int[] {
0x00000100, 0x02080100, 0x02080000, 0x42000100,
0x00080000, 0x00000100, 0x40000000, 0x02080000,
0x40080100, 0x00080000, 0x02000100, 0x40080100,
0x42000100, 0x42080000, 0x00080100, 0x40000000,
0x02000000, 0x40080000, 0x40080000, 0x00000000,
0x40000100, 0x42080100, 0x42080100, 0x02000100,
0x42080000, 0x40000100, 0x00000000, 0x42000000,
0x02080100, 0x02000000, 0x42000000, 0x00080100,
0x00080000, 0x42000100, 0x00000100, 0x02000000,
0x40000000, 0x02080000, 0x42000100, 0x40080100,
0x02000100, 0x40000000, 0x42080000, 0x02080100,
0x40080100, 0x00000100, 0x02000000, 0x42080000,
0x42080100, 0x00080100, 0x42000000, 0x42080100,
0x02080000, 0x00000000, 0x40080000, 0x42000000,
0x00080100, 0x02000100, 0x40000100, 0x00080000,
0x00000000, 0x40080000, 0x02080100, 0x40000100
};
private static final int[] SP6 = new int[] {
0x20000010, 0x20400000, 0x00004000, 0x20404010,
0x20400000, 0x00000010, 0x20404010, 0x00400000,
0x20004000, 0x00404010, 0x00400000, 0x20000010,
0x00400010, 0x20004000, 0x20000000, 0x00004010,
0x00000000, 0x00400010, 0x20004010, 0x00004000,
0x00404000, 0x20004010, 0x00000010, 0x20400010,
0x20400010, 0x00000000, 0x00404010, 0x20404000,
0x00004010, 0x00404000, 0x20404000, 0x20000000,
0x20004000, 0x00000010, 0x20400010, 0x00404000,
0x20404010, 0x00400000, 0x00004010, 0x20000010,
0x00400000, 0x20004000, 0x20000000, 0x00004010,
0x20000010, 0x20404010, 0x00404000, 0x20400000,
0x00404010, 0x20404000, 0x00000000, 0x20400010,
0x00000010, 0x00004000, 0x20400000, 0x00404010,
0x00004000, 0x00400010, 0x20004010, 0x00000000,
0x20404000, 0x20000000, 0x00400010, 0x20004010
};
private static final int[] SP7 = new int[] {
0x00200000, 0x04200002, 0x04000802, 0x00000000,
0x00000800, 0x04000802, 0x00200802, 0x04200800,
0x04200802, 0x00200000, 0x00000000, 0x04000002,
0x00000002, 0x04000000, 0x04200002, 0x00000802,
0x04000800, 0x00200802, 0x00200002, 0x04000800,
0x04000002, 0x04200000, 0x04200800, 0x00200002,
0x04200000, 0x00000800, 0x00000802, 0x04200802,
0x00200800, 0x00000002, 0x04000000, 0x00200800,
0x04000000, 0x00200800, 0x00200000, 0x04000802,
0x04000802, 0x04200002, 0x04200002, 0x00000002,
0x00200002, 0x04000000, 0x04000800, 0x00200000,
0x04200800, 0x00000802, 0x00200802, 0x04200800,
0x00000802, 0x04000002, 0x04200802, 0x04200000,
0x00200800, 0x00000000, 0x00000002, 0x04200802,
0x00000000, 0x00200802, 0x04200000, 0x00000800,
0x04000002, 0x04000800, 0x00000800, 0x00200002
};
private static final int[] SP8 = new int[] {
0x10001040, 0x00001000, 0x00040000, 0x10041040,
0x10000000, 0x10001040, 0x00000040, 0x10000000,
0x00040040, 0x10040000, 0x10041040, 0x00041000,
0x10041000, 0x00041040, 0x00001000, 0x00000040,
0x10040000, 0x10000040, 0x10001000, 0x00001040,
0x00041000, 0x00040040, 0x10040040, 0x10041000,
0x00001040, 0x00000000, 0x00000000, 0x10040040,
0x10000040, 0x10001000, 0x00041040, 0x00040000,
0x00041040, 0x00040000, 0x10041000, 0x00001000,
0x00000040, 0x10040040, 0x00001000, 0x00041040,
0x10001000, 0x00000040, 0x10000040, 0x10040000,
0x10040040, 0x10000000, 0x00040000, 0x10001040,
0x00000000, 0x10041040, 0x00040040, 0x10000040,
0x10040000, 0x10001000, 0x10001040, 0x00000000,
0x10041040, 0x00041000, 0x00041000, 0x00001040,
0x00001040, 0x00040040, 0x10000000, 0x10041000
};
/**
* Constants that help in determining whether or not a byte array is parity
* adjusted.
*/
private static final byte[] PARITY = {
8,1,0,8,0,8,8,0,0,8,8,0,8,0,2,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,3,
0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
4,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,5,0,8,0,8,8,0,0,8,8,0,8,0,6,8
};
// Key schedule constants.
private static final byte[] ROTARS = {
1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
};
private static final byte[] PC1 = {
56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3
};
private static final byte[] PC2 = {
13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31
};
/**
* Weak keys (parity adjusted): If all the bits in each half are either 0
* or 1, then the key used for any cycle of the algorithm is the same as
* all other cycles.
*/
public static final byte[][] WEAK_KEYS = {
Util.toBytesFromString("0101010101010101"),
Util.toBytesFromString("01010101FEFEFEFE"),
Util.toBytesFromString("FEFEFEFE01010101"),
Util.toBytesFromString("FEFEFEFEFEFEFEFE")
};
/**
* Semi-weak keys (parity adjusted): Some pairs of keys encrypt plain text
* to identical cipher text. In other words, one key in the pair can decrypt
* messages that were encrypted with the other key. These keys are called
* semi-weak keys. This occurs because instead of 16 different sub-keys being
* generated, these semi-weak keys produce only two different sub-keys.
*/
public static final byte[][] SEMIWEAK_KEYS = {
Util.toBytesFromString("01FE01FE01FE01FE"), Util.toBytesFromString("FE01FE01FE01FE01"),
Util.toBytesFromString("1FE01FE00EF10EF1"), Util.toBytesFromString("E01FE01FF10EF10E"),
Util.toBytesFromString("01E001E001F101F1"), Util.toBytesFromString("E001E001F101F101"),
Util.toBytesFromString("1FFE1FFE0EFE0EFE"), Util.toBytesFromString("FE1FFE1FFE0EFE0E"),
Util.toBytesFromString("011F011F010E010E"), Util.toBytesFromString("1F011F010E010E01"),
Util.toBytesFromString("E0FEE0FEF1FEF1FE"), Util.toBytesFromString("FEE0FEE0FEF1FEF1")
};
/** Possible weak keys (parity adjusted) --produce 4 instead of 16 subkeys. */
public static final byte[][] POSSIBLE_WEAK_KEYS = {
Util.toBytesFromString("1F1F01010E0E0101"),
Util.toBytesFromString("011F1F01010E0E01"),
Util.toBytesFromString("1F01011F0E01010E"),
Util.toBytesFromString("01011F1F01010E0E"),
Util.toBytesFromString("E0E00101F1F10101"),
Util.toBytesFromString("FEFE0101FEFE0101"),
Util.toBytesFromString("FEE01F01FEF10E01"),
Util.toBytesFromString("E0FE1F01F1FE0E01"),
Util.toBytesFromString("FEE0011FFEF1010E"),
Util.toBytesFromString("E0FE011FF1FE010E"),
Util.toBytesFromString("E0E01F1FF1F10E0E"),
Util.toBytesFromString("FEFE1F1FFEFE0E0E"),
Util.toBytesFromString("1F1F01010E0E0101"),
Util.toBytesFromString("011F1F01010E0E01"),
Util.toBytesFromString("1F01011F0E01010E"),
Util.toBytesFromString("01011F1F01010E0E"),
Util.toBytesFromString("01E0E00101F1F101"),
Util.toBytesFromString("1FFEE0010EFEF001"),
Util.toBytesFromString("1FE0FE010EF1FE01"),
Util.toBytesFromString("01FEFE0101FEFE01"),
Util.toBytesFromString("1FE0E01F0EF1F10E"),
Util.toBytesFromString("01FEE01F01FEF10E"),
Util.toBytesFromString("01E0FE1F01F1FE0E"),
Util.toBytesFromString("1FFEFE1F0EFEFE0E"),
Util.toBytesFromString("E00101E0F10101F1"),
Util.toBytesFromString("FE1F01E0FE0E0EF1"),
Util.toBytesFromString("FE011FE0FE010EF1"),
Util.toBytesFromString("E01F1FE0F10E0EF1"),
Util.toBytesFromString("FE0101FEFE0101FE"),
Util.toBytesFromString("E01F01FEF10E01FE"),
Util.toBytesFromString("E0011FFEF1010EFE"),
Util.toBytesFromString("FE1F1FFEFE0E0EFE"),
Util.toBytesFromString("1FFE01E00EFE01F1"),
Util.toBytesFromString("01FE1FE001FE0EF1"),
Util.toBytesFromString("1FE001FE0EF101FE"),
Util.toBytesFromString("01E01FFE01F10EFE"),
Util.toBytesFromString("0101E0E00101F1F1"),
Util.toBytesFromString("1F1FE0E00E0EF1F1"),
Util.toBytesFromString("1F01FEE00E01FEF1"),
Util.toBytesFromString("011FFEE0010EFEF1"),
Util.toBytesFromString("1F01E0FE0E01F1FE"),
Util.toBytesFromString("011FE0FE010EF1FE"),
Util.toBytesFromString("0101FEFE0001FEFE"),
Util.toBytesFromString("1F1FFEFE0E0EFEFE"),
Util.toBytesFromString("FEFEE0E0FEFEF1F1"),
Util.toBytesFromString("E0FEFEE0F1FEFEF1"),
Util.toBytesFromString("FEE0E0FEFEF1F1FE"),
Util.toBytesFromString("E0E0FEFEF1F1FEFE")
};
// Constructor(s)
// -------------------------------------------------------------------------
/** Default 0-argument constructor. */
public DES4() {
super("DES", BLOCK_SIZE, KEY_SIZE);
}
// Class methods
// -------------------------------------------------------------------------
/**
* <p>Adjust the parity for a raw key array. This essentially means that each
* byte in the array will have an odd number of '1' bits (the last bit in
* each byte is unused.</p>
*
* @param kb The key array, to be parity-adjusted.
* @param offset The starting index into the key bytes.
*/
public static void adjustParity(byte[] kb, int offset) {
for (int i = offset; i < KEY_SIZE; i++) {
kb[i] ^= (PARITY[kb[i] & 0xff] == 8) ? 1 : 0;
}
}
/**
* <p>Test if a byte array, which must be at least 8 bytes long, is parity
* adjusted.</p>
*
* @param kb The key bytes.
* @param offset The starting index into the key bytes.
* @return <code>true</code> if the first 8 bytes of <i>kb</i> have been
* parity adjusted. <code>false</code> otherwise.
*/
public static boolean isParityAdjusted(byte[] kb, int offset) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -