Golang RSA encryption and decryption program

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


Import (
"Crypto/rand"
"Crypto/rsa"
"Crypto/x509"
"Encoding/pem"
"Errors"
"FMT"
)


Encryption
Func Rsaencrypt (Origdata []byte) ([]byte, error) {
block, _: = Pem. Decode (PublicKey)//Resolve key to public key instance
If block = = Nil {
return nil, errors. New ("Public key Error")
}
Pubinterface, err: = X509. Parsepkixpublickey (block. Bytes)//parsing PEM. Decode () The block pointer instance returned
If err! = Nil {
return nil, err
}
Pub: = Pubinterface. (*rsa. PublicKey)
return RSA. Encryptpkcs1v15 (Rand. Reader, Pub, origdata)//rsa algorithm encryption
}


Decrypt
Func rsadecrypt (Ciphertext []byte) ([]byte, error) {
block, _: = Pem. Decode (Privatekey)//Resolve key to private key instance
If block = = Nil {
return nil, errors. New ("Private key error!")
}
Priv, err: = X509. Parsepkcs1privatekey (block. Bytes)//parsing PEM. Decode () The block pointer instance returned
If err! = Nil {
return nil, err
}
return RSA. Decryptpkcs1v15 (Rand. Reader, Priv, ciphertext)//rsa algorithm decryption
}


Private
var privatekey = []byte ('
-----BEGIN RSA PRIVATE KEY-----
Miicxqibaakbgqdzsfv1qscqydy4vy+p4e3catmvppxqcrvrf1cb4drkv0hau24y
7m5qytt52kr539rdbkkdlam6s20lwy7+5c0dgacdwywd/7pecelyeipzjl07vro7
Ate8bfjya+wltgk9+xnuihiumukulw4kdx21+1nlauej6pew+dakmjwf6qidaqab
Aogbajlnxentqj6ofcl9fmr2jlmjjtmrtqt9inqee7m3m7blhec+mcjohmnvbjam
Zpthdordxiz6ocuof6z2+dl35lntgfh5j7s34up2bwzf1iyyqfyscnexgnhkt1g1
Xkqthmtc2gwwtheg+s6ciiyw2igrrp2rke81vyhexprexf0hakea9izb0miysmcb
/jemljb0lb3y/b8xjgjqffbqt7bmwbvjvzwzvpnmnxi9swgdgupxscuairoxjz40
Irz2c9eouwjbaopjpvv8sgw4vaseoqljvsq/c/pifx6rvzndglc8brg7sgtppjhg
4g+m3mvgpcx1a/eu1mb+fhij2laz/ptty6scqgaw9nwiwu3drivgcsmm0myh/3x9
Dacwlsjoctiodq1fq9rrede5qfpjnajdjfsijntx1f+l3yceebxtw0ynz2mcqbi8
9kp274is5fkwkufnknukuk4wkouexeo+lpr+vihs7k6wq8ngdd4/mujojbr5mkrw
Dpwqa3n5tmndqvgv8gmcqqcakgjgwygvo3/milffimbp+m7/y3vcptarldxryqwo
Aqjxwc71zgbfdityvdgjm1mtqc8xqek1fxn1vfpy2c6o
-----END RSA PRIVATE KEY-----
`)


Public
var publickey = []byte ('
-----BEGIN Public KEY-----
Migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdzsfv1qscqydy4vy+p4e3catmv
Ppxqcrvrf1cb4drkv0hau24y7m5qytt52kr539rdbkkdlam6s20lwy7+5c0dgacd
Wywd/7pecelyeipzjl07vro7ate8bfjya+wltgk9+xnuihiumukulw4kdx21+1nl
Auej6pew+dakmjwf6qidaqab
-----END Public KEY-----
`)


Func Main () {


Data, err: = Rsaencrypt ([]byte ("polaris@studygolang.com"))//rsa encryption
If err! = Nil {
Panic (ERR)
}
Fmt. PRINTLN ("RSA Encryption", string (data))
Origdata, err: = Rsadecrypt (data)//rsa decryption
If err! = Nil {
Panic (ERR)
}
Fmt. PRINTLN ("RSA decryption", String (Origdata))
}

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.