douchen4547 2014-04-13 01:00
浏览 182
已采纳

如何在数据库或服务器中存储私钥(php)

I have a web application that uses private and public keys to encrypte my fillable form.

I'm using OPENSSL and PHP. My question is that how can i store private keys for each user in database or server? I dont know which one is more safely. Additionaly, my encyrption code ;

//create new private and public key

$new_key_pair = openssl_pkey_new(array(

    "private_key_bits" => 2048,

    "private_key_type" => OPENSSL_KEYTYPE_RSA,

));

openssl_pkey_export($new_key_pair, $private_key_pem);

$details = openssl_pkey_get_details($new_key_pair);

$public_key_pem = $details['key'];

//create signature

//openssl_sign($data, $signature, $private_key_pem, OPENSSL_ALGO_SHA256);

//save for later

file_put_contents('private_key.pem', $private_key_pem);

file_put_contents('public_key.pem', $public_key_pem);
//file_put_contents('signature.dat', $signature);

//verify signature
//$r = openssl_verify($data, $signature, $public_key_pem, "sha256WithRSAEncryption");
//var_dump($r);


echo $private_key_pem;

echo "
";

echo $public_key_pem;

echo "
";

echo $data;

echo "
";

How can i prevent my private and public keys ? It shows on the screen

  • 写回答

1条回答 默认 最新

  • duanhuokuang5280 2014-04-16 21:39
    关注

    The public key need no security, so you can save as clear text in the database.

    with the private key you have different solution based on level of security and kinds of attacks you want to avoid.

    1 save the pk as clear text in the db. Never write php code that echo the pk

    2 save the pk in p12 format protect it with a password. You can prompt the password to the user every time you need

    3 generete, store and use the pk using a HSM http://en.m.wikipedia.org/wiki/Hardware_security_module

    i suggest solution 2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区