微信支付转银行卡功能开发 Java 解析 PKCS#1

1.获取perm 文件

接口 加密记得追加商户&key

使用接口文档 即可。

https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_7


2。一定要先使用下面的转pkcs8,不然坑死老夫

PKCS#1 转 PKCS#8:
openssl rsa -RSAPublicKey_in -in <filename> -pubout

3. 拿到了PKCS#8
基本解决了加密问题。
参考: http://blog.csdn.net/chaiqunxing51/article/details/52116433

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// PrivateKey
// 切记先到Linx 下转成 PKCS8
PublicKey pub = getPubKey("H:\\workspace\\hmisbackend_hgys\\public.key", "RSA");
System.out.println("get key");
String str = "pptwc";
byte[] estr = encrypt(str.getBytes(), pub, "RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING");
String cardNo=encodeBase64(estr);
System.out.println(cardNo);

}



public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, String cipherAlgorithm) throws Exception {
ByteArrayOutputStream outbuf = null;
try {
Cipher cipher = Cipher.getInstance(cipherAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] encryptedBlock = cipher.doFinal(plainBytes);
return encryptedBlock;
} catch (Exception e) {
throw new Exception("ENCRYPT ERROR:", e);
} finally {
try {
if (outbuf != null) {
outbuf.close();
}
} catch (Exception e) {
outbuf = null;
throw new Exception("CLOSE ByteArrayOutputStream ERROR:", e);
}
}
}



public static PublicKey getPubKey(String publicKeyPath, String keyAlgorithm) {
PublicKey publicKey = null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(publicKeyPath);
publicKey = getPublicKey(inputStream, keyAlgorithm);
} catch (Exception e) {
System.out.println("加载公钥出错!");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
System.out.println("加载公钥,关闭流时出错!");
}
}
}
return publicKey;
}

public static PublicKey getPublicKey(InputStream inputStream, String keyAlgorithm) throws Exception {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String readLine = null;
while ((readLine = br.readLine()) != null) {
if (readLine.charAt(0) == '-') {
continue;
} else {
sb.append(readLine);
sb.append('\r');
}
}
X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(decodeBase64(sb.toString()));
KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
PublicKey publicKey = keyFactory.generatePublic(pubX509);

return publicKey;
} catch (Exception e) {
throw new Exception("READ PUBLIC KEY ERROR:", e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
inputStream = null;
throw new Exception("INPUT STREAM CLOSE ERROR:", e);
}
}
}

// 一下面是base64的编码和解码
public static String encodeBase64(byte[] input) throws Exception {
Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
Method mainMethod = clazz.getMethod("encode", byte[].class);
mainMethod.setAccessible(true);
Object retObj = mainMethod.invoke(null, new Object[] { input });
return (String) retObj;
}

/***
* decode by Base64
*/
public static byte[] decodeBase64(String input) throws Exception {
Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
Method mainMethod = clazz.getMethod("decode", String.class);
mainMethod.setAccessible(true);
Object retObj = mainMethod.invoke(null, input);
return (byte[]) retObj;
}

猜你喜欢

转载自a545807638.iteye.com/blog/2400878