[Golang Software recommendation] RSA Public Private key decryption (resolves Golang private key encryption public key decryption problem)

Source: Internet
Author: User
Tags asymmetric encryption
This is a creation in Article, where the information may have evolved or changed.

Security is the beginning of the software is a very important link in the financial scene and the design of assets in the scenario is more so, in the encryption algorithm is mainly used in more encryption methods are symmetric encryption and asymmetric encryption, symmetric encryption is representative of Aes,des,3des , etc. Asymmetric encryption is used more than RSA,ECC , etc., the recent hot Bitcoin using ECC elliptic curve algorithm, this article is mainly in the use of Golang in RSA using the private key to encrypt the decryption of public key problems encountered, and to find a solution to explain, Hope can help to everyone!

Attached:

Meow, kitty. Blog: W-blog.cngorsa-github address: Https://github.com/farmerx/gorsa Meow optimized Gorsa-github address: Https://github.com/wenzhenxi/gorsa

PS: Special thanks to Farmerx for the GORSA implementation

1. Understanding RSA

The difference between symmetric and asymmetric encryption is the first to understand RSA:

    • In symmetric encryption, only one key is the key, and decryption relies on this set of keys
    • Asymmetric encryption has the public key points, the private key can produce public key (Bitcoin wallet address is the public key), general encryption by public key encryption private key decryption (also have private key encryption public key decryption)

RSA Usage Scenarios:

    1. We are most familiar with the HTTPS is the use of RSA encryption, the CA authority issued to you is the private key to us to configure, in the request process end with the CA built into the system's public key encryption, the request of the server by the server for decryption verification, guarantee the transmission process of the request encryption

    2. High security scenarios (such as financial equipment UnionPay transactions, etc.) under the two-way authentication (a machine one key), each machine will generate a local set of public private key pairs, and the bar public key to the server, the use of the originating request model is as follows:

Server's public key pair abbreviation: S_PUK,S_PVK

End-generated public key pair abbreviation: C_PUK,C_PVK

Server storage: S_PVK and C_puk

-Side storage: S_PUK,C_PVK

The server uses the C_PVK encryption request-using C_PUK decryption (authentication side), using S_PVK encryption to return the results back to the end using S_puk decryption to obtain the returned results (authentication server)

This process completes the end authentication server, the server Authentication terminal is called Two-way authentication (here refers to the simple expression of this model, more secure mode will introduce the encryption machine further security)

PS: The specific algorithm implementation of RSA encryption can refer to the following two articles

Principles of RSA Algorithm (i.)

Principles of RSA Algorithm (II.)

2.GoRSA

In the Golang use RSA encryption algorithm when the author met a pit, in the Internet to find all the official library Crypto/rsa only public key encryption private key decryption implementation, means that the private key encryption public key decryption can not be achieved, and to achieve two-way authentication must be used Private key encryption public key decryption , through a few hours of looking in fact there are many forums are also discussing this issue, there are children's shoes on GitHub mentioned some solutions, the use of C packaging once, etc., but using other particularly uncomfortable and even can not run up, In the desperate time to find a seemingly can use the library, by looking at the source code used is a soft implementation, recommended here to everyone

The following points are optimized for encapsulation based on Https://github.com/farmerx/gorsa:

    • Optimizing Public private keys requires pre-registration initialization, and public-private keys can be confusing in case of concurrency
    • Encryption machine is not base64 processing, in cross-program delivery or stored procedures need Base64 avoid secondary encapsulation
    • Incoming returns uniformly use the string type to avoid conversion hassles

To get the expansion pack:

go get github.com/wenzhenxi/gorsa

Specific use:

