Soap协议通信,RSA256加密

一:RsaUtils,加密解密工具

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;


public class RsaUtils {

    /* byte数组转换成十六进制输出:"abcd" => "61626364" */
    public static String bytesToHexString(byte[] bArr) {
        StringBuffer sb = new StringBuffer(bArr.length);
        String sTmp;

        for (int i = 0; i < bArr.length; i++) {
            sTmp = Integer.toHexString(0xFF & bArr[i]);
            if (sTmp.length() < 2)
                sb.append(0);
            sb.append(sTmp.toUpperCase());
        }

        return sb.toString();
    }

    public static PublicKey getPublicKey(String publicKey) throws Exception {
        byte[] keyBytes = Base64Utils.decode(publicKey);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }

    public static byte[] encrypt(byte[] content, String publicKeyStr) throws Exception {
        PublicKey publicKey = getPublicKey(publicKeyStr);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");//javaĬ  "RSA"="RSA/ECB/PKCS1Padding"
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(content);
    }

}

二:对参数进行加密,appId,secret,publicKey,接口路径等参数让对接方提供

String appIdRsa = RsaUtils.bytesToHexString(RsaUtils.encrypt(appId.getBytes(), publicKey));
String secretRsa = RsaUtils.bytesToHexString(RsaUtils.encrypt(secret.getBytes(), publicKey));

三:拼接通信xml

String soapRequestData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" +
                    "   <soapenv:Header/>\n" +
                    "   <soapenv:Body>\n" +
                    "      <tem:GetToken>\n" +
                    "         <!--Optional:-->\n" +
                    "         <tem:appid>" + appIdRsa + "</tem:appid>\n" +
                    "         <!--Optional:-->\n" +
                    "         <tem:secret>" + secretRsa + "</tem:secret>\n" +
                    "      </tem:GetToken>\n" +
                    "   </soapenv:Body>\n" +
                    "</soapenv:Envelope>";

四:设置通信url,以及解析返回的xml

public static Map<String, Object> tokenSoapPostXml(String soapRequestData, String uploadDataIP) {

        Map<String, Object> resultMap = new HashMap<>();
        StringBuilder result = new StringBuilder();
        try {
            //SoapAction路径,如http://tempuri.org/IToken/GetToken
            String SOAPAction = UrlConfig.smartCityTokenSoapAction;
            //接口路径
            URL url = new URL(uploadDataIP + UrlConfig.smartCityConstructionPort + UrlConfig.smartCityTokenUrl);
            URLConnection connection = url.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection) connection;
            // how big it is so that we can set the HTTP Cotent-Length
            // property. (See complete e-mail below for more on this.)
            // byte[] b = bout.toByteArray();
            byte[] b = soapRequestData.getBytes("utf-8");
            // Set the appropriate HTTP parameters.
            httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            httpConn.setRequestProperty("SOAPAction", SOAPAction);
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);

            // Everything's set up; send the XML that was read in to b.
            OutputStream out = httpConn.getOutputStream();
            out.write(b);
            out.close();
            // Read the response and write it to standard out.
            InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
            BufferedReader in = new BufferedReader(isr);
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                result.append(inputLine);
            in.close();

        } catch (MalformedURLException e) {
            logger.error(e.toString());
        } catch (IOException e) {
            logger.error(e.toString());
        }

        System.out.println("结果值:" + result.toString());
        String deptXML = result.toString();
        try {
            Document document = XmlUtils.strXmlToDocument(deptXML);
            String response = XmlUtils.getValueByElementName(document, "GetTokenResult");
            response = response.replaceAll("\r", "");
            resultMap = GsonUtil.gsonToMaps(response);
            
        } catch (Exception e) {
            logger.error(e.toString());
        }

        return resultMap;
    }

五:第四步中xml解析工具XmlUtils,以及所需要的依赖


<dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1.3</version>
</dependency>


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XmlUtils {
    /**
     * 将字符串类型的XML 转化成Docunent文档结构
     *
     * @param parseStrXml 待转换的XML字符串
     * @return
     */
    public static Document strXmlToDocument(String parseStrXml) {
        StringReader read = new StringReader(parseStrXml);
        //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
        InputSource source = new InputSource(read);
        //创建一个新的SAXBuilder
        // 新建立构造器
        SAXBuilder sb = new SAXBuilder();
        Document doc = null;
        try {
            doc = sb.build(source);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;

    }

    /**
     * 根据目标节点名获取值
     *
     * @param doc           文档结构
     * @param finalNodeName 节点名
     * @return
     */
    public static String getValueByElementName(Document doc, String finalNodeName) {
        Element root = doc.getRootElement();
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> resultmap = getChildAllText(doc, root, map);
        String result = (String) resultmap.get(finalNodeName);
        return result;
    }

    /**
     * 递归获得子节点的值
     *
     * @param doc       文档结构
     * @param e         节点元素
     * @param resultmap 递归将值存入map中
     * @return
     */
    public static Map<String, Object> getChildAllText(Document doc, Element e, Map<String, Object> resultmap) {
        if (e != null) {
            //如果存在子节点
            if (e.getChildren() != null) {
                List<Element> list = e.getChildren();
                //循环输出
                for (Element el : list) {
                    //如果子节点还存在子节点,则递归获取
                    if (el.getChildren().size() > 0) {
                        getChildAllText(doc, el, resultmap);
                    } else {
                        //将叶子节点值压入map
                        resultmap.put(el.getName(), el.getTextTrim());
                    }
                }
            }
        }
        return resultmap;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python可以使用RSA算法实现RSA256加密解密。RSA加密算法是一种非对称加密算法,它使用公钥和私钥进行加密和解密操作。公钥用于加密消息,私钥用于解密消息。公钥是公开的,可以分享给其他人,而私钥应该保密,只有拥有私钥的人才能解密消息。 要在Python中实现RSA256加密解密,您可以使用第三方库,例如Cryptodome或cryptography。这些库提供了RSA加密算法的功能,可以轻松地实现RSA256加密解密。 首先,您需要生成一对RSA密钥,包括公钥和私钥。然后,您可以使用公钥对消息进行加密,使用私钥对加密的消息进行解密。请注意,RSA加密和解密的过程需要使用相应的密钥。 以下是一个简单的示例代码,演示了如何使用Cryptodome库实现RSA256加密解密: ```python from Cryptodome.PublicKey import RSA from Cryptodome.Cipher import PKCS1_OAEP # 生成RSA密钥对 key = RSA.generate(2048) private_key = key.export_key() public_key = key.publickey().export_key() # 加密和解密函数 def encrypt(message, public_key): cipher = PKCS1_OAEP.new(RSA.import_key(public_key)) encrypted_message = cipher.encrypt(message) return encrypted_message def decrypt(encrypted_message, private_key): cipher = PKCS1_OAEP.new(RSA.import_key(private_key)) decrypted_message = cipher.decrypt(encrypted_message) return decrypted_message # 示例用法 message = b"Hello, World!" encrypted_message = encrypt(message, public_key) decrypted_message = decrypt(encrypted_message, private_key) print("加密后的消息:", encrypted_message) print("解密后的消息:", decrypted_message) ``` 请注意,在使用这些库之前,您需要先安装它们。您可以使用pip命令来安装Cryptodome或cryptography库。 希望这个例子能够帮助您理解如何使用Python实现RSA256加密解密。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python 实现aes256 rsa 混合加解密](https://blog.csdn.net/weixin_73092756/article/details/130039225)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python RSA加密解密](https://blog.csdn.net/baidu_24752135/article/details/131316324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值