Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Public key encryption and Private key decryption of RSA by GO

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how GO implements the public key encryption and private key decryption of RSA. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Package main import ("crypto/rand", "crypto/rsa", "crypto/x509", "encoding/base64", "encoding/pem", "errors", "fmt") / / can be generated through openssl / / openssl genrsa-out rsa_private_key.pem 1024var privateKey = [] byte (`- BEGIN RSA PRIVATE KEY-MIICXQIBAAKBgQCrGh2sc5AKD1EQ8WdA1iWF4m7wXtO6WoS7Dtfd0Jm2ud+LKBQ+e7R6YIXnwfEKB/4Jm+jNtCi7/Zrx5gtEpUuVAyrEo5+qr5al5KibeJq3xyI/626IBsDMFX5o3WOoXceTF7+lgi6r+OuokqFJgpeh7YANXQ8Y8mn8ucw+Ly+LbQIDAQABAoGAGgoxbC3yP/WwyrlSk4WD1Gpvo9lqs7PO+4D4zWNP4YVMRitlWVUOVImYF3tmqbYprWCy/4tpn6KrECGImXvmkplXPxd4x3W+haZftx3VjTwh6fvT9yHp4swXxN+hLMItDdIOWS4U6wVJa77Dy7VfK303LZrPLqnxkf4oEywp5YECQQDZOz1WD7nOqOiyAlwDhfeLTmArN0f+gV6RLrxMp2XRqC2DN5nMq5O5BVVMK9LBgArNqYfxWYuMa3K2qliRDPPxAkEAyaNWq) / fDvjpK9TgztqsHIiG+cUQpWI759zt5qHNA+QF4L43dtAVZzBR/uam1jnRuM6K0ZCSZo2ITiqapmk8bPQJAEd9d3IbOssIS4xJun5uWElAQeX3C3p2mOiuuMmBTcDx2AiXA8aXsMXzO18WDQYhXWzRniuPjJ1pvxbeeMdDvAQJBAMDhuZAJEzrOAlQurfFICyvQQZ+Rx0dKhbzFLOxBS96mVDSRLYn+MFbzKPcOa3lY0O4d7xd4l2td7zmLkePlVjUCQQCY8VuIfKc0+AWvPnktKXbx9bBdJZSDginZM5cu7pdxW0uB9KZoLqgbGLIvWrLyA6SBqo87Q1j1//wFgLP+A2Gn-END RSA PRIVATE KEY- `) / / openssl//openssl rsa-in rsa_private_key.pem-pubout-out rsa_public_key.pemvar publicKey = [] byte (`-BEGIN PUBLIC KEY-MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrGh2sc5AKD1EQ8WdA1iWF4m7wXtO6WoS7Dtfd0Jm2ud+LKBQ+e7R6YIXnwfEKB/4Jm+jNtCi7/Zrx5gtEpUuVAyrEo5+qr5al5KibeJq3xyI/626IBsDMFX5o3WOoXceTF7+lgi6r+OuokqFJgpeh7YANXQ8Y8mn8ucw+Ly+LbQIDAQAB-END PUBLIC KEY- `) / / encryption func RsaEncrypt (origData [] byte) ([] byte Error) {/ / decrypt the public key block in pem format, _: = pem.Decode (publicKey) if block = = nil {return nil, errors.New ("public key error")} / / parse the public key pubInterface, err: = x509.ParsePKIXPublicKey (block.Bytes) if err! = nil {return nil Err} / / Type assertion pub: = pubInterface. (* rsa.PublicKey) / / encryption return rsa.EncryptPKCS1v15 (rand.Reader, pub, origData)} / / decrypt func RsaDecrypt (ciphertext [] byte) ([] byte, error) {/ / decrypt block, _: = pem.Decode (privateKey) if block = = nil {return nil Errors.New ("private key error!")} / / parse the private key priv in PKCS1 format, err: = x509.ParsePKCS1PrivateKey (block.Bytes) if err! = nil {return nil, err} / / decrypt return rsa.DecryptPKCS1v15 (rand.Reader, priv, ciphertext)} func main () {data _: = RsaEncrypt ([] byte ("test data Ω.. .2") fmt.Println (base64.StdEncoding.EncodeToString (data)) origData, _: = RsaDecrypt (data) fmt.Println (string (origData))} about "how GO implements public key encryption and private key decryption of RSA", this is the end of the article. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report