微信支付---前景提要(标准RSA算法说明)

获取公钥(apiclient.keystore)详情

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

import static java.util.Base64.*;

public class RSAEncrypt {

    /**
     * 公钥存放地 通过微信接口获取
     *
     */
    private static final String KEYSTORE = "/keystore/apiclient.keystore";

    /**
     * @param value
     * @return
     */
    public static String encode(String value) throws Exception {
        String key = loadPublicKeyStr();
        RSAPublicKey publicKey = loadPublicKeyByStr(key);
        byte[] encrypted = encrypt(publicKey, value.getBytes());
        return getEncoder().encodeToString(encrypted);
    }

    /**
     * @return
     * @throws Exception
     */
    private static String loadPublicKeyStr() throws Exception {
        try {
            InputStream is = RSAEncrypt.class.getResourceAsStream(KEYSTORE);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            return sb.toString();
        } catch (IOException e) {
            throw new Exception("公钥数据流读取错误");
        } catch (NullPointerException e) {
            throw new Exception("公钥输入流为空");
        }
    }

    /**
     * @param publicKeyStr
     * @return
     * @throws Exception
     */
    private static RSAPublicKey loadPublicKeyByStr(String publicKeyStr)
            throws Exception {
        try {
            Decoder decoder = getDecoder();
            byte[] buffer = decoder.decode(publicKeyStr);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            return (RSAPublicKey) keyFactory.generatePublic(keySpec);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | NullPointerException e) {
            throw new Exception("出错了");
        }
    }

    /**
     * @param publicKey
     * @param plainTextData
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData)
            throws Exception {
        if (publicKey == null) {
            throw new Exception("");
        }

        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] output = cipher.doFinal(plainTextData);
            return output;
        } catch (NoSuchAlgorithmException e) {
            throw new Exception("出错了");
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            return null;
        } catch (InvalidKeyException e) {
            throw new Exception("出错了");
        } catch (IllegalBlockSizeException e) {
            throw new Exception("出错了");
        } catch (BadPaddingException e) {
            throw new Exception("出错了");
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值