doupaoshu8334 2016-12-23 08:26
浏览 472
已采纳

使用SSL证书进行加密与生成的密钥

I am working on encryption and I am currently using an openssl_encrypt solution to encrypt my data. Currently I am rewriting an old codebase that uses openssl_public_encrypt and openssl_private_decrypt.

Of course I will have to match the encryption that was used previously, but I am thinking about undoing the encrypted data and rewriting the encryption and decryption methods for it.

The downside with openssl_public_encrypt values, is that they can not be larger than the certificate that is used for encryption. This downside does not exist for openssl_encrypt.

http://php.net/manual/en/function.openssl-public-encrypt.php#55901

The workaround in the legacy code base was to glue encrypts pieces together to be able to encrypt large data. This seems pretty messy and intensive to me.

My current solution is using openssl_encrypt with a simple generated key like Laravel would generate a key for it:

protected function generateRandomKey()
{
    return 'base64:'.base64_encode(random_bytes(
        $this->laravel['config']['app.cipher'] == 'AES-128-CBC' ? 16 : 32
    ));
}

This works brilliantly but with the old code base have chosen SSL certificates for encrypting/decrypting for a reason that I am yet not fully aware of.

What would be the advantage/disadvantage with openssl_encrypt vs the public/private way?

I guess working with third parties you can provide them your public certificate, but you could just as well provide them your generated key.

If third parties(like a financial system) need access to encrypted they can decrypt it just as well.

In both cases you are supplying the key to decrypt your data.

I am hoping somebody can shed some light on why you would limit yourself to encrypt values no longer than your certificate so you could use the public/private openssl functions.

Cheers.

  • 写回答

2条回答 默认 最新

  • dongxing7083 2016-12-23 10:17
    关注

    some light... ok...

    first let's look at what algorithms or categories of algorithms we are dealing with here:

    public/private key ... this means asymetric cryptography with algorithms like RSA... DH ...

    in contrast to symetric cryptography with algorithms like AES ... DES ... TwoFish...

    if your codebase contains stuff like gluing together chunks of encrypted data because the datasize was too large for the asymetric algorithm, one can safely say whoever build that did not know what he/she was doing.

    symetric algorithms are used to encrypt bulk data ... asymetric algorithms are used to encrypt the symetric key so that the owners of the corresponding private keys can decrypt the symetric key and therefore can decrypt the bulk data ciphertext

    the key for a symetric cipher like AES is not larger that the size restriction you find in the described methods

    the reason for asymetric cryptography is that you can share your public key with the world, and everyone can encrypt data for you (data here means in 99% of all real world scenarios: symetric keys) but only you as the holder of the private key can decrypt ... the private key never leaves your hands ... noone ever has access to that except the owner

    a certificate is merely a public key + metadata

    that metadata contains information about who owns the keypair, what it may be used for, from when to when the keypair is valid, etc...

    the metadata is put into a specific format and signed by a trusted third party ... the result is what you call a certificate

    the signature on the certificate is something else you can do with public key crypto ... but this time the other way around ... to create the signature for a certain set of data, you need the private key... without it you can't sign ... but everyone that holds the public key can verify the signature ... if you change a single bit in the data the signature belongs to, it becomes invalid, so you cannot simply tamper with the contents of a certificate and for example change the key

    update:

    if you need some thirdparty to be able to send you encrypted data while they have your public key:

    -they create a RANDOM symetric key that will be only known to them (let's call it Ksym)
    -they encrypt whatever data they need with that key and a symetric cipher ... (ct=ENC(DATA,Ksym))
    -they encrypt Ks for you using your public key (eKsym=ENC(Ksym,Kpub))
    -they send you ct and eKsym

    -you decipher eKsym (Ksym=DEC(eKsym,Kpriv))
    -you decipher DATA = DEC(ct,Ksym)

    facts:

    only you and that third party know Ksym

    if you want to store that data in a way even that thirdparty can not decrypt, you need to renecrypt it

    if you store eKsym and ct only you and the thirdparty that originally encrypted the data have knowledge of the needed decryption key Ksym (and of course everyone who got the key from either you or said third party)

    the fun fact is: Ksym is different for every piece of incoming data and is only valid for that piece of data ... sharing one Ksym with another third party only gives access to the one pice of data that belongs to this key

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功