dongpou7275 2017-10-19 11:45
浏览 93
已采纳

存储加密的PEM块是否安全?

I have some experience using Go, but now I don't really understand the complexity in security of what I am doing, so I need to ask.

I am creating an RSA private key, converting to PEM and then encryping it with a passphrase.

So, how secure is to store it in a public place?

I'm not looking for answers like "it's ok, just change the passphrase over time", I really want to know which mechanism of cypher Golang is using to do it and if is safe to leave the encrypted PEM in, for example, a public blockchain and why I can do it or why I cannot.

I'm leaving here the code I am using right now:

func New(passphrase string)(*pem.Block, error){
    pk, err := createPrivateKey(2048)
    if err != nil {
        return false, err
    }
    pem := getPemFromPK(pk)
    block, err := EncryptPEMBlock(pem,passphrase)
    if err != nil {
        return false, err
    }

    return block,nil
}

func createPrivateKey(bits int) (*rsa.PrivateKey, error){
    pk, err := rsa.GenerateKey(rand.Reader, bits)
    if err != nil {
        return nil, err
    }
    return pk,nil
}

func getPemFromPK(pk *rsa.PrivateKey) (*pem.Block){
    block := &pem.Block{
        Type:  "RSA PRIVATE KEY",
        Bytes: x509.MarshalPKCS1PrivateKey(pk),
    }
    return block
}

func EncryptPEMBlock(block *pem.Block, passphrase string) (*pem.Block, error){
    block, err := x509.EncryptPEMBlock(rand.Reader, block.Type, block.Bytes, []byte(passphrase), x509.PEMCipherAES256)
    if err != nil {
        return nil, err
    }
    return block,nil
}

Thank you very much.

Edit:

As an answer here and other forums, it is not recommended to publish in public any type of private key, even if encrypted.

This topic is answered.

  • 写回答

1条回答 默认 最新

  • dongzhenyin2001 2017-10-20 05:27
    关注

    You are making a mistake in your thinking about what a private key is and what a passphrase is. The passphrase is used to encrypt and unencrypt your private key - if you are storing a key file which needs a passphrase to be used, then that file contains your encrypted key.

    If you store the "private key" as you say, it sounds like you wish to publicly store the unencrypted key. However, even if you publish an encrypted private key on a public online repository, there's many ways to crack a passphrase. If the passphrase is short or unsecure in other ways, the attacker now has your private key. If they target you and gain access to a machine of yours that has used this key in an application (i.e. bash), then they can just access bash history log to find the passphrase.

    Sometimes actually, it's trivial to keylog someone in a targeted attack.

    There are many many things that can go wrong if you store an unencrypted private key online.

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题