RSA 非对称加解密算法

本文档详细介绍了RSA非对称加密算法的实现过程,包括公钥和私钥的生成、加密和解密操作。通过示例代码展示了如何在Java中使用RSA进行加解密,并提供了关键步骤的解释,帮助读者深入理解RSA加密机制。
摘要由CSDN通过智能技术生成
package com.007.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;

import javax.crypto.Cipher;

import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.security.util.DerInputStream;
import sun.security.util.DerValue;

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;

/**
 * 
 * @author 007
 * @date Jan 26, 2015 4:27:10 PM
 * @version V1.0
 * @Description: TODO(Rsa加密)
 * 
 */
public class RSAMain {
	private static Logger _log = Logger.getLogger(RSAMain.class);
	private PublicKey pubKey = null;
	private PrivateKey priKey = null;

	private static RSAMain rsaMain;

	/**
	 * 加密长度 用4096 做的key,选500 和选400分段长度 机密后的密文长度也有很大差距 选大的加密后密文短
	 * 
	 * 
	 * 1024:加密分段长度选100 解密长度选128
	 * 4096:加密分段长度选500 解密长度选512
	 * 
	 * 
	 */
	private RSAMain() {

		String keyinfo = "MIICXAIBAAKBgQDOxNnegpUXw4W9L+G92VgNuCsmFQYqRuOSVsue61KL2t6jVSTuxYqkanA6sCI9JzRspY8eAY6VRjcctAvEkBgLMRPgpIvw0Mv7JxxjDcm6nFE37+IdizKXlqv+cFgSjJhCGrM6JPK5K+kiyO7tyksdcAOyU8uIaqmEeVVCpv7oqwIDAQABAoGBAL11HfSGTjpyC08oWn3scoRPpinExdtjDQ5/vR+mnAJN0AZ089xc4QZ6CM+dqCHElIKgyJxD7PPWXJVOXCIFriCdWoQlA77JxXXTgHOpk74XCMJNlV+jKscr5jJGhFuyfI7VWEMIFddBpys3+xgzVo6YRlwDEnbEIEAUfIZqNQiBAkEA7AV/R34Bj2MQMW2KG2R81My0zr4LGxrxMNAKnJcUmjl0vIw53xGH00mSEMhMb/mYwNibYQk0NYLkR8946McLywJBAOBFdj8It6ZGKHjq4lnl3GO0uxpJhcUUfmyOErkh3bbJtyp4X7BB999/ddkpwjYHDxzPwWYF7PGnJjSfor/duqECQEehe3mFIpAT7qwxpIKfwssKva0pPFGjoaPA4BeaOqR735QBsuaieWiEoZH6WEh7pgKROsb+dhvvd7zbmMR89HUCQF4kCkzPZfe//4EyZ62A1ZBs7xVLw4FE5wS+ZpGb66us5NxJBzG/n0MMB/YOl12CfQ+FDhO7bQjLNzhjwjdz5cECQFcITAKCy4s4OStjdC5s7hx/9qKGQD0+Ih59gnd14a8btINUAf5VE0pSKnjh/bo6u2JCtsYGgoZpilNgLc7Kxnw="; //密钥信息用 BASE64 编码加密过,需要先解密

		String key4096pub = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA+O7xM/5YtSTNOhM+K0m0S2kYlTDV3YHz9pyOp5E1cFNLiJRglNslnZrRHDIW4jvXOES2WuHk5/x65mtsm44ccuhY6pnjgDU87Du8HNoT/4x290KgjtfxZedZF2zzBSl9Q2kkRe+erJWA7+J22AUKv0Kyfkh64l+wgvoTR/UCcd1C/qil9QeFutOMt8zkmFDp47jcDA6EkPfaOfCEAJboql84tp1AAMgo6o10lgR8XN9UuyLRVT4NvpTJhEZufNqLWfGVqwQjODRIUA/57CDBWSfak1N36za+w7V9Ng+tO1aL2Uhw1UbpkW4slnUfb02BNEdTnVBImTfTGmM1b9k4kuZu5Kx9mV5sK7ztF6qWvpKiduwfg1ap1N/BNJ3h7SrFVQi1SElG1diZIHu1mf118GMCex04BOY8mYeMANq7EjNfBh8AN2ggdyLr2kZqHjIGlg77wnQqpzpJgYcFhfm6nodqMKUJ0r2Q2uOEM+teDk+DLYblygcVLCIm+l0gAWF7gWSFqrgk5gdqt8Spk1/BC5njkhFAo2CcWWhOnYpQBVRsV4/96Rd5v7y0MCIngdTg3o+18bnkR6/el3aYZawOzfmvgcOP7lr0yf0eU9iGDS74UQTykYuH7zDjx4fyvQA6ASrornJzPpdf+wK9B0WBp3o5qR1BA2xRyorpmJ+EpK0CAwEAAQ==";

		String pubkey4096data = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA+O7xM/5YtSTNOhM+K0m0" + "\r"
				+ "S2kYlTDV3YHz9pyOp5E1cFNLiJRglNslnZrRHDIW4jvXOES2WuHk5/x65mtsm44c" + "\r"
				+ "cuhY6pnjgDU87Du8HNoT/4x290KgjtfxZedZF2zzBSl9Q2kkRe+erJWA7+J22AUK" + "\r"
				+ "v0Kyfkh64l+wgvoTR/UCcd1C/qil9QeFutOMt8zkmFDp47jcDA6EkPfaOfCEAJbo" + "\r"
				+ "ql84tp1AAMgo6o10lgR8XN9UuyLRVT4NvpTJhEZufNqLWfGVqwQjODRIUA/57CDB" + "\r"
				+ "WSfak1N36za+w7V9Ng+tO1aL2Uhw1UbpkW4slnUfb02BNEdTnVBImTfTGmM1b9k4" + "\r"
				+ "kuZu5Kx9mV5sK7ztF6qWvpKiduwfg1ap1N/BNJ3h7SrFVQi1SElG1diZIHu1mf11" + "\r"
				+ "8GMCex04BOY8mYeMANq7EjNfBh8AN2ggdyLr2kZqHjIGlg77wnQqpzpJgYcFhfm6" + "\r"
				+ "nodqMKUJ0r2Q2uOEM+teDk+DLYblygcVLCIm+l0gAWF7gWSFqrgk5gdqt8Spk1/B" + "\r"
				+ "C5njkhFAo2CcWWhOnYpQBVRsV4/96Rd5v7y0MCIngdTg3o+18bnkR6/el3aYZawO" + "\r"
				+ "zfmvgcOP7lr0yf0eU9iGDS74UQTykYuH7zDjx4fyvQA6ASrornJzPpdf+wK9B0WB" + "\r" + "p3o5qR1BA2xRyorpmJ+EpK0CAwEAAQ==";

		String prikey4096data = "MIIJKQIBAAKCAgEA+O7xM/5YtSTNOhM+K0m0S2kYlTDV3YHz9pyOp5E1cFNLiJRglNslnZrRHDIW4jvXOES2WuHk5/x65mtsm44ccuhY6pnjgDU87Du8HNoT/4x290KgjtfxZedZF2zzBSl9Q2kkRe+erJWA7+J22AUKv0Kyfkh64l+wgvoTR/UCcd1C/qil9QeFutOMt8zkmFDp47jcDA6EkPfaOfCEAJboql84tp1AAMgo6o10lgR8XN9UuyLRVT4NvpTJhEZufNqLWfGVqwQjODRIUA/57CDBWSfak1N36za+w7V9Ng+tO1aL2Uhw1UbpkW4slnUfb02BNEdTnVBImTfTGmM1b9k4kuZu5Kx9mV5sK7ztF6qWvpKiduwfg1ap1N/BNJ3h7SrFVQi1SElG1diZIHu1mf118GMCex04BOY8mYeMANq7EjNfBh8AN2ggdyLr2kZqHjIGlg77wnQqpzpJgYcFhfm6nodqMKUJ0r2Q2uOEM+teDk+DLYblygc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值