附录2
签名生 成
- Java
- Node.js
- Python
- .Net
- PHP
httpsRequest.setReqStat("S");
httpsRequest.setErrDesc("");
String jsonStr = gson.toJson(httpsRequest);
String message = jsonStr;
String path = KEYPAIR_DIR + "privatekey.pem";
FileReader fileReader = new FileReader(path);
PEMParser pemParser = new PEMParser(fileReader);
PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject();
byte[] encodedPrivateKey = pemKeyPair.getPrivateKeyInfo().getEncoded();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
Signature privateSignature = Signature.getInstance("SHA256withRSA");
privateSignature.initSign(privateKey);
privateSignature.update(message.getBytes(StandardCharsets.UTF_8));
byte[] signature = privateSignature.sign();
String signatureStr = Base64.getEncoder().encodeToString(signature);
const privateKeyPath = "key/private.key";
const fetchPrivateKey = async (path) => {
try {
const response = await fetch(path);
const privateKey = await response.text();
return privateKey;
catch (error) {}
}
};
const privateKey = await fetchPrivateKey(privateKeyPath);
const rsaPrivateKey = forge.pki.privateKeyFromPem(privateKey);
const request = {
reqStat: 'S',
errDesc: ''
};
const jsonString = JSON.stringify(request);
const data = `${jsonString}`;
//encryption step
const privateKey = await fetchPrivateKey(privateKeyPath);
const rsaPrivateKey = forge.pki.privateKeyFromPem(privateKey);
const md = forge.md.sha256.create();
md.update(data, 'utf8');
const signature = rsaPrivateKey.sign(md);
const signedMessage = forge.util.encode64(signature);
private_key_path = r"...\"
with open(private_key_path, "rb") as f:
private_key_data = f.read()
private_key = serialization.load_pem_private_key(private_key_data, password=None, backend=default_backend())
sourceSystem = "mxs"
request_data = OrderedDict()
request_data["reqStat"] = "S"
request_data["errDesc"] = ""
json_output = json.dumps(request_data, separators=(",", ":"))
data = f"{json_output}"
signature = private_key.sign(
data.encode('utf-8'),
padding.PKCSv15(),
hashes.SHA256()
)
encoded_signature = base64.b64encode(signature).decode('utf-8')
request_data["signedMessage"] = encoded_signature
request.reqStat = "S";
request.errDesc = "";
using (RSA rsa = RSA.Create())
{
//private key used to sign message
var reader = System.IO.File.ReadAllText(@"E:\..\private.key");
string jsonString = JsonConvert.SerializeObject(request);
var json = JObject.Parse(jsonString);
json.Remove("signedMessage");
var updateJson = json.ToString();
var minifiedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(updateJson));
String data = minifiedJson;
rsa.ImportFromPem(reader.ToCharArray());
byte[] signature = rsa.SignData(Encoding.UTF8.GetBytes(data), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
signedMessage = Convert.ToBase64String(signature);
}
$paymentRequest->reqStat = 'S';
$paymentRequest->errDesc = '';
$jsonString = json_encode($paymentRequest);
$json = json_decode($jsonString);
unset($json->signedMessage);
$updateJson = json_encode($json);
$minifiedJson = json_encode(json_decode($updateJson));
$data = $minifiedJson;
$privateKey = file_get_contents('key/mxs/mxs_keypair/private.key');
//Sign the data
$rsa = openssl_get_privatekey($privateKey);
openssl_sign($data, $signature, $rsa, OPENSSL_ALGO_SHA256);
$signedMessage = base64_encode($signature);