com.security.ch08_rsa.RSACoderTextKey.java Source code

Java tutorial

Introduction

Here is the source code for com.security.ch08_rsa.RSACoderTextKey.java

Source

package com.security.ch08_rsa;

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;

public abstract class RSACoderTextKey {

    /**
     * ?
     */
    public static final String KEY_ALGORITHM = "RSA";

    /**
     * License code sent to user
     */
    private static final String LICENSE_CODE = "LicenseCode";

    /**
     * 
     */
    private static final String PUBLIC_KEY = "RSAPublicKey";

    /**
     * ?
     */
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    /**
     * RSA
     * 1024?
     * 64?
     * 51265536?
     */
    private static final int KEY_SIZE = 1024;

    public static final String UTF_8 = "UTF-8";

    /**
     * Decrypt data by private key
     * @param data base64 encoded string
     * @param key base64 encoded string
     * @return decrypted string
     * @throws Exception
     */
    public static String decryptByPrivateKey(String data, String key) throws Exception {
        byte[] result = decryptByPrivateKey(Base64.decodeBase64(data), Base64.decodeBase64(key));
        return new String(result, UTF_8);
    }

    /**
     * ?
     *
     * @param data
     *            ?
     * @param key
     *            ?
     * @return byte[] ?
     * @throws Exception
     */
    private static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {

        // ??
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        // ??
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

        // ?
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        return cipher.doFinal(data);
    }

    /**
     * Decrypt data with provided public key
     * @param data base64 encoded string
     * @param key base64 encoded string
     * @return decrypted string
     * @throws Exception
     */
    public static String decryptByPublicKey(String data, String key) throws Exception {
        byte[] result = decryptByPublicKey(Base64.decodeBase64(data), Base64.decodeBase64(key));
        return new String(result, UTF_8);
    }

    /**
     * 
     *
     * @param data
     *            ?
     * @param key
     *            
     * @return byte[] ?
     * @throws Exception
     */
    private static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {

        // ?
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        // ?
        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);

        // ?
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return cipher.doFinal(data);
    }

    /**
     * Encrypt a string by provide public key
     * @param data string to encrypt
     * @param key base64 encoded string
     * @return base64 encoded data
     * @throws Exception
     */
    public static String encryptByPublicKey(String data, String key) throws Exception {
        byte[] result = encryptByPublicKey(data.getBytes(UTF_8), Base64.decodeBase64(key));
        return Base64.encodeBase64String(result);
    }

    /**
     * 
     *
     * @param data
     *            ?
     * @param key
     *            
     * @return byte[] ?
     * @throws Exception
     */
    private static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {

        // ?
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);

        // ?
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        return cipher.doFinal(data);
    }

    /**
     * Encrypt a string with provided private key
     * @param data string to decrypt
     * @param key private key encoded with base64
     * @return base64 encoded string
     * @throws Exception
     */
    public static String encryptByPrivateKey(String data, String key) throws Exception {
        byte[] result = encryptByPrivateKey(data.getBytes(UTF_8), Base64.decodeBase64(key));
        return Base64.encodeBase64String(result);
    }

    /**
     * ?
     *
     * @param data
     *            ?
     * @param key
     *            ?
     * @return byte[] ?
     * @throws Exception
     */
    private static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {

        // ??
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        // ??
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

        // ?
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.ENCRYPT_MODE, privateKey);

        return cipher.doFinal(data);
    }

    public static String getPrivateKeyString(Map<String, String> keyMap) {
        return keyMap.get(PRIVATE_KEY);
    }

    public static String getPublicKeyString(Map<String, String> keyMap) {
        return keyMap.get(PUBLIC_KEY);
    }

    /**
     * ?
     *
     * @return Map Map
     * @throws Exception
     */
    public static Map<String, String> initKey() throws Exception {
        UUID licenseCode = UUID.randomUUID();
        return initKey(licenseCode);
    }

    /**
     * ?
     *
     * @return Map Map
     * @throws Exception
     */
    public static Map<String, String> initKey(UUID licenseCode) throws Exception {
        // ?
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

        // ??
        //        keyPairGen.initialize(KEY_SIZE);
        keyPairGen.initialize(KEY_SIZE, new SecureRandom(licenseCode.toString().getBytes(UTF_8)));

        // ?
        KeyPair keyPair = keyPairGen.generateKeyPair();

        // 
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

        // ?
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

        // ?
        Map<String, String> keyMap = new HashMap<String, String>();

        keyMap.put(PUBLIC_KEY, Base64.encodeBase64String(publicKey.getEncoded()));
        keyMap.put(PRIVATE_KEY, Base64.encodeBase64String(privateKey.getEncoded()));
        keyMap.put(LICENSE_CODE, licenseCode.toString());

        return keyMap;
    }
}