com.vico.license.util.rsa.RSAdoDecrypt.java Source code

Java tutorial

Introduction

Here is the source code for com.vico.license.util.rsa.RSAdoDecrypt.java

Source

package com.vico.license.util.rsa;

import com.vico.license.util.FileNames;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.Key;

public class RSAdoDecrypt {

    /**
     * 
     *
     * @param cryptograph
     * 
     * @return
     * @throws Exception
     */
    private static final String ALGORITHM = "RSA";
    private static final Logger logger = Logger.getLogger(RSAdoDecrypt.class);

    public static String decrypt(String cryptograph) throws Exception {
        Key privateKey;
        String path = Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath();
        ObjectInputStream ois = null;
        try {
            /** ? */
            ois = new ObjectInputStream(new FileInputStream(path + FileNames.PRIVATEKEY_NAME));
            privateKey = (Key) ois.readObject();
        } catch (Exception e) {
            throw e;
        } finally {
            ois.close();
        }

        /** Cipher?RSA */
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        /** ? */
        byte[] b1 = Base64.decodeBase64(cryptograph);
        /** ? */
        byte[] b = cipher.doFinal(b1);
        return new String(b);
    }
}