微信小程序rsa分段加密

前段时间公司要开发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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专治八阿哥的孟老师

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值