前段时间公司要开发app配套的微信小程序,而app的接口是使用rsa加密的。
找了很多资料,发现rsa的秘钥长度限制了加密时明文的长度。又不能重新写一套接口。纠结了三天,终于集合各方资料弄出了一个微信小程序试用的rsa分段加密程序.
详见rsa.js
demo下载地址http://download.csdn.net/detail/u011731544/9908595
里面有好几种加密
这个是导出的签名方法,注释的两行是不分段的。不过我测试过,分段的encryptLong也可以兼容不分段的encrypt。
function sign(text) {
//var encStr = encrypt_rsa.encrypt(text);//不分段的加密
//encStr = hex2b64(encStr);//不分段加密需要base64一下
var encStr = encrypt_rsa.encryptLong(text);//分段加密
console.log(text)
console.log("加密结果:" + encStr)
return encStr;
}
配套的java端解密的代码如下:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
/**
* RSA算法
*
*/
public class RSA {
/**
* SIGN_ALGORITHMS
*/
public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
/**
* 公钥加密
*
* @param content
* @param public_key
* @return
* @throws Exception
*/
public static String signWithPublicKey(String content, String public_key) throws Exception {
byte[] buffer = Base64.decodeBase64(public_key);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(1, publicKey);
byte[] data = content.getBytes("utf-8");
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
int key_len = publicKey.getModulus().bitLength() / 8 - 11;
for (int i = 0; inputLen - offSet > 0; offSet = i * key_len) {
byte[] cache;
if (inputLen - offSet > key_len) {
cache = cipher.doFinal(data, offSet, key_len);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
++i;
}
byte[] encryptedData = out.toByteArray();
out.close();
return new String(Base64.encodeBase64(encryptedData));
}
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = Base64.decodeBase64(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 私钥解密
*
* @param content
* @param private_key
* @param input_charset
* @return
* @throws Exception
*/
public static String decryptByPrivateKey(String content, String private_key, String input_charset)
throws Exception {
PrivateKey prikey = getPrivateKey(private_key);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, prikey);
InputStream ins = new ByteArrayInputStream(Base64.decodeBase64(content));
ByteArrayOutputStream writer = new ByteArrayOutputStream();
// rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密
byte[] buf = new byte[128];
int bufl;
while ((bufl = ins.read(buf)) != -1) {
byte[] block = null;
if (buf.length == bufl) {
block = buf;
} else {
block = new byte[bufl];
for (int i = 0; i < bufl; i++) {
block[i] = buf[i];
}
}
writer.write(cipher.doFinal(block));
}
return new String(writer.toByteArray(), input_charset);
}
}
参考资料http://download.csdn.net/detail/yubin2009m/9862378
http://blog.csdn.net/ufo00001/article/details/72822907