來源:互聯網
上載者:User
關鍵字
md5withrsa
php
python
node.js
客戶介面是使用的java實現,驗證使用的md5withrsa,拆開看都理解,放到一塊就懵逼了,求講解!!!!
附java的實現方式:
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); }}
回複內容:
客戶介面是使用的java實現,驗證使用的md5withrsa,拆開看都理解,放到一塊就懵逼了,求講解!!!!
附java的實現方式:
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); }}