Java tutorial
/* * Copyright (C) 2010 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.scoppelletti.security.keygen; import it.scoppelletti.programmerpower.security.*; import java.security.*; import java.security.spec.*; import java.util.*; import javax.crypto.*; import javax.crypto.spec.*; import org.apache.commons.codec.binary.*; import it.scoppelletti.programmerpower.security.spi.*; /** * Rappresentazione di una chiave per l’algoritmo * <ACRONYM TITLE="Triple Digital Encryption Standard">3DES</ACRONYM> con una * collezione di parametri che consente di ricostruire la stessa chiave. * * @see it.scoppelletti.programmerpower.security.spi.DESedeKeyFactory * @see <A HREF="http://csrc.nist.gov/publications/PubsFIPSArch.html" * TARGET="_blank">FIPS 46-3: Data Encryption Standard (DES)</A> * @since 1.0.0 */ public final class DESedeKeyToPropertySetProvider implements KeyToPropertySetProvider { /** * Costruttore. */ public DESedeKeyToPropertySetProvider() { } public Properties toProperties(Key key) { byte[] data; SecretKey desKey; SecretKeyFactory keyFactory; DESedeKeySpec param; Properties props; if (!(key instanceof SecretKey)) { return null; } try { keyFactory = SecretKeyFactory.getInstance(DESedeKeyFactory.ALGORITHM); } catch (NoSuchAlgorithmException ex) { return null; } try { desKey = keyFactory.translateKey((SecretKey) key); } catch (InvalidKeyException ex) { return null; } try { param = (DESedeKeySpec) keyFactory.getKeySpec(desKey, DESedeKeySpec.class); } catch (InvalidKeySpecException ex) { return null; } props = new Properties(); props.setProperty(CryptoUtils.PROP_KEYFACTORY, DESedeKeyFactory.class.getName()); data = param.getKey(); props.setProperty(DESedeKeyFactory.PROP_KEY, Hex.encodeHexString(data)); Arrays.fill(data, Byte.MIN_VALUE); return props; } }