Golang Aes Encryption Example

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

An example of AES encryption. For members ' reference.

package mainimport  (     "Crypto/aes"      "Crypto/cipher"      "FMT") Func main ()  {    aesenc := aesencrypt{}     arrencrypt, err := aesenc.encrypt ("ABCDE")     if  err != nil {        fmt. Println (Arrencrypt)         return    }     strmsg, err := aesenc.decrypt (Arrencrypt)     if err ! = nil {        fmt. Println (Arrencrypt)         return    }     fmt. Println (STRMSG)}type aesencrypt struct {}func  (this *aesencrypt)  getkey ()  [ ]byte {    strkey :=  "1234567890123456 "    keylen := len (strkey)     if keyLen <  16 {        panic ("res key  length cannot be less than")      }    arrkey := []byte (strkey)     if keylen  >= 32 {        //fetch the first 32 bytes of          return arrkey[:32]    }    if keylen  >= 24 {        //fetch the first 24 bytes of          return arrkey[:24]    }    //take the first 16 bytes     return arrkey[:16]}//Encrypted string func  (This *aesencrypt)  Encrypt (STRMESG  string)   ([]byte, error)  {    key := this.getkey ()      var iv = []byte (Key) [: AES. Blocksize]    encrypted := make ([]byte, len (STRMESG))      aesblockencrypter, err := aes. Newcipher (Key)     if err != nil {         return nil, err    }    aesencrypter :=  cipher. Newcfbencrypter (AESBLOCKENCRYPTER,&NBSP;IV)     aesencrypter.xorkeystream (encrypted, [ ]byte (STRMESG))     return encrypted, nil}//Decrypt string func  (This *AesEncrypt )  decrypt (src []byte)   (strdesc string, err error)  {     Defer func ()  {        //error handling          if e := recover (); e != nil {              err = e. (Error)         }    } ()     key := this.getkey ()     var iv = []byte ( Key) [: AES. Blocksize]    decrypted := make ([]byte, len (SRC))      Var aesblockdecrypter cipher. Block    aesblockdecrypter, err = aes. Newcipher ([]byte (key))     if err != nil {         return  "",  err    }    aesdecrypter  := cipher. Newcfbdecrypter (AESBLOCKDECRYPTER,&NBSP;IV)     aesdecrypter.xorkeystream (decrypted,  SRC)     return string (decrypted),  nil}


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.