RSA Plus Decryption Example

Source: Internet
Author: User

RSA Plus Decryption Example

package mainimport  ("Crypto/rand" "Crypto/rsa" "crypto/x509" "Encoding/base64" "Encoding/pem" "Errors" " FMT "// " Log ") Var privatekey = []byte ('-----begin rsa private  KEY-----miicxaibaakbgqdxqrrwrluhfsgww4snjemcyrye9+rige9v7q2/smqi7/4h9tznrce/jm80vmxqb6c4t60hbmvuo8wspap2on/ tz7jx9qsfcwjorqi7wbglovovww+hyo6orifsc9lfg8bvrxuucozijtmjiorkzpis/duv/ypobav+ d1bwkzsj9qidaqabaogap2sdgcp96r25hvvg54rbw1orifewlat5yltdq7le3fm2uel6gdmm+9ao1law+ tyaaim7bhf3wtxbrlefjyuf7nslulmujvxqoxdwm+lxunqxxiiatxtuj/3nhymrnroww9jsaathsfi6ctlbni3y+skya+ Nqextcpgsrn4urv5ecqqddc8qhz1q2rj9ff+/2g+50l8f0/hdnkmwkrzgj4ynfon219qxv1to58w7+da7eqj+sqvuisi6mfgoz4fvq28y/akea +uy9svkok8nrq926fxrxlxu5lqvaa7+bipk2ewgpcjo+ytjydkr1a+j+nmgqsjex8m2dk6q9ivmk1l/ka5dyswjatmnlejmt0ln+q/j+ lxtavlgvfncbdxndacxwwyebbj9of0qvuoublobjfviujlqqfaptdhsimgfgpowknv+nlwjbajvqfp/wc1pazr4+yvhdxwr+ 7qo8rx1dyvmzmgkir4jrepopkdowos9gljymgld7xjjibpgyiltt4mzsaha2ukkcqgv1x9i+ ayn1yp45jgxiizvurphirfdanbikbymexm51ncthhhldidnvu6tup981eg87nem8qqtvwjevsbwumyy=-----End rsa private key-----') var publickey = []byte ('- ----Begin public key-----MIGFMA0GCSQGSIB3DQEBAQUAA4GNADCBIQKBGQDXQRRWRLUHFSGWW4SNJEMCYRYE9+RIGE9V7Q2 /smqi7/4h9tznrce/jm80vmxqb6c4t60hbmvuo8wspap2on/tz7jx9qsfcwjorqi7wbglovovww+ Hyo6orifsc9lfg8bvrxuucozijtmjiorkzpis/duv/ypobav+d1bwkzsj9qidaqab-----End public key-----') func  main ()  {VAR BASE64STR STRING// RSA encryption// rsa Encryption succeeds Base64 encoding data, err :=  rsaencrypt ([]byte ("Rang into |18721232342| slowly obsolete")) if err == nil {base64str =  Base64. Stdencoding.encodetostring (data) fmt. PRINTLN ("encrypted encoded content =",  base64str)} else {fmt. PRINTLN ("RSA encryption Results",  err)}// base64 True code//  the successful RSA decryption datastr, berr := base64. Stdencoding.decodestring (BASE64STR) if berr == nil {origdata, rerr :=  Rsadecrypt (DATASTR) if rerr == nil {fmt. PRINTLN ("Decode decrypted content => ",  string (Origdata))} else {fmt. PRINTLN ("RSA decryption Result:",  rerr)}} else {fmt. PRINTLN ("Base64 decoding Result:",  berr)}}//  encryption Func rsaencrypt (origdata []byte)   ([]byte,  Error)  {block, _ := pem. Decode (PublicKey) if block == nil {return nil, errors. New ("Public key error")}pubinterface, err := x509. Parsepkixpublickey (block. Bytes) If err != nil {return nil, err}pub := pubinterface. (*rsa. PublicKey) Return rsa. Encryptpkcs1v15 (Rand. Reader, pub, origdata)}//  decryption Func rsadecrypt (ciphertext []byte)   ([]byte,  Error)  {block, _ := pem. Decode (Privatekey) if block == nil {return nil, errors. New ("private key error!")} priv, err := x509. Parsepkcs1privatekey (block. Bytes) If err != nil {return nil, err}return rsa. Decryptpkcs1v15 (Rand. Reader, priv, ciphertext)}


RSA Plus Decryption Example

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.