加密方案:rsa使用公钥对字符串加密和私钥解密

package com.duoduozb.constants;

import java.io.ByteArrayOutputStream;

import java.security.Key;

import java.security.KeyFactory;

import java.security.PublicKey;

import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

/**

* @author Created by deli

* @date 2018/9/21 08:14

* @description rsa加密工具

*/

public class RsaEncryptUtil {

    /** */

/**

* 加密算法RSA

*/

    public static final String KEY_ALGORITHM = "RSA";// RSA/NONE/NoPadding,RSA/NONE/PKCS1Padding

    /**

* String to hold name of the encryption padding.

*/

    public static final String PADDING = "RSA/NONE/PKCS1Padding";// RSA/NONE/NoPadding

    /**

* String to hold name of the security provider.

*/

    public static final String PROVIDER = "BC";

    /** */

/**

* 签名算法

*/

    public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

    /** */

/**

* 获取公钥的key

*/

    private static final String PUBLIC_KEY = "RSAPublicKey";

    /** */

/**

* 获取私钥的key

*/

    private static final String PRIVATE_KEY = "RSAPrivateKey";

    /** */

/**

* RSA最大加密明文大小

*/

    private static final int MAX_ENCRYPT_BLOCK = 117;

    /** */

/**

* RSA最大解密密文大小

*/

    private static final int MAX_DECRYPT_BLOCK = 128;

    /*

* 公钥加密

*/

    public static String encryptByPublicKey(String str) throws Exception {

        Cipher cipher = Cipher.getInstance(PADDING, PROVIDER);

        // 获得公钥

        Key publicKey = getPublicKey();

        // 用公钥加密

        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        // 读数据源

        byte[] data = str.getBytes("UTF-8");

        int inputLen = data.length;

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int offSet = 0;

        byte[] cache;

        int i = 0;

        // 对数据分段加密

        while (inputLen - offSet > 0) {

            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {

                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);

            } else {

                cache = cipher.doFinal(data, offSet, inputLen - offSet);

            }

            out.write(cache, 0, cache.length);

            i++;

            offSet = i * MAX_ENCRYPT_BLOCK;

        }

        byte[] encryptedData = out.toByteArray();

        out.close();

        return Base64Util.encode(encryptedData);

    }

    /*

* 公钥解密

*/

    public static String decryptByPublicKey(String str) throws Exception {

        Cipher cipher = Cipher.getInstance(PADDING, PROVIDER);

        // 获得公钥

        Key publicKey = getPublicKey();

        // 用公钥解密

        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        // 读数据源

        byte[] encryptedData = Base64Util.decode(str);

        int inputLen = encryptedData.length;

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int offSet = 0;

        byte[] cache;

        int i = 0;

        // 对数据分段解密

        while (inputLen - offSet > 0) {

            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {

                cache = cipher

                        .doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);

            } else {

                cache = cipher

                        .doFinal(encryptedData, offSet, inputLen - offSet);

            }

            out.write(cache, 0, cache.length);

            i++;

            offSet = i * MAX_DECRYPT_BLOCK;

        }

        byte[] decryptedData = out.toByteArray();

        out.close();

        return new String(decryptedData, "UTF-8");

    }

    /**

* 读取公钥

*

    * @return

    * @throws Exception

    * @comment

    */

    private static Key getPublicKey() throws Exception {

        String key = GlobalConstants.ras_public_key;

        byte[] keyBytes;

        keyBytes = Base64Util.decode(key);

        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        return publicKey;

    }

}

package com.duoduozb.constants;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import it.sauronsoftware.base64.Base64;

/**

* @author Created by deli

* @date 2018/9/21 08:17

* @description rsa 工具类

*/

public class Base64Util {

    /** */

/**

* 文件读取缓冲区大小

*/

    private static final int CACHE_SIZE = 1024;

    /** */

/**

    *

    * BASE64字符串解码为二进制数据

    *

    *

    * @param base64

    * @return

    * @throws Exception

    */

    public static byte[] decode(String base64) throws Exception {

        return Base64.decode(base64.getBytes());

    }

    /** */

/**

    *

    * 二进制数据编码为BASE64字符串

    *

    *

    * @param bytes

    * @return

    * @throws Exception

    */

    public static String encode(byte[] bytes) throws Exception {

        return new String(Base64.encode(bytes));

    }

    /** */

/**

    *

    * 将文件编码为BASE64字符串

    *

    *

    * 大文件慎用,可能会导致内存溢出

    *

    *

    * @param filePath

    *            文件绝对路径

    * @return

    * @throws Exception

    */

    public static String encodeFile(String filePath) throws Exception {

        byte[] bytes = fileToByte(filePath);

        return encode(bytes);

    }

    /** */

/**

    *

    * BASE64字符串转回文件

    *

    *

    * @param filePath

    *            文件绝对路径

    * @param base64

    *            编码字符串

    * @throws Exception

    */

    public static void decodeToFile(String filePath, String base64)

            throws Exception {

        byte[] bytes = decode(base64);

        byteArrayToFile(bytes, filePath);

    }

    /** */

/**

    *

    * 文件转换为二进制数组

    *

    *

    * @param filePath

    *            文件路径

    * @return

    * @throws Exception

    */

    public static byte[] fileToByte(String filePath) throws Exception {

        byte[] data = new byte[0];

        File file = new File(filePath);

        if (file.exists()) {

            FileInputStream in = new FileInputStream(file);

            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);

            byte[] cache = new byte[CACHE_SIZE];

            int nRead = 0;

            while ((nRead = in.read(cache)) != -1) {

                out.write(cache, 0, nRead);

                out.flush();

            }

            out.close();

            in.close();

            data = out.toByteArray();

        }

        return data;

    }

    /** */

/**

    *

    * 二进制数据写文件

    *

    *

    * @param bytes

    *            二进制数据

    * @param filePath

    *            文件生成目录

*/

    public static void byteArrayToFile(byte[] bytes, String filePath)

            throws Exception {

        InputStream in = new ByteArrayInputStream(bytes);

        File destFile = new File(filePath);

        if (!destFile.getParentFile().exists()) {

            destFile.getParentFile().mkdirs();

        }

        destFile.createNewFile();

        OutputStream out = new FileOutputStream(destFile);

        byte[] cache = new byte[CACHE_SIZE];

        int nRead = 0;

        while ((nRead = in.read(cache)) != -1) {

            out.write(cache, 0, nRead);

            out.flush();

        }

        out.close();

        in.close();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Base64.java public final class Base64 { static private ...
    BUG弄潮儿阅读 844评论 0 0
  • 概述 之前一直对加密相关的算法知之甚少,只知道类似DES、RSA等加密算法能对数据传输进行加密,且各种加密算法各有...
    Henryzhu阅读 3,068评论 0 14
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,156评论 19 139
  • 福到了 老词啦 总觉得这个对联很经典 学写隶书 感觉楷书不见长进 米芾的影子也木看出来 特写一下 横批也要有滴 写...
    何处落风送暗香阅读 446评论 0 5
  • 退去了激情,他发现他不爱你了,他要求分手。 你难过,哭泣着答应,事后的每一天,你会伤感,会睹物思人,会一个人无厘头...
    几何月阅读 332评论 0 1