`

微信支付转银行卡功能开发 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;
}

分享到:
评论

相关推荐

    企业微信支付到个人银行卡(PKCS#1格式的公钥转为PKCS#8格式)

    网上有例子,自己遇到了坑(公钥的格式不对导致一直报公钥非法错误,里面有详细解决过程)

    PKCS#1 java 语言实现参考 V1.0

    基于BigInteger类用java封装的PKCS#1 v2.1 全算法实现,模块与规范一一对应。包含 I2OSP OS2IP RSAEP RSADP RSASP1 RSAVP1 RSAES-OAEP RSAES-PKCS1_v1_5 RSASSA-PSS RSASSA-PKCS1-v1_5 以及 MGF SourceAlgrithm等...

    按PKCS#10数据格式封装PKCS#10数据

    按照ASN.1语法,解析证书请求数据包,再按PKCS#10格式封装证书请求数据。

    使用数字证书进行PKCS#7数字签名

    越来越多的应用需要我们使用USB接口数字证书进行PKCS#7数字签名。本文分别介绍了使用微软CryptoAPI方式和OpenSSL Engine方式进行数字签名。特别地,提出了OpenSSL Engine简化方式,这种方式更为灵活方便易行。

    PKCS#7格式加解密验签

    提到PKCS,首先想到的是RSA的证书格式(类似,PKCS8,P12等),最近在和一家俄罗斯支付对接时,对方说加密成PKCS#7格式,当时就懵了,以为对方说错了,在这块卡了好几天,通过查资料,终于找到方法了。确实是PKCS#7...

    Java RSA私钥 pkcs1转pkcs8;

    Java RSA私钥 pkcs1转pkcs8;适用于Java与C#程序交互时使用

    C# SHA256 PKCS#7 生成验名、验签源码 中行支付.rar

    C# SHA256 PKCS#7 生成验名、验签源码 中行支付.

    pkcs#11标准中文文档

    关于pkcs#11标准库接口的中文文档,很详细,对于初学者要使用pkcs#11标准接口来说,非常有用。

    pkcs-11v2-20d3.rar_PKCS#1_PKCS#11 USBKey_RSA PKCS_pkcs_usbkey

    在密码系统中,PKCS#11是公钥加密标准(PKCS, Public-Key Cryptography Standards)中的一份子 ,由RSA实验室(RSA Laboratories)发布[1],它为加密令牌定义了一组平台无关的API ,如硬件安全模块和智能卡。...

    PKCS#11的部分使用例码

    PKCS#11的部分使用例码 PKCS#11的部分使用例码

    PKCS #1: RSA Cryptography Specifications Version 2.2

    PKCS #1: RSA Cryptography Specifications Version 2.2 Table of Contents 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1. Requirements Language . . . . . . . . . . . . . . . . . ...

    pkcs#1_V2.1.zip

    PKCS#1 V2.1版本 定义RSA公开密钥算法加密和签名机制,主要用于组织PKCS#7中所描述的数字签名和数字信封

    wxpaybank微信转银行卡.zip

    网上的案例 把公钥的PKCS#1 转PKCS#8 错误

    PKCS #1 v2.2 RSA Cryptography Standard.pdf

    RSA规范标准

    PKCS (公钥密码标准)

    • PKCS #1: RSA Cryptography Standard RSA密码标准 • PKCS #7: Cryptographic Message Syntax Standard密文信息语法标准 • PKCS #10: Certification Request Syntax Standard认证请求语法标准 • PKCS #11: ...

    pkcs#1规范

    RSA加密标准。PKCS#1定义了RSA公钥函数的基本格式标准,特别是数字签名。它定义了数字签名如何计算,包括待签名数据和签名本身的格式;它也定义了RSA公/私钥的语法。

    PKCS#11 v2.2(DOC)

    本标准为那些保存密码信息,执行密码... 它遵循一种基于对象的简单方法,提出技术独立性(各种各样的设备)和资源共享(多个应用程序访问多个设备)的目标,把设备的一种通用的逻辑视图,即密码令牌,提供给应用程序。

    C# pkcs # 7 签名 验签

    C# 语言,.net平台下实现pkcs # 7 签名 验签,C# 语言,.net平台下实现pkcs # 7 签名 验签

    pkcs#7标准格式

    文档介绍了pkcs#7的格式和结构,里面虽然是英文的,但是很详细的,能看到,另外用ASN1工具和这个结合能很快的理解pkcs#7的结构。

Global site tag (gtag.js) - Google Analytics