加密解密算法java实现(6)—RSA 分段加解密的例子

一、maven

<dependency>    
        <groupId>commons-codec</groupId>    
        <artifactId>commons-codec</artifactId>    
        <version>1.9</version>    
</dependency>  
二、支持密钥对生成、分段加密的帮助类

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

import org.apache.commons.codec.binary.Base64;

public class RsaHelper {
	/**
	 * 生成公钥、私钥对(keysize=1024)
	 * @param keySize
	 * @return
	 */
	public RsaHelper.KeyPairInfo getKeyPair() {
		return getKeyPair(1024);
	}
	/**
	 * 生成公钥、私钥对
	 * @param keySize
	 * @return
	 */
	public RsaHelper.KeyPairInfo getKeyPair(int keySize) {
		try {
			KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
			// 初始化密钥对生成器,密钥大小一般要大于1024位,
//			String aa=String.valueOf(new Random().nextInt(999999999));
//			SecureRandom random = new SecureRandom(aa.getBytes()); //?????
//			keyPairGen.initialize(keySize,random);
			keyPairGen.initialize(keySize);
			// 生成一个密钥对,保存在keyPair中
			KeyPair keyPair = keyPairGen.generateKeyPair();
			// 得到私钥
			RSAPrivateKey oraprivateKey = (RSAPrivateKey) keyPair.getPrivate();
			// 得到公钥
			RSAPublicKey orapublicKey = (RSAPublicKey) keyPair.getPublic();
			
			RsaHelper.KeyPairInfo pairInfo = new RsaHelper.KeyPairInfo(keySize);
			//公钥
			byte[] publicKeybyte = orapublicKey.getEncoded();
			String publicKeyString = Base64.encodeBase64String(publicKeybyte);
			pairInfo.setPublicKey(publicKeyString);
			//私钥
			byte[] privateKeybyte = oraprivateKey.getEncoded();
			String privateKeyString = Base64.encodeBase64String(privateKeybyte);
			pairInfo.setPrivateKey(privateKeyString);
			
			return pairInfo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 获取公钥对象
	 * @param publicKeyBase64
	 * @return
	 * @throws InvalidKeySpecException
	 * @throws NoSuchAlgorithmException
	 */
	public PublicKey getPublicKey(String publicKeyBase64) 
			throws InvalidKeySpecException,NoSuch
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
非常感谢您的提问。关于Java RSA分段加解密,我可以给您提供一些相关的信息和代码示例。RSA是一种非对称加密算法,可以实现安全的数据传输和数据存储。但是,RSA算法在处理大量数据时可能会出现性能问题。因此,我们需要将大块数据分成小块进行加密解密。以下是Java RSA分段加解密的代码示例: ``` // RSA密钥长度,推荐使用2048位及以上 private static final int RSA_KEY_SIZE = 2048; // 分段加密的块大小,推荐使用RSA密钥长度-11 private static final int BLOCK_SIZE = RSA_KEY_SIZE / 8 - 11; // 生成RSA密钥对 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(RSA_KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 分段加密 public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); int inputLen = data.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] block; int i = 0; while (inputLen - offSet > 0) { if (inputLen - offSet > BLOCK_SIZE) { block = cipher.doFinal(data, offSet, BLOCK_SIZE); } else { block = cipher.doFinal(data, offSet, inputLen - offSet); } out.write(block, 0, block.length); i++; offSet = i * BLOCK_SIZE; } byte[] encryptedData = out.toByteArray(); out.close(); return encryptedData; } // 分段解密 public static byte[] decrypt(byte[] encryptedData, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] block; int i = 0; while (inputLen - offSet > 0) { if (inputLen - offSet > RSA_KEY_SIZE / 8) { block = cipher.doFinal(encryptedData, offSet, RSA_KEY_SIZE / 8); } else { block = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(block, 0, block.length); i++; offSet = i * (RSA_KEY_SIZE / 8); } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; } ``` 通过以上代码示例,您可以实现Java RSA分段加解密。请注意,RSA算法只适合加密小块数据,不适合加密大块数据,否则可能会出现性能问题。如果您需要加密大块数据,可以考虑使用对称加密算法,如AES。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值