JAVA RSA加密解密代码范例(byte[]版)

Posted 柳鲲鹏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA RSA加密解密代码范例(byte[]版)相关的知识,希望对你有一定的参考价值。

具体代码:

package tsoffice;


import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

public final class TestRSA
{
    private final static String ALGORITHM = "RSA";
    private final static String CHARSET   = "utf-8";

    /**
     * 密钥长度 于原文长度对应 以及越长速度越慢
     */
    private final static int KEY_SIZE = 512;

    private static PublicKey  publicKey;
    private static PrivateKey privateKey;
    
    /**
     * 随机生成密钥对
     */
    public static void genKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM);
        keyPairGen.initialize(KEY_SIZE, new SecureRandom());
        KeyPair keyPair = keyPairGen.generateKeyPair();
        publicKey  = keyPair.getPublic();
        privateKey = keyPair.getPrivate();
    }

    public static byte[] encrypt(String str, PublicKey pubKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        
        return cipher.doFinal(str.getBytes(CHARSET));
    }

    public static byte[] decrypt(byte[] inputByte, PrivateKey priKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        return cipher.doFinal(inputByte);
    }

    public static void main(String[] args) throws Exception {
        long temp = System.currentTimeMillis();
        //生成公钥和私钥
        genKeyPair();
        System.out.println("生成密钥消耗时间:" + (System.currentTimeMillis() - temp) / 1000.0 + "秒");
        
        String message = "公钥加密测试-:泰山Office";
        
        System.out.println("原文:" + message);
        temp = System.currentTimeMillis();
        byte[] messageEn = encrypt(message, publicKey);
        System.out.println("密文:" + messageEn.length);
        System.out.println("加密消耗时间:" + (System.currentTimeMillis() - temp) / 1000.0 + "秒");
        
        temp = System.currentTimeMillis();
        byte[] messageDe = decrypt(messageEn, privateKey);
        System.out.println("解密:" + new String(messageDe));
        System.out.println("解密消耗时间:" + (System.currentTimeMillis() - temp) / 1000.0 + "秒");
        
    }
}

以上是关于JAVA RSA加密解密代码范例(byte[]版)的主要内容,如果未能解决你的问题,请参考以下文章

JAVA 前端用RSA.js加密 传到后端解密有乱码

java加密算法之RSA篇

java加密算法之RSA篇

java加密算法之RSA篇

JAVA DES加密解密代码范例

RSA加密大文件