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

Java tutorial

Introduction

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

Source

package com.vico.license.util.rsa;

import com.vico.license.util.ByteArrayToObj;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.Key;

public class RSAdoEncrypt {

    /**
     * RSA::  1.?
     * 2.????,????,
     * 3.????,????,?,?????
     * :???
     *
     * @param source ?
     * @return Sring b1 ?
     * @throws Exception
     */

    private static final String ALGORITHM = "RSA";

    public static String encrypt(String source, byte[] publickey) throws Exception {

        String path = Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath();
        Key publicKey = null;

        publicKey = (Key) ByteArrayToObj.ByteToObject(publickey);

        /** Cipher???RSA */
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] b = source.getBytes("UTF-8");

        /** ?*/
        byte[] b1 = cipher.doFinal(b);

        String encryptedcode = Base64.encodeBase64String(b1);
        return encryptedcode;
    }
}