package com.sankuai.meituan.waimai.common; import org.apache.commons.lang.ArrayUtils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import sun.security.rsa.RSAPrivateKeyImpl; import sun.security.rsa.RSAPublicKeyImpl; import sun.security.util.DerValue; import javax.crypto.Cipher; import java.io.ByteArrayOutputStream; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.HashMap; import java.util.Map; /** * Date: 14-9-18 * Time: 下午4:20 */ public class RSAHelper { public static final int ENCODE_MAX = 117; public static final int DECODE_MAX = 128; public static String decode(String encBase64String,String privateKeyString) throws Exception { BASE64Decoder base64Decoder = new BASE64Decoder(); byte[] desEncodeRead = base64Decoder.decodeBuffer(privateKeyString);// DerValue d = new DerValue(desEncodeRead); RSAPrivateKey privateKey= (RSAPrivateKey) RSAPrivateKeyImpl.parseKey(d); Cipher cipher =Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] src = base64Decoder.decodeBuffer(encBase64String); ByteArrayOutputStream out = new ByteArrayOutputStream(); for(int i=0; i<src.length; i+= DECODE_MAX){ byte[] toDecodeSegment = ArrayUtils.subarray(src, i,i+DECODE_MAX); byte[] destByte = cipher.doFinal(toDecodeSegment); System.out.println("decode"+destByte.length); out.write(destByte); } byte[] decode = out.toByteArray(); return new String(decode, "UTF-8"); } public static String encode(String src, String publicKey) throws Exception { BASE64Decoder base64Decoder = new BASE64Decoder(); BASE64Encoder base64Encoder = new BASE64Encoder(); byte[] desEncodeRead = base64Decoder.decodeBuffer(publicKey);// DerValue d = new DerValue(desEncodeRead); RSAPublicKey p = (RSAPublicKey) RSAPublicKeyImpl.parse(d); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, p); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] toEncode = src.getBytes(); for(int i=0; i<toEncode.length; i+= ENCODE_MAX){ byte[] toEncodeSegment = ArrayUtils.subarray(toEncode, i,i+ENCODE_MAX); byte[] ecodeSegemnt = cipher.doFinal(toEncodeSegment); System.out.println("adfdsSS"+ecodeSegemnt.length); out.write(ecodeSegemnt); } byte[] encode = out.toByteArray(); return base64Encoder.encode(encode); } /** * return map siz KEY为public key value为private key BASE64串 */ public static Map generateRSAKey() throws NoSuchAlgorithmException { BASE64Encoder base64Encoder = new BASE64Encoder(); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); // Generate keys RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); byte[] privateEncode = privateKey.getEncoded(); byte[] publicEncode = publicKey.getEncoded(); String privateKeyBase64 = base64Encoder.encode(privateEncode); String publicKeyBase64 = base64Encoder.encode(publicEncode); Map retValue = new HashMap(1); retValue.put(publicKeyBase64, privateKeyBase64); return retValue; } }
RSA/ECB/PKCS1Padding 算法,加密字节最大117,但不论设置到大,加密后的字节数组大小为128, 解密必须按128个字节一组进行,否则报错,128个字节一组实际上也保证了分段加加密是可逆的,分段 解密后能得到加密前的字节数组
相关推荐
主要介绍了C# RSA分段加解密实现方法,结合具体实例形式分析了C# RSA加密解密的原理与具体实现技巧,需要的朋友可以参考下
采用Java Swing技术实现的一个小工具类 请在jre环境下使用
jsencrypt.min.js通过JSEncrypt分段加密解密,自己添加了分段加解密的方法
PHP RSA分段加密解密
RSA非对称加密,前端加解密算法代码。公钥明文,私钥各自保存,公钥加密,私钥解密。 RSA分段加密 适用于加密长度超标,可用于图片base64等大长度内容加密。
rsa进行加密和解密的长度是有限制的,那怎么解决这个问题呢?我们可以通过分段加密的方法解决这个问题
使用RSA非对称加密完成JavaScript前端RSA加密和分段加解密,最近研究了RSA非对称加密,关于什么是RSA,网上各种文章一搜一大把,但是关于如何使用RSA完成前端的组合加密解密,东西就非常少了,并且由于RSA的特性,一...
IOS RSA加密 分段解密
使用RSA非对称加密完成Java后端RSA加密和分段加解密,最近研究了RSA非对称加密,关于什么是RSA,网上各种文章一搜一大把,由于RSA的特性,一个1024位的密钥只能加密117位字节数据,当数据量超过117位字节的时候,程序...
PHP的RSA加密解密类,支持RSA分段解密,可用于解密APP发送的较长的字符串。 使用方法: Rsa::privDecrypt($encryptedString); //RSA私钥解密 Rsa::privEncrypt($string); //RSA私钥加密 Rsa::publicDecrypt($...
rsa分段加密,附件是java和php的例子。附件中的证收是用openssl生成的测试证书。
对请求和响应内容进行RSA分段加密,开箱就用无后作用。
C#做的RSA加密解密, 带winform界面, 测试通过, 代码完整。
rsa加解密
代码描述了如何使用RSA算法进行非对称加密, 一对密钥由公钥和私钥组成(可以使用很多对密钥)。私钥可以解密公钥加密的数据,公钥可以解密私钥加密的数据(私钥公钥可以互相加密解密)
Java 生成RSA密钥进行数据加密解密 支持超长字符分区 ,同时支持本地读取生成的密钥文件和远程服务器上存放的秘钥文件
【delphiRSA加解密】支持RSA公钥加密私钥解密,RSA私钥加密公钥解密,秘钥格式支持PKCS8和PKCS1,D7~XE10可用
一个SOCKET服务端与客户端的通信,另加了RSA分段加密技术
iOS RSA加密与解密Demo:https://github.com/fuaiyi/RSAEncryption博客:http://www.jianshu.com/u/b1d7ade703b4