客户接口是使用的java实现,验证使用的md5withrsa,拆开看都理解,放到一块就懵逼了,求讲解!!!!
附java的实现方式:
<code>package main; import java.security.KeyFactory; import java.security.KeyPair; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.security.SecureRandom; public class main { /** * 生成秘钥 */ public static void makeKey() { String KEY_ALGORITHM = "rsa"; java.security.KeyPairGenerator keygen; try { keygen = java.security.KeyPairGenerator .getInstance(KEY_ALGORITHM ); SecureRandom secrand = new SecureRandom(); secrand.setSeed("tmriPayment".getBytes()); // 初始化随机产生器 keygen.initialize(1024); KeyPair keys = keygen.genKeyPair(); RSAPublicKey pubkey = (RSAPublicKey) keys.getPublic(); RSAPrivateKey prikey = (RSAPrivateKey) keys.getPrivate(); String pubKeyStr= Base64Util.encodeBase64(pubkey.getEncoded());//得到公钥 并做base64编码 String priKeyStr= Base64Util.encodeBase64(prikey.getEncoded());//得到私钥 并做base64编码 System.out.println("公钥:"+ pubKeyStr); System.out.println("私钥:"+ priKeyStr); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 加密 * @return */ public static String encodeData() { try { byte[] data = "fdfdfsdfdfd".getBytes(); String privateKey = "MIICXAIBAAKBgQDOF3dzQpPVIvHrx2qwszcv9Cw5XhCEyuRTy+HDPD684NSGGGnL"+ "34cLc1BCEjlT3v9H81SigvSUDB++YbMcZOrRFT+MAt22yi98BcRP60vgVixfTCCI"+ "lc39b6G6I6ac5BUfOKwdUKNUnCLvcojshmtGezcEYGocMziOIwXIdBXErQIDAQAB"+ "AoGANlycRovuQM9J7v6YFun/Cagnri4wv0ZhefUSpRQUHHBVvtVbuspIbe3J4tO5"+ "yXTN86Ws0n0mlJKqIObWfwvjoDADNeEWFXUI5YKyEnpnvvZwTo1JjrDy8QJpGe94"+ "yNebfGLKyZtIc/zLq9sFboyqNCtn2hu7IsG2g4SGe9BFcsUCQQDnrc6yXG6jmFC/"+ "7HFqXj5NtQc72vYbIjf32yG8W1D1j3ghZZwtUkIa8g3WMxFrYW9DFMzTDi3/mPSd"+ "1A9OG89DAkEA47oFZj6xf7gzDbHDdKX/S9ehjQjP2q6V7SYXnhdEwioxjJpj9qrP"+ "SKQg8A9m4nUl2FXJ2spxApotHoKHqTGFTwJBAIkcohGJBqmnQVL0mgK7l9/hXVCd"+ "O72/OKRlecfBu1449H2/ZvijkB/mVS4Jtyt31KM8siPOZoa4fTzS/ePaLYUCQDHj"+ "LY2hjFbzblPArpXeS5g8y8pOtOIuPu/t2Vyrskdq4OHxbJa8Ap7iPcj5RsuzaDAF"+ "UywYDzvHtLyrUbbR/2cCQGbbIWDLPtzHzI7QumtabHq9M5zUgvtXDksnOltcEDSX"+ "aBoGSbpRcPcGFfFMY+fd/kdmEambYn+270DNHPvqljU="; byte[] keyBytes; keyBytes = Base64Util.decodeBase64(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(priKey); signature.update(data);//data为要生成签名的源数据字节数组 return Base64Util.encodeBase64(Base64Util.encodeBase64(signature.sign()).getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }//对私钥做base64解码 return ""; } /** * 解密 * @return */ public static Boolean decodeData(String msg) { String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMrfAeCg5zyHJKs269vMywoMYwKWGfBbk31K44g0ZLg8YN4c9jOH4hyAYkhN5mWY69Q8Sb82OAlxBXMY1zQshKk6kzZGOrYDQOt/bvVg6kPdf9IgtWky4YaCVSR2C0zYCviz+pHXJrbTeyVVLmbaxsr2lEjamBoDcfM1uE9hj83QIDAQAB"; try { byte[] data = "fdfdfsdfdfd".getBytes(); byte[] publicKey1 = Base64Util.decodeBase64(publicKey);//对提供的公钥做base64解码 byte[] sign = Base64Util.decodeBase64( new String(Base64Util.decodeBase64(msg)));//签名需要做二次base64解码 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey1); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey pubKey = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initVerify(pubKey); signature.update(data); return signature.verify(sign); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }//对私钥做base64解码 return false; } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //makeKey(); String msg = encodeData(); //System.out.println(msg); //String msg = "ZlBGVnVXQm1YVjhhWDhGbVVYV24raEJzRVB1WFAycUhKdVIwbGl3azY5T2VKWHBkMDlpL1U5aGYwcEVvK1h0M0RNMFNsRTNQM1pYazFyT25xblZwTStRZmJhVGVPUXBsZ0ozZVFKemFnaWt5dzNvUXRNZC81RGQ4RGlSR3pqUmRhOFd2OCt1ekRiakZBdWtqeGg3Y1plTEdTT2ppQ08rcFVqOUQxZXh6ZG1VPQ=="; //Boolean data = decodeData(msg); //System.out.println(data); } } </code>
回复内容:
客户接口是使用的java实现,验证使用的md5withrsa,拆开看都理解,放到一块就懵逼了,求讲解!!!!
附java的实现方式:
<code>package main; import java.security.KeyFactory; import java.security.KeyPair; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.security.SecureRandom; public class main { /** * 生成秘钥 */ public static void makeKey() { String KEY_ALGORITHM = "rsa"; java.security.KeyPairGenerator keygen; try { keygen = java.security.KeyPairGenerator .getInstance(KEY_ALGORITHM ); SecureRandom secrand = new SecureRandom(); secrand.setSeed("tmriPayment".getBytes()); // 初始化随机产生器 keygen.initialize(1024); KeyPair keys = keygen.genKeyPair(); RSAPublicKey pubkey = (RSAPublicKey) keys.getPublic(); RSAPrivateKey prikey = (RSAPrivateKey) keys.getPrivate(); String pubKeyStr= Base64Util.encodeBase64(pubkey.getEncoded());//得到公钥 并做base64编码 String priKeyStr= Base64Util.encodeBase64(prikey.getEncoded());//得到私钥 并做base64编码 System.out.println("公钥:"+ pubKeyStr); System.out.println("私钥:"+ priKeyStr); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 加密 * @return */ public static String encodeData() { try { byte[] data = "fdfdfsdfdfd".getBytes(); String privateKey = "MIICXAIBAAKBgQDOF3dzQpPVIvHrx2qwszcv9Cw5XhCEyuRTy+HDPD684NSGGGnL"+ "34cLc1BCEjlT3v9H81SigvSUDB++YbMcZOrRFT+MAt22yi98BcRP60vgVixfTCCI"+ "lc39b6G6I6ac5BUfOKwdUKNUnCLvcojshmtGezcEYGocMziOIwXIdBXErQIDAQAB"+ "AoGANlycRovuQM9J7v6YFun/Cagnri4wv0ZhefUSpRQUHHBVvtVbuspIbe3J4tO5"+ "yXTN86Ws0n0mlJKqIObWfwvjoDADNeEWFXUI5YKyEnpnvvZwTo1JjrDy8QJpGe94"+ "yNebfGLKyZtIc/zLq9sFboyqNCtn2hu7IsG2g4SGe9BFcsUCQQDnrc6yXG6jmFC/"+ "7HFqXj5NtQc72vYbIjf32yG8W1D1j3ghZZwtUkIa8g3WMxFrYW9DFMzTDi3/mPSd"+ "1A9OG89DAkEA47oFZj6xf7gzDbHDdKX/S9ehjQjP2q6V7SYXnhdEwioxjJpj9qrP"+ "SKQg8A9m4nUl2FXJ2spxApotHoKHqTGFTwJBAIkcohGJBqmnQVL0mgK7l9/hXVCd"+ "O72/OKRlecfBu1449H2/ZvijkB/mVS4Jtyt31KM8siPOZoa4fTzS/ePaLYUCQDHj"+ "LY2hjFbzblPArpXeS5g8y8pOtOIuPu/t2Vyrskdq4OHxbJa8Ap7iPcj5RsuzaDAF"+ "UywYDzvHtLyrUbbR/2cCQGbbIWDLPtzHzI7QumtabHq9M5zUgvtXDksnOltcEDSX"+ "aBoGSbpRcPcGFfFMY+fd/kdmEambYn+270DNHPvqljU="; byte[] keyBytes; keyBytes = Base64Util.decodeBase64(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(priKey); signature.update(data);//data为要生成签名的源数据字节数组 return Base64Util.encodeBase64(Base64Util.encodeBase64(signature.sign()).getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }//对私钥做base64解码 return ""; } /** * 解密 * @return */ public static Boolean decodeData(String msg) { String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMrfAeCg5zyHJKs269vMywoMYwKWGfBbk31K44g0ZLg8YN4c9jOH4hyAYkhN5mWY69Q8Sb82OAlxBXMY1zQshKk6kzZGOrYDQOt/bvVg6kPdf9IgtWky4YaCVSR2C0zYCviz+pHXJrbTeyVVLmbaxsr2lEjamBoDcfM1uE9hj83QIDAQAB"; try { byte[] data = "fdfdfsdfdfd".getBytes(); byte[] publicKey1 = Base64Util.decodeBase64(publicKey);//对提供的公钥做base64解码 byte[] sign = Base64Util.decodeBase64( new String(Base64Util.decodeBase64(msg)));//签名需要做二次base64解码 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey1); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey pubKey = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initVerify(pubKey); signature.update(data); return signature.verify(sign); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }//对私钥做base64解码 return false; } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //makeKey(); String msg = encodeData(); //System.out.println(msg); //String msg = "ZlBGVnVXQm1YVjhhWDhGbVVYV24raEJzRVB1WFAycUhKdVIwbGl3azY5T2VKWHBkMDlpL1U5aGYwcEVvK1h0M0RNMFNsRTNQM1pYazFyT25xblZwTStRZmJhVGVPUXBsZ0ozZVFKemFnaWt5dzNvUXRNZC81RGQ4RGlSR3pqUmRhOFd2OCt1ekRiakZBdWtqeGg3Y1plTEdTT2ppQ08rcFVqOUQxZXh6ZG1VPQ=="; //Boolean data = decodeData(msg); //System.out.println(data); } } </code>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
