$config = array(
//"config" =>"D:/phpserver/Lighttpd/openssl.cnf",
//'config' => 'D:/phpStudy/Lighttpd/OpenSSL.cnf',
'private_key_bits' => 1024, // 키 크기.
'private_key_type' => OPENSSL_KEYTYPE_RSA
);
//$res = openssl_pkey_new();
$res = openssl_pkey_new($config);
// 개인 키 가져오기
// openssl_pkey_export($res, $privkey, "PassPhrase 번호 1 " );
openssl_pkey_export($res, $privkey);
var_dump($privkey);
// 공개 키 가져오기
$pubkey=openssl_pkey_get_details($res);
// echo "------------>
";
// print_r($pubkey["rsa"]);
// $bin_str=$pubkey["rsa"]["n"];
// print_r($bin_str);
// echo "
";
// //echo $bin_hex_str = pack("H*" , bin2hex($bin_str));
// echo $bin_hex_str = bin2hex($bin_str);
// echo "
----- -------<
";
$pubkey=$pubkey["key"];
// var_dump($privkey);
// var_dump($pubkey);
echo $privkey."
";
echo $pubkey."
";
?>
$private_key = '-----RSA 개인 키 시작-----
MIICXQIBAAKBgQDNSQ4AmPZE01MpEO9dabRDsS0XwxKekEFUoWJneFG+NA6P52Js
ISw79QYNfQwM7akIaPpzU0 +kkWjTSXiDqFUv5y2vaA3W2Tre5N51uCwUK6/LfzHX
Bg0d+Bnlzy6Kb6bpgPgvbhAkNbvjbCS0V9h4v3WUTLR9QauJ05Q8qe70sQIDAQAB
AoGAb/9aZ1MMkNImfbIilXk1 AvVn353qy51yNyTxFfhbyISIKVSYqtuSUYfFbJh7
eZYz8UWs1jxuqB847IUZ/eJZzpVev480Tv9YejN0DhJ4Yw6y/kpJWo/9kEcET6Tk
vatXh+wgmieszkxe 0kUvk41GPV7a4GWWQj6P4vjx+qktaNECQQDuo7ENW10dZXLG
YNKqUhgkdi9NPjRlHCg Z4RW0uoRqUsggqNVkaqpY8BaJOkkXWiQ0aVTPBiwo8k0+
S50rGNYNAkEA3DgxY9OwRc6 5VqS/UxV28pyLe2gy0XSVwA97SVNvYJJ58iNGEvYs
vEZ3GrTNAZpbPKcruCS6NiDK761Ot5A0NQJAOsRO1pY4QQP5lsbDOjRLQ7djAKEf
6YJckbrvSHliIHjQVdKd8kOePZaqRhL3auvE3K9Ptkez2kWJJh0i0DkmhQJBAMCm
F+7wnWA0PEbj0MvqFyiechb/TJWXm/lskbnP17tv6GY/HWoxGWmLa15dlIo2cmJZ
DBI Hby8y+NU7k4VaDZECQQCO018O5TJWl+O+bWyzV1eR603YP+JVjiSKUPtuyzVS
NIz+ClwspGmdUwPWlLqr5JfKm+OfpaLIepfJinsCgwpP
-----RSA 개인 키 끝---- -';
//$private_key ='-----RSA 개인 키 시작----
//MIICXAIBAAKBgQDVFOcE2nEBi+4FYvS36j1ShHHvusR0dXXe9NzAA/biH6NODywH
//cQo2Lyco6oSvY+cSs PewjjyatRCtq/Ri6c/ am64CJu+HM1lZH1Gh05C2KVJ2dveX
//gcdsz+/XPHDxYKAq/hW9S1NeYr2x4eJdpYNYucLn8XWgeLRnlJUnBl/saQIDAQAB
//AoGAK2oz28hKKB+DorNT2zktv2de85JhPr1EA +ZuJ+1m2xHJKzlqJiYBB6M53ltS
//J446YZOPE/kXHgpwWIJlq9Wp1KiZPEfoRWzz1 wmuj8koq95vcxBeuyjX4ez4Dio6
//Vmr9BABFuoAh0GAq8kTJOz0DURCEcwv+iI7IL aNoRjPFWuUCQQDveqRLE2/N8NIy
// P7YVnniAiaSL/e+XYQXCMIDsdEdqZNRjhjfDoBqpV/tGjnuAn9zBVHNNLbLni5nf
//2nqgrh5jAkEA48gQmBO9koOnvO//Q581JSBKfga4i7DetQ80OD4kYt6fwezkaeHX
//k6 4sjmpfD/Ys2heKFhyNDO/k0NsUxGlNwwJBAKPx7/u9qLLyBNSFrWALhy9oC5AT
// Hi0iLNEOZ5R43lNGB+jCf/aa6uW357wb2wzpZDWuXOkfOfUuKv+HEKBj00UCQCxd
/ /woWsiqcoB5rwcBrevr49fOTxMC7dQ3Omw3gU+odbb2iZWKAbAh9ctIcExwFX5MKS
//BS5rXzne3AWmNnRPQnsCQDdOCBxkF5YctHqlmEETfKodVZG69COZGykdlenOxK6g
//qNNAT2y2mxInYGlPIJ+TfxxFIqesHdph4s/lw6T2ISY=
//-----END RSA 개인 키-----';
$public_key = '---- -공개 키 시작----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNSQ4AmPZE01MpEO9dabRDsS0X
wxKekEFUoWJneFG+NA6P52JsISw79QYNfQwM7akIaPpzU0+kkWjTSXiD qFUv5y2v
aA3W2Tre5N51uCwUK6/LfzHXBg0d+Bnlzy6Kb6bpgPgvbhAkNbvjbCS0V9h4v3WU
TLR9QauJ05Q8qe70sQIDAQAB
-----공개 키 끝-----' ;
//$private_key=$privkey;
//$public_key=$pubkey;
$fp=fopen("private_key","w");
fwrite ($fp,$private_key);
fclose($fp);
$fp=fopen("public_key","w");
fwrite($fp,$public_key);
fclose($fp);
//echo $private_key;
$pi_key = openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用 ,可用返回资源id 리소스 ID
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
print_r($pi_key);echo "n";
print_r($pu_key); echo "n";
echo "
---------
";
$data = "woshizhu";//原始数据
$encrypted = "";
$decrypted = "";
echo "소스 데이터:",$data,"
";
echo "개인 키 암호화:n";
openssl_private_encrypt($data,$encrypted,$pi_key);//개인 키 암호화
$encrypted = base64_encode($encrypted);//암호화된 콘텐츠에는 일반적으로 특수 문자가 포함되어 있으며 다음에서 사용하려면 인코딩 변환이 필요합니다. network URL을 통해 전송할 때 base64 인코딩이 URL에 안전한지 주의하세요
echo "
------------개인 키 암호화--------- ---
";
echo $encrypted,"
";
echo "
echo "공개 키 해독:n";
$decrypted="";
openssl_public_decrypt(base64_decode($ encrypt), $decrypted,$pu_key);//개인키로 암호화된 내용은 공개키로 복호화할 수 있습니다
echo $decrypted,"n";
echo "---- ------ ---------------n";
echo "공개 키 암호화:n";
openssl_public_encrypt($data,$encrypted,$pu_key);//공개 키 암호화
$encrypted = base64_encode($encrypted);
echo $encrypted,"n";
$decrypted="" ;
//$encrypted="JBeapcp9iWWYJYElgqtrZxfxM4wVkCaSn/oJZ7NjfR23o76fdbxEXpf+PGWACw3PeTdObwL4108wR3ihKmy2iYkIExGjBYyvx2w9aHies8ZsOIP3LjiMHYTm9 3Rr8 Sc5XxHWQc3Dhbq16JWHYZ2d+RrOpHd4x84GF3JXwivrGO4=";
echo "개인 키 해독:n";
openssl_private_decrypt(base64_decode($encrypted), $decrypted,$pi_key); //개인 키 복호화
echo "--->:".$decrypted,"n";
?>
위 내용은 내용의 측면을 포함하여 php openssl rsa 암호화 및 복호화를 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.