it.scoppelletti.programmerpower.security.spi.DESedeKeyFactory.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.programmerpower.security.spi.DESedeKeyFactory.java

Source

/*
 * 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.programmerpower.security.spi;

import java.security.*;
import java.security.spec.*;
import java.util.*;
import javax.crypto.spec.*;
import org.apache.commons.codec.*;
import org.apache.commons.codec.binary.*;
import it.scoppelletti.programmerpower.*;
import it.scoppelletti.programmerpower.security.*;
import it.scoppelletti.programmerpower.types.*;

/**
 * Classe di factory di una chiave per l&rsquo;algoritmo
 * <ACRONYM TITLE="Triple Digital Encryption Standard">3DES</ACRONYM>.
 *  
 * <H4>1. Propriet&agrave;</H4>
 * 
 * <P><TABLE WIDTH="100%" BORDER="1" CELLPADDING="5">
 * <THEAD>
 * <TR>
 *     <TH>Propriet&agrave;</TH>
 *     <TH>Descrizione</TH>     
 * </TR>
 * </THEAD>
 * <TBODY>     
 * <TR>
 *      <TD>{@code key}</TD>
 *      <TD>Chiave di 24 byte in formato esadecimale.</TD>
 * </TR>   
 * </TBODY>
 * </TABLE></P>
 *   
 * @see   it.scoppelletti.programmerpower.security.CryptoUtils#getKey
 * @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 DESedeKeyFactory extends AbstractCryptoKeyFactory {

    /**
     * Codice dell&rsquo;algoritmo di crittografia 3DES. Il valore della
     * costante &egrave; <CODE>{@value}</CODE>. 
     */
    public static final String ALGORITHM = "DESede";

    /**
     * Nome della propriet&agrave; sulla quale deve essere impostato il valore
     * della chiave. Il valore della costante &egrave; <CODE>{@value}</CODE>.
     */
    public static final String PROP_KEY = "key";

    /**
     * Costruttore.
     */
    public DESedeKeyFactory() {
    }

    @Override
    protected String getAlgorithm(Properties props, String prefix) {
        return DESedeKeyFactory.ALGORITHM;
    }

    @Override
    protected KeyRep.Type getKeyType(Properties props, String prefix) {
        return KeyRep.Type.SECRET;
    }

    @Override
    protected KeySpec getKeySpec(String alg, KeyRep.Type keyType, Properties props, String prefix) {
        String name, value;
        byte[] key;
        KeySpec keySpec;

        name = Strings.concat(prefix, DESedeKeyFactory.PROP_KEY);
        value = props.getProperty(name);
        if (Strings.isNullOrEmpty(value)) {
            throw new ArgumentNullException(name);
        }

        try {
            key = Hex.decodeHex(value.toCharArray());
        } catch (DecoderException ex) {
            throw SecurityUtils.toSecurityException(ex);
        }

        try {
            keySpec = new DESedeKeySpec(key);
        } catch (InvalidKeyException ex) {
            throw SecurityUtils.toSecurityException(ex);
        }

        return keySpec;
    }
}