Path: blob/master/src/java.base/share/classes/javax/crypto/spec/DESedeKeySpec.java
41159 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto.spec;2627import java.security.InvalidKeyException;2829/**30* This class specifies a DES-EDE ("triple-DES") key.31*32* @author Jan Luehe33*34* @since 1.435*/36public class DESedeKeySpec implements java.security.spec.KeySpec {3738/**39* The constant which defines the length of a DESede key in bytes.40*/41public static final int DES_EDE_KEY_LEN = 24;4243private byte[] key;4445/**46* Creates a DESedeKeySpec object using the first 24 bytes in47* <code>key</code> as the key material for the DES-EDE key.48*49* <p> The bytes that constitute the DES-EDE key are those between50* <code>key[0]</code> and <code>key[23]</code> inclusive51*52* @param key the buffer with the DES-EDE key material. The first53* 24 bytes of the buffer are copied to protect against subsequent54* modification.55*56* @exception NullPointerException if <code>key</code> is null.57* @exception InvalidKeyException if the given key material is shorter58* than 24 bytes.59*/60public DESedeKeySpec(byte[] key) throws InvalidKeyException {61this(key, 0);62}6364/**65* Creates a DESedeKeySpec object using the first 24 bytes in66* <code>key</code>, beginning at <code>offset</code> inclusive,67* as the key material for the DES-EDE key.68*69* <p> The bytes that constitute the DES-EDE key are those between70* <code>key[offset]</code> and <code>key[offset+23]</code> inclusive.71*72* @param key the buffer with the DES-EDE key material. The first73* 24 bytes of the buffer beginning at <code>offset</code> inclusive74* are copied to protect against subsequent modification.75* @param offset the offset in <code>key</code>, where the DES-EDE key76* material starts.77*78* @exception NullPointerException if <code>key</code> is null.79* @exception InvalidKeyException if the given key material, starting at80* <code>offset</code> inclusive, is shorter than 24 bytes81*/82public DESedeKeySpec(byte[] key, int offset) throws InvalidKeyException {83if (key.length - offset < 24) {84throw new InvalidKeyException("Wrong key size");85}86this.key = new byte[24];87System.arraycopy(key, offset, this.key, 0, 24);88}8990/**91* Returns the DES-EDE key.92*93* @return the DES-EDE key. Returns a new array94* each time this method is called.95*/96public byte[] getKey() {97return this.key.clone();98}99100/**101* Checks if the given DES-EDE key, starting at <code>offset</code>102* inclusive, is parity-adjusted.103*104* @param key a byte array which holds the key value105* @param offset the offset into the byte array106* @return true if the given DES-EDE key is parity-adjusted, false107* otherwise108*109* @exception NullPointerException if <code>key</code> is null.110* @exception InvalidKeyException if the given key material, starting at111* <code>offset</code> inclusive, is shorter than 24 bytes112*/113public static boolean isParityAdjusted(byte[] key, int offset)114throws InvalidKeyException {115if (key.length - offset < 24) {116throw new InvalidKeyException("Wrong key size");117}118if (DESKeySpec.isParityAdjusted(key, offset) == false119|| DESKeySpec.isParityAdjusted(key, offset + 8) == false120|| DESKeySpec.isParityAdjusted(key, offset + 16) == false) {121return false;122}123return true;124}125}126127128