java 代码
package com.example.trip_vip;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import cn.hutool.core.codec.Base64;
public class MakeSign {
private static String encodeName = "utf-8";
public static void main(String[] args) {
try {
String accountId = "4965E5E2530767C1";
String signKey = "E0A73F3A5D78D198BF2FB57879CF9A79";
String aesKey = "D2DN340LGYSsBrP6";
String aesIv = "5693259981590433";
String requestJson = "{\"body\":\"ndgoebijlofhjbbghfldcjmdlbhfkbehoofoeafhljpjnceacdjnnahfhkjibplmclafjgdkpknpfpfammfcbcnoabjigcoglbdgddiljnaecfehccnhedpombockfajjihkkjmlnkabjegaklljfodpbiaammiemioajpehdlnlipflhnijkngbilhfghbcimejcnnkmdmbcfndblhcbmncighpnolnlkgncmbjbfpphehoocdkchgfbbafjickhhfellheekdjofbjfedglbiidjmmebfffkpbjindepidcjbmoebpeoeekcjhcmcgadcalchhnbcnkdbockljkbfejfiddpekebhnbkajmjihjhdfdnakohpgeejijlfbjciakjdcfeclbiellgjalajphblkoleiimcjkmajankcldmnnhofgopkefpdplbjabghbiapgomfpnkphdlobcojapaappchbacljconafemcdiplahdkgogidnkkkpdchmlfhlkemccdennbfkandhhlhalhflfhodcmfjcnjkaelnoainmaakckekmjonbmmbhbdaiicljpibjjdcddggepaibniibifipehjllgpajilagikmgnnopaiekbbhjcbhcologopheelcbjfhondfllknbfhpbipjccdpeemjciphdcdpghhmimogilfcbfgogaohpdanoileghhmpmglnelfpafenpdcepbeimncjlolgahhljhgodjabcologlfgmikgdhmmgcinpeebaifgjpmemdmojhfhikhgjjjnnhokaniagmjohgmpfepgekllajalldgfagoijlihklbhcihdbgdnaomikfkcjgjpohdcjdlfpiaccpfapcooalnmoeeddoifafhjdpoibpehdfckpmkblbgbhdejfemljbmhgdeeembdimobcahfpahefkfodmlgefedelecolfgbmcfghcpleallolkdabdjjnjmmlbfdbdlmhgpkechggkpinimepnhegnbdippliaeokaollmafiogldjkahihglmgohcfpmgdelfkolcdiicfjngoijopfjgcbehamflfcaibdgfdggnhmpcedepfhcpngfehgdoledhednombjnehgjlnbgakhcnlpadmchodgehhckkclbjamjadalphjhkbfnnblemognepjoejldllnjdihiojlagndkdefdalbjpmloechinhpdckcaoplckhhockbckngpfbcpgcieobmidkbcjgbpohbcepnahjlmijakoeoleeipdnooimceplefdeflbopcjbpgebomgaagglanfebicfhknbmkolldmoacheeffcjfebjaakgkhicpkbmmidgfohfiolagohlhleofiihfmjpgffafnlgfenninjjelnffkhjifefkkiekhfklljkjeanocpjdkhdflhdfbfjmfnfcojfpjjgdlomdncnhdebnkgjjcndbmogdngfekhgpbkaiaojlpkbngkignfmegiikmbcdlcjgdjikfcpcidomgacaeonbjacgbmhmhhmemppefmjmkhfnphladoimkiaikffkknckmojobpllhbkpnoggeijfonmggfhdpacddlklbonlbpphpppklmilbggkeddekbokadeeeikcalcljhpadnakpoikikdaignicnlblmglncbgeabeamdccimkcginijjhibaochcbhlaonifahopigeojongafpeancophigfmeppadkjcneokgmahinflmoehhipkjdjimeaflfdfangeiblihdopllcapgcfknfgafdnkhnhchmbdiidoebjlocbfjlkmegloadgcokemdadbaedjjlkfkicmhbgldjcopgpneoljeckpljlhklilmhelcbapgbbmbgbkmojeaephjamlkhkggkniglealmfkpoebmpjggajjajeognlkmlelcjkpbfigmipmnhlfnanahjjpeecpbkklkioaafadikelcaleneilfnneaippdiokiccmpnpkpclmaalgfhllbfajlcnblmojacbjbnglljgbkdhcifhjoljgblkdiklddgieejnkojjkljnokpiphbcapglknfjfaphmfbbalbcnnajkmhiihbmkgmefjenppddbnpabmdpaipieijcpjbfhdekafgnfkcgkcboeabejijniojkmllkkkihefhembibgmgfggjojgnphdpheneogomddooffbmfnlhcgfmkclmjnbboihcfhjofbdhmflpmbagdedeimlpeamohgchdnhfgnadeonihhgbghfgmmgegofhkhidigjaadodbjeddblflbhdjcbjbldidinkfeefmbcefniacjhcegjmkdcbemdeokjlpafdhklppnkddehdlcgphhjlmgflilmklecapiknmbffghcmjibgnkicnkncieooiojnioggifkjkhliigbikmanjlcfmpjclbngefehgpdfknpfpjjpiochcaapdaigmdkbpilgabjgphiaghdnhiifmbagkbjfpcjchieonalghogndcfplhbmdofndlclcmigiehgibkgdapdcfbdmocfggilfngobjndokkpkobonmnpdgnnbkfimdgkmejgmicgeimhcpecmhnecobmaddoleocepngjdndcoikfaoiaifegofkjciopichldlfifljlkmekhngpmimmfjnmlcpngmgnfaifmchmjjnhiolflandjeallbhijddcemfkjchcemdimcgdbkeipeaocfcplhhipgdlclfpdaeliahdkfmooikhaakkmalnggifndfefkjadclhaakigmcllacmhfefmepcbbjnafnljalfccffchmbkbnmpahhgkdlmdfkgpapjbfpgbgphmcjmffalljjpgnifmnphncookpgckacbpmehfeceeeebphgkoijikcoeiodefnocpbgijphdfalopkjcnbalhmdddkfdocblmgbfoplcoakcojjhofbdejmoimmpbclcbfkmmmblnhlabldjcenlbblohljgigmfpagpdncphminmemcnlhnbgfbjkbhdnhhcodccmbapjaaidjocaomkadedgpdocjngagjhgjiicipbfkhkelllapdjcngjodnaefclihahfinkccmigdhdjhlnglbmgiehddhgfbbhbicpofmmemkbebnpmmbalfknkfkhmfagafmopifclkleholjagkfemimflppjjgmlomeohokjoklghklpkhcplkkcbknmemajnpdlinpdjkpifoekbfoeiemfiipioofnckeompfbccdobapkbdjkibleobiaocpamipncidmbdlecddhhgadcknpaolapkifdeabfcdakbilmodkelmbbbgkcpmcemgldkgackeoojcfgdpodcibkbehgnnaglkmnlgliahcihlgcgb\",\"header\":{\"sign\":\"0c0cd6ee28a453cb754e0dee4d3fd4ec\",\"accountId\":\"4965E5E2530767C1\",\"requestTime\":\"2017-11-30 14:43:20\",\"version\":\"1.0\",\"serviceName\":\"CreateOrder\"}}";
JSONObject jsonObject = JSON.parseObject(requestJson);
String body = jsonObject.getString("body");
String reqTimeStr = jsonObject.getJSONObject("header").getString("requestTime");
String version = jsonObject.getJSONObject("header").getString("version");
String serviceName = jsonObject.getJSONObject("header").getString("serviceName");
String signSource = jsonObject.getJSONObject("header").getString("sign");
String bodyJsonStr = decryptAES(body, aesKey, aesIv);
String signParam = String.format("%s%s%s%s%s%s", accountId, serviceName, reqTimeStr, body, version, signKey);
String signTarget = md5(signParam).toLowerCase();
System.out.println(signSource);
System.out.println(signParam);
if (signTarget.equals(signSource) == true) {
System.out.println("ok");
} else {
System.out.println("验签失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param encData
* @param secretKey
* @param vector
* @return
* @throws Exception
*/
public static String encryptAES(String encData, String secretKey, String vector) {
try {
byte[] raw = secretKey.getBytes(encodeName);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(vector.getBytes(encodeName));
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(encData.getBytes(encodeName));
return encodeBytes(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param decData
* @param secretKey
* @param vector
* @return
* @throws Exception
*/
public static String decryptAES(String decData, String secretKey, String vector) {
try {
byte[] raw = secretKey.getBytes(encodeName);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(vector.getBytes(encodeName));
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = decodeBytes(decData);
byte[] original = cipher.doFinal(encrypted1);
return new String(original, encodeName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param bytes
* @return
*/
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}
/**
* @param str
* @return
*/
public static byte[] decodeBytes(String str) {
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < str.length(); i += 2) {
char c = str.charAt(i);
bytes[i / 2] = (byte) ((c - 'a') << 4);
c = str.charAt(i + 1);
bytes[i / 2] += (c - 'a');
}
return bytes;
}
public static String encryptBase64(String source) {
try {
return Base64.encode(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] decryptBase64(String source) {
try {
return Base64.decode(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String md5(String str) {
if (str == null) {
return null;
}
MessageDigest messageDigest = null;
try {
messageDigest = java.security.MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes(encodeName));
} catch (NoSuchAlgorithmException e) {
return str;
} catch (UnsupportedEncodingException e) {
return str;
}
byte[] byteArray = messageDigest.digest();
StringBuilder md5StrBuff = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
}
python
import hashlib
import json
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
def decode_bytes(string: str) -> bytes:
length = len(string) // 2
bytes_array = bytearray(length)
for i in range(0, len(string), 2):
c = string[i]
bytes_array[i // 2] = (ord(c) - ord('a')) << 4
c = string[i + 1]
bytes_array[i // 2] += ord(c) - ord('a')
return bytes(bytes_array)
def encrypted(body: dict, secret_key_raw: str, vector: str) -> str:
cipher = AES.new(bytes(secret_key_raw, 'utf-8'), AES.MODE_CBC, bytes(vector, 'utf-8'))
body_json = json.dumps(body, separators=(',', ':'))
body_bytes = bytes(body_json, 'utf-8')
body_pad = pad(body_bytes, AES.block_size, style='pkcs7')
encrypted_data = cipher.encrypt(body_pad)
data_body = encode_bytes(encrypted_data)
return data_body
def encode_bytes(bytes: bytes) -> str:
str_buf = []
for b in bytes:
str_buf.append(chr(((b >> 4) & 0xF) + ord('a')))
str_buf.append(chr((b & 0xF) + ord('a')))
return ''.join(str_buf)
def decrypted(body: str, secret_key_raw: str, vector: str) -> dict:
cipher = AES.new(bytes(secret_key_raw, 'utf-8'), AES.MODE_CBC, bytes(vector, 'utf-8'))
body_bytes = decode_bytes(body)
body_decrypt = cipher.decrypt(body_bytes)
decrypt_str = unpad(body_decrypt, 16).decode('utf-8')
decrypt_dict = json.loads(decrypt_str)
return decrypt_dict
def sign(data: object, trip_secret: str) -> str:
data = data['header']['accountId'] + data['header']['serviceName'] + data['header']['requestTime'] + data['body'] + \
data['header']['version'] + trip_secret
md5 = hashlib.md5()
md5.update(data.encode('utf-8'))
sign = md5.hexdigest()
return sign
标签:body,java,String,python,bytes,return,str,ASE,import
From: https://www.cnblogs.com/guanchaoguo/p/17720615.html