一.rsa算法介绍:百度百科
二.首先通过rsa算法生成一对公私钥
private static void generateKeyPair() throws Exception{ Security.addProvider(new com.sun.crypto.provider.SunJCE()); /** RSA算法要求有一个可信任的随机数源 */ SecureRandom sr = new SecureRandom(); /** 为RSA算法创建一个KeyPairGenerator对象 */ KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); /** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */ kpg.initialize(4096, sr); /** 生成密匙对 */ KeyPair kp = kpg.generateKeyPair(); /** 得到公钥 */ Key publicKey = kp.getPublic(); /** 得到私钥 */ Key privateKey = kp.getPrivate(); /** 用对象流将生成的密钥写入文件 */ ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(ResourceBundleUtil.getSystem(WYPublicKey)/*"yxb_ok_publickey.keystore"*/)); ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(ResourceBundleUtil.getSystem(WYPrivateKey)/*"text_privatekey.keystore"*/)); oos1.writeObject(publicKey); oos2.writeObject(privateKey); /** 清空缓存,关闭文件输出流 */ oos1.close(); oos2.close(); }
三.对字符串进行分段加密操作
public static String encrypt(String source,String publickey) throws Exception{ //source为需要加密的对应 publickey-rsa公钥 /** 将文件中的公钥对象读出 */ ObjectInputStream ois = new ObjectInputStream(RsaUtil.class.getResourceAsStream("/"+publickey)); Key key = (Key) ois.readObject(); ois.close(); /** 得到Cipher对象来实现对源数据的RSA加密 */ Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] b = source.getBytes("utf-8"); byte[] b1 = null; /** 执行加密操作 */ for (int i = 0; i < b.length; i += 501) { byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(b, i,i + 501)); b1 = ArrayUtils.addAll(b1, doFinal); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(b1); }
2.对加密后参数进行对应的解密:
public static String decrypt(String cryptograph,String privatekey) throws Exception{ //cryptograph-通过rsa公钥加密得到的参数 privatekey-与公钥对应的私钥 /** 将文件中的私钥对象读出 */ // cryptograph= new String(cryptograph.getBytes(),"gbk"); ObjectInputStream ois = new ObjectInputStream(RsaUtil.class.getResourceAsStream("/"+privatekey)); Key key = (Key) ois.readObject(); /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */ Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, key); BASE64Decoder decoder = new BASE64Decoder(); byte[] b1 = decoder.decodeBuffer(cryptograph); /** 执行解密操作 */ byte[] b = null; StringBuilder sb = new StringBuilder(); for (int i = 0; i < b1.length; i += 512) { byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(b1, i, i + 512)); sb.append(new String(doFinal,"utf-8")); } return sb.toString(); }
3.测试方法
public static void main(String[] args) throws Exception { String a = "abc"; String b = encrypt(a,"yxb_ok_publickey.keystore"); String c = decrypt(b,"yxb_ok_privatekey.keystore"); System.out.println("加密前的参数:"+a); System.out.println("加密后的参数为:"+b); System.out.println("解密后的参数:"+c); }
4.运行结果为:
5.项目导入的包为:
import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.FileReader; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.Security; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.crypto.Cipher; import org.apache.commons.lang3.ArrayUtils; import org.apache.xml.security.utils.Base64; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;总结:分段加密可以避免应为需要加密的原字符串过长而出现错误。
文章来源: RSA算法的分段加密