cn.mrdear.pay.util.RSAUtils.java Source code

Java tutorial

Introduction

Here is the source code for cn.mrdear.pay.util.RSAUtils.java

Source

/*
 * Copyright 2005-2015 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package cn.mrdear.pay.util;

import com.sun.istack.internal.NotNull;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 * Utils - RSA/
 */
public final class RSAUtils {

    /**  */
    private static final String KEY_ALGORITHM = "RSA";

    /** / */
    private static final String TRANSFORMATION = "RSA/ECB/PKCS1Padding";

    /** ??? */
    private static final Provider PROVIDER = new BouncyCastleProvider();

    /**
     * ??
     */
    private RSAUtils() {
    }

    /**
     * ?
     * 
     * @param keySize
     *            ?
     * @return 
     */
    public static KeyPair generateKeyPair(int keySize) {
        Assert.state(keySize > 0);

        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM, PROVIDER);
            keyPairGenerator.initialize(keySize);
            return keyPairGenerator.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * ??
     * 
     * @param encodedKey
     *            ?
     * @return ?
     */
    public static PrivateKey generatePrivateKey(byte[] encodedKey) {

        try {
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM, PROVIDER);
            return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeySpecException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * ??
     * 
     * @param keyString
     *            (BASE64?)
     * @return ?
     */
    public static PrivateKey generatePrivateKey(String keyString) {
        Assert.isNotEmpty(keyString);

        return generatePrivateKey(Base64.decodeBase64(keyString));
    }

    /**
     * ?
     * 
     * @param encodedKey
     *            ?
     * @return 
     */
    public static PublicKey generatePublicKey(byte[] encodedKey) {
        Assert.notNull(encodedKey);

        try {
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM, PROVIDER);
            return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeySpecException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * ?
     * 
     * @param keyString
     *            (BASE64?)
     * @return 
     */
    public static PublicKey generatePublicKey(@NotNull String keyString) {

        return generatePublicKey(Base64.decodeBase64(keyString));
    }

    /**
     * ?
     * 
     * @param key
     *            
     * @return (BASE64?)
     */
    public static String getKeyString(Key key) {
        Assert.notNull(key);

        return Base64.encodeBase64String(key.getEncoded());
    }

    /**
     * ?
     * 
     * @param type
     *            
     * @param inputStream
     *            ?
     * @param password
     *            ?
     * @return 
     */
    public static Key getKey(String type, InputStream inputStream, String password) {
        Assert.isNotEmpty(type);
        Assert.notNull(inputStream);

        try {
            KeyStore keyStore = KeyStore.getInstance(type, PROVIDER);
            keyStore.load(inputStream, password != null ? password.toCharArray() : null);
            String alias = keyStore.aliases().hasMoreElements() ? keyStore.aliases().nextElement() : null;
            return keyStore.getKey(alias, password != null ? password.toCharArray() : null);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (CertificateException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (UnrecoverableKeyException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * ??
     * 
     * @param type
     *            
     * @param inputStream
     *            ?
     * @return ?
     */
    public static Certificate getCertificate(String type, InputStream inputStream) {
        Assert.isNotEmpty(type);
        Assert.notNull(inputStream);

        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance(type, PROVIDER);
            return certificateFactory.generateCertificate(inputStream);
        } catch (CertificateException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * ???
     * 
     * @param algorithm
     *            ??
     * @param privateKey
     *            ?
     * @param data
     *            ?
     * @return ??
     */
    public static byte[] sign(String algorithm, PrivateKey privateKey, byte[] data) {
        Assert.isNotEmpty(algorithm);
        Assert.notNull(privateKey);
        Assert.notNull(data);

        try {
            Signature signature = Signature.getInstance(algorithm, PROVIDER);
            signature.initSign(privateKey);
            signature.update(data);
            return signature.sign();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (SignatureException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * ???
     * 
     * @param algorithm
     *            ??
     * @param publicKey
     *            
     * @param sign
     *            ??
     * @param data
     *            ?
     * @return ??
     */
    public static boolean verify(String algorithm, PublicKey publicKey, byte[] sign, byte[] data) {
        Assert.isNotEmpty(algorithm);
        Assert.notNull(publicKey);
        Assert.notNull(sign);
        Assert.notNull(data);

        try {
            Signature signature = Signature.getInstance(algorithm, PROVIDER);
            signature.initVerify(publicKey);
            signature.update(data);
            return signature.verify(sign);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (SignatureException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * ???
     * 
     * @param algorithm
     *            ??
     * @param certificate
     *            ?
     * @param sign
     *            ??
     * @param data
     *            ?
     * @return ??
     */
    public static boolean verify(String algorithm, Certificate certificate, byte[] sign, byte[] data) {
        Assert.isNotEmpty(algorithm);
        Assert.notNull(certificate);
        Assert.notNull(sign);
        Assert.notNull(data);

        try {
            Signature signature = Signature.getInstance(algorithm, PROVIDER);
            signature.initVerify(certificate);
            signature.update(data);
            return signature.verify(sign);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (SignatureException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * 
     * 
     * @param publicKey
     *            
     * @param data
     *            ?
     * @return 
     */
    public static byte[] encrypt(PublicKey publicKey, byte[] data) {
        Assert.notNull(publicKey);
        Assert.notNull(data);

        try {
            Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            return cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * 
     * 
     * @param privateKey
     *            ?
     * @param data
     *            ?
     * @return 
     */
    public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
        Assert.notNull(privateKey);
        Assert.notNull(data);

        try {
            Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            return cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

}