Go language RSA uses the Generate public key key, go uses RSA encryption to decrypt

Source: Internet
Author: User
Tags decrypt rand readfile
Package main import ("Crypto/rsa" "crypto/x509" "Encoding/pem" "Crypto/rand" "Flag" "Log" "OS") Func main () { var bits int flag. Intvar (&bits, "B", 2048, "key length, default 1024-bit") if err: = Genrsakey (bits); Err! = Nil {log. Fatal ("Key file generation failed. ")} log. PRINTLN ("Key file generation succeeded. ")} func Genrsakey (bits int) error {//Generate private key file Privatekey, err: = RSA. GenerateKey (Rand. Reader, BITS) if err! = Nil {return err} derstream: = X509. Marshalpkcs1privatekey (privatekey) Block: = &pem. block{Type: "Private Key", Bytes:derstream,} file, err: = OS. Create ("Private.pem") if err! = Nil {return err} err = Pem. Encode (file, block) if err! = Nil {return err}//Generate public key file PublicKey: = &privatekey.publickey Derpkix, err: = X 509.MarshalPKIXPublicKey (PublicKey) if err! = Nil {return ERR} block = &pem. block{Type: "Public Key", Bytes:derpkix,} file, err = OS. Create ("Public.pem") if err! = Nil {return err} err = Pem. Encode (file, block) if err! = Nil {return err
	} return Nil}
 



Package main import ("Crypto/rand" "Crypto/rsa" "crypto/x509" "Encoding/base64" "Encoding/pem" "Errors" "Flag" " FMT "" "Io/ioutil" "OS") var decrypted string var privatekey, PublicKey []byte func init () {var err error flag. Stringvar (&decrypted, "D", "", "encrypted data") flag. Parse () publickey, err = Ioutil. ReadFile ("Public.pem") if err! = nil {os. Exit ( -1)} Privatekey,err = Ioutil. ReadFile ("Private.pem") if err! = nil {os.
		Exit ( -1)}} Func main () {var data []byte var err error data, err = Rsaencrypt ([]byte ("Fyxichen")) if err! = Nil { Panic (Err)} origdata, err: = Rsadecrypt (data) if err! = Nil {panic (err)} FMT. Println (String (origdata))}//cryptographic 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)}//Decrypt 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)}


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.