0

参考此链接http://blog.iamzsx.me/show.html?id=155002,我使用生成了一对密钥

$ openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650

这个命令。然后我得到private_key.pem& public_key.der

我遵循实现我得到的加密数据是

<ceaedf05 f39b69ab 8618f859 599926e9 c18d9233 342c58e0 534b66af 8b33dda3 375d6a11 b95ad821 11549637 ce4c26dc 760169c6 04096aff 9ec48938 1c6b149f 8180f9bb 8e1337ce 2b492af2 e0464e4c 9ab7aabd 9da53caf 1b5e5277 3c708b43 cb6515ee c9a18b02 4941416c 7f1e36d3 dcd3705b 66f8a159 ab8a7a3f a9db8306>

但我想要的是

dXXEEADvRDdk4QVM6TU9dyYtuHe8RVzP3KsWAoDDSQ2w+NELVPOv4mSVW1jIhEhb+pLsmOvLzGIW1/2qkp91M1UoPuhgplV2JEgP8EvcBn8EY43n7wVASGP9heRK0lYMfENP54f35+GFP68nU5zbrmX0IN6DGGGSNdSewzLb6s4=

实际上我想要的是以下功能:
在iOS中:

- (NSString *)encryptedString:(NSString *)plainText

在 PHP 中:

function decrypted_string()  
{
    $encrypted_string = $_POST['encrypted_string']; 
    // implementation
    return $plain_text;
}

如何实现这些功能?

我已经用谷歌搜索过了,不幸的是,没有我想要的解决方案。希望能在这里得到帮助。提前致谢。

4

3 回答 3

2

在发送到 php 之前,您需要对 NSData 进行 base64 编码。请参阅以下帖子的答案:

从 NSData 创建 base-64 字符串

在php端,您可以使用openssl_private_decrypt解密。参考如下:

http://php.net/manual/en/function.openssl-private-decrypt.php

样本:

<?php
$fp=fopen("/path/to/private_key.pem","r");
$priv_key=fread($fp,8192);
fclose($fp);
$res = openssl_get_privatekey($priv_key, "myPassword");

$data = $_POST['encrypted_string']; 
if (openssl_private_decrypt(base64_decode($data), $decrypted, $res))
         echo "Decrypted:$decrypted";
?>
于 2012-12-24T09:44:26.667 回答
1

我的建议:使用phpseclib,一个纯 PHP RSA 实现。例如。

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->setPassword('abc123');
$rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,927035AD39FA7CED

un9ayDNHNdXKtAzmGB0eCxJ+3z54qNTH2nejrvxVANqDHbOh5642hIWVxrNwD0LC
dbosArWk00bAi9HGgU0UnzN4So3XM2yVxejFh98k8VuB0zPiNWHLGYhaj81BEeob
eLI3QzPCyX7CR1KJwHJC/5xHJMKN+1TCnn/nBsP12JelBJBui6R90LShfqecdJAa
oDpGGcDtPchOaHxHkv8i4AFHKm7QBDbebZZe/Bu0tKr4DJYSeQxe+a+YoqSkdRs5
JoQiqTd+kT9RT+7Kf22xtlThJQ6mh/i7rF0N32eUQsNYgs2eHmjgu55cDI6/XC+x
zIiDAsk2prtVpMzVJDkTUVnY2+AcMfgk2Oxpsx+H/9pHYWp/qma8mmRR7ALdyzce
cVmzCh4NjSPXnlrv4LCY5mEc3ObanIx6ZirmtJWwBiqADVt+aByrLicO3AoN//UU
dUiEtvIC+cl2wZ+eRRUC4uUr47a0AakkXTjZERsJt/cTNpgFdra4mVOefNxnt1rk
lfS9dQyFrBaDiS6IwpK3I9gNuvSam9SIqhSx7WT038Lh50bKvWodDck8OHvl1FwI
FiLN/VehnTJTMDe5Ee7nC6jPqVdagH0nZkNJDlVyjKCbCEKlzYOBr9mYQUqV2lSL
fbpBJOjmHesQAhP4UJNHNOhcyHBQ5WV60y0lySKPUqu20x0N3E+ByleA8g28ijwv
F79RmcldG8x5X6Ur2W7mtKbGFfLC3CFRW9r6a0ycdpfkWo/gii/tQpbBPyYPrsWe
MNkbkzdVMLOIKZLVAd+i82UrDg3zor45JhSRsRhnLRhaZuMOukcqGQ==
-----END RSA PRIVATE KEY-----');

$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);

echo $rsa->decrypt(base64_decode('qieklzzWct0ab7UOows+uvlaI5VLEHwIED4+O9KjVKopGTPIVaB8ioSUNZKnh5LUepmc/gowrp4pM1X??J+e+rPlwqI98d56ijdWuDOOojGR5VCNilztPSxI6PIpQeL58y+B5EVUWv49dcTuGwuRiU/FL8APkNqdEE??k43SLKyhY7w='));
于 2012-12-31T00:42:22.960 回答
1

扩展纽伯特的答案..

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->setPassword('abc123');
$rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,927035AD39FA7CED

un9ayDNHNdXKtAzmGB0eCxJ+3z54qNTH2nejrvxVANqDHbOh5642hIWVxrNwD0LC
dbosArWk00bAi9HGgU0UnzN4So3XM2yVxejFh98k8VuB0zPiNWHLGYhaj81BEeob
eLI3QzPCyX7CR1KJwHJC/5xHJMKN+1TCnn/nBsP12JelBJBui6R90LShfqecdJAa
oDpGGcDtPchOaHxHkv8i4AFHKm7QBDbebZZe/Bu0tKr4DJYSeQxe+a+YoqSkdRs5
JoQiqTd+kT9RT+7Kf22xtlThJQ6mh/i7rF0N32eUQsNYgs2eHmjgu55cDI6/XC+x
zIiDAsk2prtVpMzVJDkTUVnY2+AcMfgk2Oxpsx+H/9pHYWp/qma8mmRR7ALdyzce
cVmzCh4NjSPXnlrv4LCY5mEc3ObanIx6ZirmtJWwBiqADVt+aByrLicO3AoN//UU
dUiEtvIC+cl2wZ+eRRUC4uUr47a0AakkXTjZERsJt/cTNpgFdra4mVOefNxnt1rk
lfS9dQyFrBaDiS6IwpK3I9gNuvSam9SIqhSx7WT038Lh50bKvWodDck8OHvl1FwI
FiLN/VehnTJTMDe5Ee7nC6jPqVdagH0nZkNJDlVyjKCbCEKlzYOBr9mYQUqV2lSL
fbpBJOjmHesQAhP4UJNHNOhcyHBQ5WV60y0lySKPUqu20x0N3E+ByleA8g28ijwv
F79RmcldG8x5X6Ur2W7mtKbGFfLC3CFRW9r6a0ycdpfkWo/gii/tQpbBPyYPrsWe
MNkbkzdVMLOIKZLVAd+i82UrDg3zor45JhSRsRhnLRhaZuMOukcqGQ==
-----END RSA PRIVATE KEY-----');

$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);

echo $rsa->decrypt(base64_decode('qieklzzWct0ab7UOows+uvlaI5VLEHwIED4+O9KjVKopGTPIVaB8ioSUNZKnh5LUepmc/gowrp4pM1X??J+e+rPlwqI98d56ijdWuDOOojGR5VCNilztPSxI6PIpQeL58y+B5EVUWv49dcTuGwuRiU/FL8APkNqdEE??k43SLKyhY7w='));
于 2013-01-01T05:00:00.087 回答