Maison >développement back-end >tutoriel php >node.js - 求Md5withRsa php 、python、或者nodejs的实现,或者给讲讲原理?
客户接口是使用的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>