从 RSAUtils.encryptByPublicKey (Java) 到 openssl_public_encrypt (PHP)
From RSAUtils.encryptByPublicKey (Java) to openssl_public_encrypt (PHP)
我正在尝试从 Java 代码使用 openssl_public_encrypt 在 php 中进行转换,但无法在列表中进行管理以返回输出。
如果有人能提供帮助,将不胜感激
MerchantPublicSecretKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDrXM8q2qrEuH+xFVcEpeiV4yX7r/5+SsjgLD/OF3VPYqKA07xOklLXhJqT8Shjtu/uye9BahqajqOiP9PjqAsN1amGodE1POhsAO537Av+pIh1sX1ULr+0fzxWzwphE3SX2dImTwWWxdZZB45P2GMtR1F+jA4E2Ud8oO1KIaX++QIDAQAB'
userInfoJson = {
"payCardNo":"test1",
"payCardBank":"test2",
"payCardBranch":"test3"
}
Java Code
sign = =Base64Utils.encode(RSAUtils.encryptByPublicKey(userInfoJson.getBytes("UTF-8"), MerchantPublicSecretKey));
我设法解决了这个问题,如果能帮助到这里的人就是解决方案
$app_secret = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDrXM8q2qrEuH+xFVcEpeiV4yX7r/5+SsjgLD/OF3VPYqKA07xOklLXhJqT8Shjtu/uye9BahqajqOiP9PjqAsN1amGodE1POhsAO537Av+pIh1sX1ULr+0fzxWzwphE3SX2dImTwWWxdZZB45P2GMtR1F+jA4E2Ud8oO1KIaX++QIDAQAB';
$user_data= [
"payCardNo" => "test1",
"payCardBank" => "test2",
"payCardBranch" => "test3"
];
function encryptData ($user_data){
$user = json_encode($user_data, JSON_FORCE_OBJECT);
$pub_key = chunk_split($app_secret, 64, "\n");
$pub_key = "-----BEGIN PUBLIC KEY-----\n" . $pub_key . "-----END PUBLIC KEY-----\n";
$key = openssl_pkey_get_public($pub_key);
$result = '';
for($i = 0; $i < strlen($user) / 117; $i++ ) {
$data = substr($user, $i * 117, 117);
openssl_public_encrypt ($data, $encrypt, $key);
$result .= $encrypt;
}
return base64_encode($result);
}
// usage
$encrypted_data = encryptData($user_data);
我正在尝试从 Java 代码使用 openssl_public_encrypt 在 php 中进行转换,但无法在列表中进行管理以返回输出。
如果有人能提供帮助,将不胜感激
MerchantPublicSecretKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDrXM8q2qrEuH+xFVcEpeiV4yX7r/5+SsjgLD/OF3VPYqKA07xOklLXhJqT8Shjtu/uye9BahqajqOiP9PjqAsN1amGodE1POhsAO537Av+pIh1sX1ULr+0fzxWzwphE3SX2dImTwWWxdZZB45P2GMtR1F+jA4E2Ud8oO1KIaX++QIDAQAB'
userInfoJson = {
"payCardNo":"test1",
"payCardBank":"test2",
"payCardBranch":"test3"
}
Java Code
sign = =Base64Utils.encode(RSAUtils.encryptByPublicKey(userInfoJson.getBytes("UTF-8"), MerchantPublicSecretKey));
我设法解决了这个问题,如果能帮助到这里的人就是解决方案
$app_secret = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDrXM8q2qrEuH+xFVcEpeiV4yX7r/5+SsjgLD/OF3VPYqKA07xOklLXhJqT8Shjtu/uye9BahqajqOiP9PjqAsN1amGodE1POhsAO537Av+pIh1sX1ULr+0fzxWzwphE3SX2dImTwWWxdZZB45P2GMtR1F+jA4E2Ud8oO1KIaX++QIDAQAB';
$user_data= [
"payCardNo" => "test1",
"payCardBank" => "test2",
"payCardBranch" => "test3"
];
function encryptData ($user_data){
$user = json_encode($user_data, JSON_FORCE_OBJECT);
$pub_key = chunk_split($app_secret, 64, "\n");
$pub_key = "-----BEGIN PUBLIC KEY-----\n" . $pub_key . "-----END PUBLIC KEY-----\n";
$key = openssl_pkey_get_public($pub_key);
$result = '';
for($i = 0; $i < strlen($user) / 117; $i++ ) {
$data = substr($user, $i * 117, 117);
openssl_public_encrypt ($data, $encrypt, $key);
$result .= $encrypt;
}
return base64_encode($result);
}
// usage
$encrypted_data = encryptData($user_data);