支付宝验签的java代码 支付宝验签的java代码在哪里
求ECDSA的Java代码
【方案1】
在成都做网站、网站设计、外贸营销网站建设过程中,需要针对客户的行业特点、产品特性、目标受众和市场情况进行定位分析,以确定网站的风格、色彩、版式、交互等方面的设计方向。成都创新互联公司还需要根据客户的需求进行功能模块的开发和设计,包括内容管理、前台展示、用户权限管理、数据统计和安全保护等功能。
package ECDSA;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class Ecdsa {
private static String src = "hello berber" ;
public static void main(String []args){
jdkECDSA();
}
public static void jdkECDSA(){
// 1.初始化密钥
try{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256);
KeyPair keyPair = keyPairGenerator.generateKeyPair() ;
ECPublicKey ecPublicKey = (ECPublicKey)keyPair.getPublic() ;
ECPrivateKey ecPrivateKey = (ECPrivateKey)keyPair.getPrivate() ;
// 执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("EC") ;
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec) ;
Signature signature = Signature.getInstance("SHA1withECDSA");
signature.initSign(privateKey);
signature.update(src.getBytes());
byte []arr = signature.sign();
System.out.println("jdk ecdsa sign :"+ HexBin.encode(arr));
// 验证签名
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("EC");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
signature = Signature.getInstance("SHA1withECDSA");
signature.initVerify(publicKey);
signature.update(src.getBytes());
boolean bool = signature.verify(arr);
System.out.println("jdk ecdsa verify:"+bool);
}catch(Exception e){
}
}
}
Java数字签名——ECDSA算法
【方案2】
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
new MyTest().getSign();
}
void getSign() {
// Get the instance of the Key Generator with "EC" algorithm
try {
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
g.initialize(kpgparams);
KeyPair pair = g.generateKeyPair();
// Instance of signature class with SHA256withECDSA algorithm
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(pair.getPrivate());
System.out.println("Private Keys is::" + pair.getPrivate());
System.out.println("Public Keys is::" + pair.getPublic());
String msg = "text ecdsa with sha256";//getSHA256(msg)
ecdsaSign.update((msg + pair.getPrivate().toString())
.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
System.out.println("Signature is::"
+ new BigInteger(1, signature).toString(16));
// Validation
ecdsaSign.initVerify(pair.getPublic());
ecdsaSign.update(signature);
if (ecdsaSign.verify(signature))
System.out.println("valid");
else
System.out.println("invalid!!!!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}
java – 使用secp256r1曲线和SHA256算法生
怎么验证生成的Ecdsa签名是正确的呢,可以看下这篇文章:RSA,ECC,Ecdsa,国密SM2的签名,验签,加密
我用java怎么去实现支付宝接口,没有思路,求大神帮忙,谢谢,
在下面找java demo
一般都是配置几个信息
按照支付方式不同 (担保交易啥的 我忘了 3个好像)
有不不同的传参,看看就行
代码里可以用httpClient做get、post请求
Java如何生成支付宝RSA2签名
对支付宝进行设置再回主页面进行转义,具体步骤如下。
支付宝APP支付(Java后台生成签名具体步骤)
/**
*支付宝支付
* @param orderId 订单编号
* @param actualPay 实际支付金额
* @return
*/
private String getOrderInfoByAliPay(String orderId,float actualPay) {
//回调页面
String ali_call_back_url = propertiesService.ALI_CALL_BACK_URL;
String seller_id = propertiesService.SELLER_ID;//商户编号
String[] parameters={
"service=\"mobile.securitypay.pay\"",//固定值(手机快捷支付)
"partner=\"2088421544444\"",//合作身份者ID(16位)
"_input_charset=\"utf-8\"",
"notify_url=\""+ali_call_back_url+"\"",//通知地址
"out_trade_no=\""+orderId+"\"",//商户内部订单号
"subject=\"测试\"",//测试
"payment_type=\"1\"",//固定值
"seller_id=\""+seller_id+"\"",//账户邮箱
"total_fee=\""+"0.01"+"\"",//支付金额(元)
"body=\"订单说明\"",//订单说明
"it_b_pay=\"30m\""(订单过期时间 30分钟过期无效)
};
String signOrderUrl = signAllString(parameters);
return signOrderUrl;
}
/**
* 支付宝签名
* @param array
* @return
*/
private String signAllString(String [] array){
StringBuffer sb = new StringBuffer("");
for (int i = 0; i array.length; i++) {
if(i==(array.length-1)){
sb.append(array[i]);
}else{
sb.append(array[i]+"");
}
}
System.out.println(sb.toString());
String sign = "";
try {
sign = URLEncoder.encode(RSA.sign(sb.toString(), AlipayConfig.private_key, "utf-8"), "utf-8");//private_key私钥
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
sb.append("sign=\""+sign+"\"");
sb.append("sign_type=\"RSA\"");
return sb.toString();
}
package com.alipay.sign;
import javax.crypto.Cipher;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSA{
public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
/**
* RSA签名
* @param content 待签名数据
* @param privateKey 商户私钥
* @param input_charset 编码格式
* @return 签名值
*/
public static String sign(String content, String privateKey, String input_charset)
{
try
{
byte[] decode = Base64.decode(privateKey);
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(decode );
KeyFactory keyf= KeyFactory.getInstance("RSA");
PrivateKey priKey= keyf.generatePrivate(priPKCS8);
java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
signature.initSign(priKey);
signature.update( content.getBytes(input_charset) );
byte[] signed = signature.sign();
return Base64.encode(signed);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* RSA验签名检查
* @param content 待签名数据
* @param sign 签名值
* @param ali_public_key 支付宝公钥
* @param input_charset 编码格式
* @return 布尔值
*/
public static boolean verify(String content, String sign, String ali_public_key, String input_charset)
{
try
{
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = Base64.decode(ali_public_key);
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
java.security.Signature signature = java.security.Signature
.getInstance(SIGN_ALGORITHMS);
signature.initVerify(pubKey);
signature.update( content.getBytes(input_charset) );
boolean bverify = signature.verify( Base64.decode(sign) );
return bverify;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
/**
* 解密
* @param content 密文
* @param private_key 商户私钥
* @param input_charset 编码格式
* @return 解密后的字符串
*/
public static String decrypt(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.decode(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);
}
/**
* 得到私钥
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = Base64.decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
}
java 支付宝支付接口 的 NotifyUrl 应该怎么返回success 官网上说打印输出 我用了println, 后来用return
public String alibaPayCallBackNotice(HttpServletRequest request, HttpServletResponse response, String outTradeNo) {
//获取支付宝POST过来反馈信息
MapString,String params = new HashMapString,String();
Map?, ? requestParams = request.getParameterMap();
for (Iterator? iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
//乱码解决,这段代码在出现乱码时使用。
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
//boolean AlipaySignature.rsaCheckV1(MapString, String params, String publicKey, String charset, String sign_type)
try {
boolean flag = AlipaySignature.rsaCheckV1(params, App.AliBaPay.ALIPAY_PUBLIC_KEY, "UTF-8","RSA2");
if (flag) {
if ("TRADE_SUCCESS".equals(params.get("trade_status")) || "TRADE_FINISHED".equals(params.get("trade_status"))) {
// 支付成功,录入订单数据
if (userMoneyService.userUpdateWaterAccountInfo(Integer.valueOf(outTradeNo))) {
return "success";
}
}
}
} catch (Exception e) {
}
return "failure";
}
整个接口返回的数据,处理为字符串返回即可,不需要跳转,也不能跳转,要在你的resposne中body数据
记得采纳
网站标题:支付宝验签的java代码 支付宝验签的java代码在哪里
分享网址:http://hbruida.cn/article/doeiiis.html