美文网首页
加密方案:rsa使用公钥对字符串加密和私钥解密

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

作者: deli6780 | 来源:发表于2018-09-25 08:53 被阅读143次

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();

    }

}

相关文章

  • openssl终端命令RSA和DES加解密

    RSA 1、生成公钥和私钥 2、加密解密文件 公钥加密私钥解密 私钥加密公钥解密 3、在程序中使用依次生成 csr...

  • iOS RSA加签和验签(SHA1WithRSA)

    RSA 简介 RSA是一种非对称加密算法,使用公钥加密就可以使用私钥解密,使用私钥加密就可以使用公钥解密。RSA公...

  • Rsa加解密

    /*** Rsa 加解密* 用法:* (1)公钥加密,私钥解密* (2)私钥加密,公钥解密*/class ...

  • RSA非对称加密算法

    RSA算法,经典非对称加密算法,通过生成公钥 私钥 进行加密解密 公钥加密 私钥解密 反之 私钥加密 公钥...

  • iOS RSA加签和验签

    RSA是一种非对称加密算法,使用公钥加密就可以使用私钥解密,使用私钥加密就可以使用公钥解密。RSA公钥对外公开,私...

  • 加密相关

    公钥加密 私钥解密 ,私钥加密,公钥验证(签名) HTTPS -- AFSecurityPolicy RSA ...

  • RSA加密算法

    RSA加解密、签名验签過程 RSA加密是一种非对称加密,通常使用公钥加密,私钥解密,私钥签名,公钥验签。私钥是個人...

  • 苹果应用签名原理

    公钥和私钥 加密:公钥加密,私钥解密 签名:私钥加密,公钥解密 公私钥是一对,谁都可以加密和解密,具体看使用场景加...

  • RSA在线加密解密

    在线RSA公私钥加密解密,支持私钥加密->公钥解密、公钥加密->私钥解密,支持PKCS#1、PKCS#8格式公私钥...

  • iOS之密码学

    一、非对称加密 - RSA : +公钥加密,私钥解密; + 私钥加密,公钥解密; + 只能通过因式分解来破解 二、...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/pljkoftx.html