Table of Contents
回复内容:
Home Backend Development PHP Tutorial node.js - 求Md5withRsa php 、python、或者nodejs的实现,或者给讲讲原理?

node.js - 求Md5withRsa php 、python、或者nodejs的实现,或者给讲讲原理?

Jun 06, 2016 pm 08:09 PM
node.js php python

客户接口是使用的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>
Copy after login
Copy after login

回复内容:

客户接口是使用的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>
Copy after login
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Exploring Performance and Efficiency Python vs. C : Exploring Performance and Efficiency Apr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles