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.

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

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