Package Mainimport ("Log" "Errors" "Github.com/wenzhenxi/gorsa") var PubKey = '-----BEGIN Public Key-----miibijanbgkqhkig9w0baqefaaocaq8amiibcgkcaqeak+ 89v7vpoj1rg6btakym56qmflwncbvdj3mltvvtxvuubyqc5b6u909mmmrlbqs//pwc6zc3wzzu1+ayh8xbuaezua3ejlphiafiviz04raw10+ 1xnby/rqe23tdqsv9a2jv/axje/27b62nzvcweitu1knq3mgdcuqkjke+lkhq7nwprcod/ffvqsurvg0yfuekoz/ 6upspr6vri331hwrb4dlyy8qfumdsyvvexe4njzwblxcqkexrrahi2sqrcl3teguihtduxcskridiamd+qt2yp+vvbz6huiqiwsih1bohjer/ Joq2/o6x3cmuppu4advngy8bq236ixvrmqidaqab-----END Public Key-----' var pirvatekey = '-----BEGIN private key-----miiepaibaakcaqeak+ 89v7vpoj1rg6btakym56qmflwncbvdj3mltvvtxvuubyqc5b6u909mmmrlbqs//pwc6zc3wzzu1+ayh8xbuaezua3ejlphiafiviz04raw10+ 1xnby/rqe23tdqsv9a2jv/axje/27b62nzvcweitu1knq3mgdcuqkjke+lkhq7nwprcod/ffvqsurvg0yfuekoz/ 6upspr6vri331hwrb4dlyy8qfumdsyvvexe4njzwblxcqkexrrahi2sqrcl3teguihtduxcskridiamd+qt2yp+vvbz6huiqiwsih1bohjer/ joq2/o6x3cmuppu4advngy8bq236ixvrmqidaqabaoibaqccbxzvhmfvceg+yud5+ W63dmcq0qpmdllzpbwpxmeclh8smm5uq2sruegy5ubng0wkc/r64bzris6pjkcrzqu95rp+heugem3c4smdiwtmyzwea8uisy7fhbkiq/ Rly6an5eb0kmjpzfa16s9ktszdtfnvp9tmuao7iie6ihet1x0wcx7aowvqp9mdxbhv5t0tvt8vfrptldfgiuk45t3tr83tdcx53uc8cl5ui8lewqjph4bgdhj 3/mgtdwg+lw2vlab4x+ alcdjmch6rcb1b8hs9iltdkdvw9kiryqh5mbacxzydeaqj1i2kamjiu2qdutnkxnoc96hy2xmusndhaogbampwjupuzqiojfnys99x++ ztcvvwgrabevtvh6jpsga0k3cykgwrnnssmkhbzza0p3/nmswwc7lil8whefudap2ntvfpvj19xvm71gnuycq/ hojqiaxytsnt1gbutcmtfzmakusjqdm/hunjmm9zh+w4lt5qm2y/ Ykcthoi65bvbaogbamfigsibnjdnhvap7hfwcymgolwgeeechg6uq6lbai9t8c7xmsfc6dqinmmqualgdamvb6izpk4kgqaxmft5h7hekzgkbxckbd9xslm72 bwhm/nd/hkzdhqqrnapfhy6/s8cijrnrfdhsjbia8k73yiucsqlhaaugfpzdhet8ktjaogaqdxezi1dapuirhmun9zrkr8nke1uz0aafirpmc+ cp2hk05pwvaptatixto0jwu38g3qlcytwdqga6wwpxnopnikkcmxjjmqo2yjtrg9gevazdsalhxprpptwkspet+ o2oxna40pomk54uhydhyeuakuxqsd4mcw4jxzjn0suuzmcgyagzpbckjulch19ffi69rdidjqqpiufyevit7hibspttlham+ 3u78oqlzqukmrdcx5ddcidziicmfkvf8whertivaqsfhytnf/pmw8avupy5g3if5/nhj76cnrubhsfqtv+wqnzoypphzgvqeqbhcoxjsm+ Qv3cdgjlu6omhgqeaqkbgqcnml5sx7gsaeb0rsnugpp2gezaqj0h4occ8knrhk8ruvxiu9b2zka2z/qukfb1gigckxyr+lqq25/+tgvinjuf6p3fvkhl0u8jog0iqppjxo3vl9b8ewwlcfqvb/nqfmama4chk0qeue+mqi++ mwgybrhx1lioxefujo+pxrmekw==-----END Private Key-----' func main () {//Public key encryption private key decrypts if err: = Applypubeprid (); Err! = nil {log. PRINTLN (ERR)}//Public key decryption private key Encryption if err: = APPLYPRIEPUBD (); Err! = Nil {log. PRINTLN (ERR)}}//public key encryption private key decryption Func Applypubeprid () error {Pubenctypt, err: = Gorsa. Publicencrypt (' Hello World ', PubKey) if err! = Nil {return Err}pridecrypt, err: = Gorsa. Prikeydecrypt (Pubenctypt,pirvatekey) if err! = Nil {return err}if string (pridecrypt)! = ' Hello world ' {return errors. New (' decryption failed ')}return nil}//public key decryption private key cryptographic func applypriepubd () error {Prienctypt, err: = Gorsa. Prikeyencrypt (' Hello World ', pirvatekey) if err! = Nil {return Err}pubdecrypt, err: = Gorsa. Publicdecrypt (Prienctypt,pubkey) if err! = Nil {return err}if string (pubdecrypt)! = ' Hello world ' {return errors. New (' decryption failed ')}return nil}

3 Summary

RSA is widely used in software development, if you also encounter Golang private key encryption public key decryption problem , welcome to use GORSA extension to solve problems, welcome to the collection of praise!

Note: The author has limited ability to say the wrong place hope that we can point out, but also hope to communicate!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.