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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测