⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jcesecuritymodule.java

📁 this is the frame work for iso8583
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2009 Alejandro P. Revilla * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program.  If not, see <http://www.gnu.org/licenses/>. */package  org.jpos.security.jceadapter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.security.InvalidKeyException;import java.security.Key;import java.security.Provider;import java.security.Security;import java.util.Hashtable;import java.util.Properties;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import org.jpos.core.Configuration;import org.jpos.core.ConfigurationException;import org.jpos.iso.ISOUtil;import org.jpos.security.BaseSMAdapter;import org.jpos.security.EncryptedPIN;import org.jpos.security.SMAdapter;import org.jpos.security.SMException;import org.jpos.security.SecureDESKey;import org.jpos.util.LogEvent;import org.jpos.util.Logger;import org.jpos.util.SimpleMsg;/** * <p> * JCESecurityModule is an implementation of a security module in software. * It doesn't require any hardware device to work.<br> * JCESecurityModule also implements the SMAdapter, so you can view it: either * as a self contained security module adapter that doesn't need a security module * or a security module that plugs directly to jpos, so doesn't need * a separate adapter.<br> * It relies on Java(tm) Cryptography Extension (JCE), hence its name.<br> * JCESecurityModule relies on the JCEHandler class to do the low level JCE work. * </p> * <p> * WARNING: This version of JCESecurityModule is meant for testing purposes and * NOT for life operation, since the Local Master Keys are stored in CLEAR on * the system's disk. Comming versions of JCESecurity Module will rely on * java.security.KeyStore for a better protection of the Local Master Keys. * </p> * @author Hani Samuel Kirollos * @version $Revision: 2706 $ $Date: 2009-03-05 09:24:43 -0200 (Thu, 05 Mar 2009) $ */public class JCESecurityModule extends BaseSMAdapter {    /**     * Creates an uninitialized JCE Security Module, you need to setConfiguration to initialize it     */    public JCESecurityModule () {        super();    }    /**     * @param lmkFile Local Master Keys filename of the JCE Security Module     * @throws SMException     */    public JCESecurityModule (String lmkFile) throws SMException    {        init(null, lmkFile, false);    }    public JCESecurityModule (String lmkFile, String jceProviderClassName) throws SMException    {        init(jceProviderClassName, lmkFile, false);    }    public JCESecurityModule (Configuration cfg, Logger logger, String realm) throws ConfigurationException    {        setLogger(logger, realm);        setConfiguration(cfg);    }    /**     * Configures a JCESecurityModule     * @param cfg The following properties are read:<br>     *    lmk: Local Master Keys file (The only required parameter)<br>     *    jce: JCE Provider Class Name, if not provided, it defaults to: com.sun.crypto.provider.SunJCE<br>     *    rebuildlmk: (true/false), rebuilds the Local Master Keys file with new keys (WARNING: old keys will be erased)<br>     *    cbc-mac: Cipher Block Chaining MAC algorithm name for given JCE Provider.<br>     *             Default is ISO9797ALG3MACWITHISO7816-4PADDING from BouncyCastle provider (known as Retail-MAC)<br>     *             that is suitable for most of interfaces with double length MAC key<br>     *             ANSI X9.19 aka ISO/IEC 9797-1 MAC algorithm 3 padding method 2 - ISO7816<br>     *    ede-mac: Encrypt Decrypt Encrypt MAC algorithm name for given JCE Provider.<br>     *             Default is DESEDEMAC from BouncyCastle provider<br>     *             that is suitable for BASE24 with double length MAC key<br>     *             ANSI X9.19<br>     * @throws ConfigurationException     */    public void setConfiguration (Configuration cfg) throws ConfigurationException {        this.cfg = cfg;        try {            init(cfg.get("provider"), cfg.get("lmk"), cfg.getBoolean("rebuildlmk"));        } catch (SMException e) {            throw  new ConfigurationException(e);        }    }    public SecureDESKey generateKeyImpl (short keyLength, String keyType) throws SMException {        SecureDESKey generatedSecureKey = null;        Key generatedClearKey = jceHandler.generateDESKey(keyLength);        generatedSecureKey = encryptToLMK(keyLength, keyType, generatedClearKey);        return  generatedSecureKey;    }    public SecureDESKey importKeyImpl (short keyLength, String keyType, byte[] encryptedKey,            SecureDESKey kek, boolean checkParity) throws SMException {        SecureDESKey importedKey = null;        // decrypt encrypted key        Key clearKEY = jceHandler.decryptDESKey(keyLength, encryptedKey, decryptFromLMK(kek),                checkParity);        // Encrypt Key under LMK        importedKey = encryptToLMK(keyLength, keyType, clearKEY);        return  importedKey;    }    public byte[] exportKeyImpl (SecureDESKey key, SecureDESKey kek) throws SMException {        byte[] exportedKey = null;        // get key in clear        Key clearKey = decryptFromLMK(key);        // Encrypt key under kek        exportedKey = jceHandler.encryptDESKey(key.getKeyLength(), clearKey, decryptFromLMK(kek));        return  exportedKey;    }    public EncryptedPIN encryptPINImpl (String pin, String accountNumber) throws SMException {        EncryptedPIN encryptedPIN = null;        byte[] clearPINBlock = calculatePINBlock(pin, FORMAT00, accountNumber);        // Encrypt        byte[] translatedPINBlock = jceHandler.encryptData(clearPINBlock, getLMK(PINLMKIndex));        encryptedPIN = new EncryptedPIN(translatedPINBlock, FORMAT00, accountNumber);        return  encryptedPIN;    }    public String decryptPINImpl (EncryptedPIN pinUnderLmk) throws SMException {        String pin = null;        byte[] clearPINBlock = jceHandler.decryptData(pinUnderLmk.getPINBlock(),                getLMK(PINLMKIndex));        pin = calculatePIN(clearPINBlock, pinUnderLmk.getPINBlockFormat(), pinUnderLmk.getAccountNumber());        return  pin;    }    public EncryptedPIN importPINImpl (EncryptedPIN pinUnderKd1, SecureDESKey kd1) throws SMException {        EncryptedPIN pinUnderLmk = null;        // read inputs        String accountNumber = pinUnderKd1.getAccountNumber();        // Use FORMAT00 for encrypting PIN under LMK        byte destinationPINBlockFormat = FORMAT00;        // get clear PIN        byte[] clearPINBlock = jceHandler.decryptData(pinUnderKd1.getPINBlock(),                decryptFromLMK(kd1));        // extract clear pin (as entered by card holder)        String pin = calculatePIN(clearPINBlock, pinUnderKd1.getPINBlockFormat(),                accountNumber);        // Format PIN Block using proprietary FORMAT00 to be encrypetd under LMK        clearPINBlock = calculatePINBlock(pin, destinationPINBlockFormat, accountNumber);        // encrypt PIN        byte[] translatedPINBlock = jceHandler.encryptData(clearPINBlock, getLMK(PINLMKIndex));        pinUnderLmk = new EncryptedPIN(translatedPINBlock, destinationPINBlockFormat,                accountNumber);        return  pinUnderLmk;    }    public EncryptedPIN exportPINImpl (EncryptedPIN pinUnderLmk, SecureDESKey kd2,            byte destinationPINBlockFormat) throws SMException {        EncryptedPIN exportedPIN = null;        String accountNumber = pinUnderLmk.getAccountNumber();        // process        // get clear PIN        byte[] clearPINBlock = jceHandler.decryptData(pinUnderLmk.getPINBlock(),                getLMK(PINLMKIndex));        // extract clear pin        String pin = calculatePIN(clearPINBlock, pinUnderLmk.getPINBlockFormat(),                accountNumber);        clearPINBlock = calculatePINBlock(pin, destinationPINBlockFormat, accountNumber);        // encrypt PIN        byte[] translatedPINBlock = jceHandler.encryptData(clearPINBlock, decryptFromLMK(kd2));        exportedPIN = new EncryptedPIN(translatedPINBlock, destinationPINBlockFormat,                accountNumber);        return  exportedPIN;    }    public EncryptedPIN translatePINImpl (EncryptedPIN pinUnderKd1, SecureDESKey kd1,            SecureDESKey kd2, byte destinationPINBlockFormat) throws SMException {        EncryptedPIN translatedPIN = null;        String accountNumber = pinUnderKd1.getAccountNumber();        // get clear PIN        byte[] clearPINBlock = jceHandler.decryptData(pinUnderKd1.getPINBlock(),                decryptFromLMK(kd1));        String pin = calculatePIN(clearPINBlock, pinUnderKd1.getPINBlockFormat(),                accountNumber);        // Reformat PIN Block        clearPINBlock = calculatePINBlock(pin, destinationPINBlockFormat, accountNumber);        // encrypt PIN        byte[] translatedPINBlock = jceHandler.encryptData(clearPINBlock, decryptFromLMK(kd2));        translatedPIN = new EncryptedPIN(translatedPINBlock, destinationPINBlockFormat,                accountNumber);        return  translatedPIN;    }    /**     * Generates CBC-MAC (Cipher Block Chaining Message Authentication Code)     * for some data.     *     * @param data the data to be MACed     * @param kd the key used for MACing     * @return generated CBC-MAC bytes     * @throws SMException     */    protected byte[] generateCBC_MACImpl (byte[] data, SecureDESKey kd) throws SMException {        LogEvent evt = new LogEvent(this, "jce-provider-cbc-mac");        try {          return generateMACImpl(data,kd,cfg.get("cbc-mac","ISO9797ALG3MACWITHISO7816-4PADDING"),evt);        } catch (Exception e) {          Logger.log(evt);          throw  e instanceof SMException ? (SMException)e : new SMException(e);        }    }    /**     * Generates EDE-MAC (Encrypt Decrypt Encrypt Message Authentication Code)     * for some data.     *     * @param data the data to be MACed     * @param kd the key used for MACing     * @return generated EDE-MAC bytes     * @throws SMException     */    protected byte[] generateEDE_MACImpl (byte[] data, SecureDESKey kd) throws SMException {        LogEvent evt = new LogEvent(this, "jce-provider-ede-mac");        try {          return generateMACImpl(data,kd,cfg.get("ede-mac","DESEDEMAC"),evt);        } catch (Exception e) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -