天天看點

微信支付---前景提要(标準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("出錯了");
        }
    }

}


           

繼續閱讀