📄 rijndael.java
字号:
/* Rijndael.java -- Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.This file is a part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.crypto.cipher;import gnu.java.security.Registry;import gnu.java.security.util.Util;//import java.io.PrintWriter;import java.security.InvalidKeyException;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;/** * <p>Rijndael --pronounced Reindaal-- is the AES. It is a variable block-size * (128-, 192- and 256-bit), variable key-size (128-, 192- and 256-bit) * symmetric key block cipher.</p> * * <p>References:</p> * * <ol> * <li><a href="http://www.esat.kuleuven.ac.be/~rijmen/rijndael/">The * Rijndael Block Cipher - AES Proposal</a>.<br> * <a href="mailto:vincent.rijmen@esat.kuleuven.ac.be">Vincent Rijmen</a> and * <a href="mailto:daemen.j@protonworld.com">Joan Daemen</a>.</li> * </ol> */public final class Rijndael extends BaseCipher{ // Debugging methods and variables // ------------------------------------------------------------------------- // private static final String NAME = "rijndael"; private static final boolean DEBUG = false; private static final int debuglevel = 9; // private static final PrintWriter err = new PrintWriter(System.out, true); // private static void debug(String s) { // err.println(">>> "+NAME+": "+s); // } // Constants and variables // ------------------------------------------------------------------------- private static final int DEFAULT_BLOCK_SIZE = 16; // in bytes private static final int DEFAULT_KEY_SIZE = 16; // in bytes private static final String SS = "\u637C\u777B\uF26B\u6FC5\u3001\u672B\uFED7\uAB76" + "\uCA82\uC97D\uFA59\u47F0\uADD4\uA2AF\u9CA4\u72C0" + "\uB7FD\u9326\u363F\uF7CC\u34A5\uE5F1\u71D8\u3115" + "\u04C7\u23C3\u1896\u059A\u0712\u80E2\uEB27\uB275" + "\u0983\u2C1A\u1B6E\u5AA0\u523B\uD6B3\u29E3\u2F84" + "\u53D1\u00ED\u20FC\uB15B\u6ACB\uBE39\u4A4C\u58CF" + "\uD0EF\uAAFB\u434D\u3385\u45F9\u027F\u503C\u9FA8" + "\u51A3\u408F\u929D\u38F5\uBCB6\uDA21\u10FF\uF3D2" + "\uCD0C\u13EC\u5F97\u4417\uC4A7\u7E3D\u645D\u1973" + "\u6081\u4FDC\u222A\u9088\u46EE\uB814\uDE5E\u0BDB" + "\uE032\u3A0A\u4906\u245C\uC2D3\uAC62\u9195\uE479" + "\uE7C8\u376D\u8DD5\u4EA9\u6C56\uF4EA\u657A\uAE08" + "\uBA78\u252E\u1CA6\uB4C6\uE8DD\u741F\u4BBD\u8B8A" + "\u703E\uB566\u4803\uF60E\u6135\u57B9\u86C1\u1D9E" + "\uE1F8\u9811\u69D9\u8E94\u9B1E\u87E9\uCE55\u28DF" + "\u8CA1\u890D\uBFE6\u4268\u4199\u2D0F\uB054\uBB16"; private static final byte[] S = new byte[256]; private static final byte[] Si = new byte[256]; private static final int[] T1 = new int[256]; private static final int[] T2 = new int[256]; private static final int[] T3 = new int[256]; private static final int[] T4 = new int[256]; private static final int[] T5 = new int[256]; private static final int[] T6 = new int[256]; private static final int[] T7 = new int[256]; private static final int[] T8 = new int[256]; private static final int[] U1 = new int[256]; private static final int[] U2 = new int[256]; private static final int[] U3 = new int[256]; private static final int[] U4 = new int[256]; private static final byte[] rcon = new byte[30]; private static final int[][][] shifts = new int[][][] { { { 0, 0 }, { 1, 3 }, { 2, 2 }, { 3, 1 } }, { { 0, 0 }, { 1, 5 }, { 2, 4 }, { 3, 3 } }, { { 0, 0 }, { 1, 7 }, { 3, 5 }, { 4, 4 } } }; /** * KAT vector (from ecb_vk): * I=96 * KEY=0000000000000000000000010000000000000000000000000000000000000000 * CT=E44429474D6FC3084EB2A6B8B46AF754 */ private static final byte[] KAT_KEY = Util.toBytesFromString("0000000000000000000000010000000000000000000000000000000000000000"); private static final byte[] KAT_CT = Util.toBytesFromString("E44429474D6FC3084EB2A6B8B46AF754"); /** caches the result of the correctness test, once executed. */ private static Boolean valid; // Static code - to intialise lookup tables -------------------------------- static { long time = System.currentTimeMillis(); int ROOT = 0x11B; int i, j = 0; // S-box, inverse S-box, T-boxes, U-boxes int s, s2, s3, i2, i4, i8, i9, ib, id, ie, t; char c; for (i = 0; i < 256; i++) { c = SS.charAt(i >>> 1); S[i] = (byte) (((i & 1) == 0) ? c >>> 8 : c & 0xFF); s = S[i] & 0xFF; Si[s] = (byte) i; s2 = s << 1; if (s2 >= 0x100) { s2 ^= ROOT; } s3 = s2 ^ s; i2 = i << 1; if (i2 >= 0x100) { i2 ^= ROOT; } i4 = i2 << 1; if (i4 >= 0x100) { i4 ^= ROOT; } i8 = i4 << 1; if (i8 >= 0x100) { i8 ^= ROOT; } i9 = i8 ^ i; ib = i9 ^ i2; id = i9 ^ i4; ie = i8 ^ i4 ^ i2; T1[i] = t = (s2 << 24) | (s << 16) | (s << 8) | s3; T2[i] = (t >>> 8) | (t << 24); T3[i] = (t >>> 16) | (t << 16); T4[i] = (t >>> 24) | (t << 8); T5[s] = U1[i] = t = (ie << 24) | (i9 << 16) | (id << 8) | ib; T6[s] = U2[i] = (t >>> 8) | (t << 24); T7[s] = U3[i] = (t >>> 16) | (t << 16); T8[s] = U4[i] = (t >>> 24) | (t << 8); } // // round constants // int r = 1; rcon[0] = 1; for (i = 1; i < 30; i++) { r <<= 1; if (r >= 0x100) { r ^= ROOT; } rcon[i] = (byte) r; } time = System.currentTimeMillis() - time; if (DEBUG && debuglevel > 8) { System.out.println("=========="); System.out.println(); System.out.println("Static Data"); System.out.println(); System.out.println("S[]:"); for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { System.out.print("0x" + Util.toString(S[i * 16 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("Si[]:"); for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { System.out.print("0x" + Util.toString(Si[i * 16 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T1[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T1[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T2[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T2[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T3[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T3[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T4[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T4[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T5[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T5[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T6[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T6[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T7[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T7[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T8[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T8[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("U1[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(U1[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("U2[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(U2[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("U3[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(U3[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("U4[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.println("0x" + Util.toString(U4[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("rcon[]:"); for (i = 0; i < 5; i++) { for (j = 0; j < 6; j++) { System.out.print("0x" + Util.toString(rcon[i * 6 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("Total initialization time: " + time + " ms."); System.out.println(); } } // Constructor(s) // ------------------------------------------------------------------------- /** Trivial 0-arguments constructor. */ public Rijndael() { super(Registry.RIJNDAEL_CIPHER, DEFAULT_BLOCK_SIZE, DEFAULT_KEY_SIZE); } // Class methods // ------------------------------------------------------------------------- /** * <p>Returns the number of rounds for a given Rijndael's key and block * sizes.</p> * * @param ks the size of the user key material in bytes. * @param bs the desired block size in bytes. * @return the number of rounds for a given Rijndael's key and block sizes. */ public static int getRounds(int ks, int bs) { switch (ks) { case 16: return bs == 16 ? 10 : (bs == 24 ? 12 : 14); case 24: return bs != 32 ? 12 : 14; default: // 32 bytes = 256 bits return 14; } } private static void rijndaelEncrypt(byte[] in, int inOffset, byte[] out, int outOffset, Object sessionKey, int bs) { Object[] sKey = (Object[]) sessionKey; // extract encryption round keys int[][] Ke = (int[][]) sKey[0]; int BC = bs / 4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -