drdr123456 2018-09-16 10:38
浏览 988

如何在Golang中使用Openssl RSA公私钥对加密和解密数据?

I am trying to write a program which encrypts data using a RSA public key and and decrypts data using private key. The RSA keys were generated with openssl tool.

I found Spacemonkeygo Openssl https://github.com/spacemonkeygo/openssl wrapper for this purpose. But unable to find any sample over & also their is no document available for the same. So that I am unable to use.

Please guide me how can I use Openssl in Golang?

I am using first time encryption decryption & Openssl.

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • dp7311 2018-09-16 11:21
    关注

    I am trying to write a program which encrypts data using a RSA public key and and decrypts data using private key. The RSA keys were generated with openssl tool.

    You don't need an OpenSSL library package to do this: you just need some of the crypto, encoding, and other packages in the Go standard library. Namely:

    Create a PEM block from the key, setting Type to "RSA PRIVATE KEY" or "RSA PUBLIC KEY", parse the keys with the x509 functions (PKIX for public), use a type assertion to make it the appropriate RSA public/private type, encrypt the message using OAEP padding, an SHA-256 hash function, and rand.Reader for a source of entropy, base64 encode the resulting cipher if you're sending it as text rather than binary, then base64 decode it and decrypt it using the same but with the private key on the other side.

    See in particular func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) ([]byte, error).

    Read the documentation in these packages and some general info about encryption and RSA, there are also usage examples for each of these packages on StackOverflow -- though perhaps not put all together.

    Every package you need for the described goal is in the Go standard library.

    You may need to check that your PKCS function version (e.g. PKCS8) lines up with the private key produced by your OpenSSL version.

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装