Golang1.7.3 Non-Utility extension protocol using AES encryption for standard libraries

Source: Internet
Author: User
Tags decrypt decrypt text
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Crypto/aes"    "Crypto/cipher"    "FMT") type tobytes struct {CIP cipher. Block Pdgtext []byte}func Main () {CIP, _: = AES. Newcipher ([]byte("1234567891234567")) Enc: = &tobytes{cip:cip, Pdgtext:make ([]byteCip. BlockSize ())} src: = Enc. Encrypt ([]byte("Czxichen")) DST: = Enc. Decrypt (SRC) fmt. Println (string(DST))}//Use AES to encrypt text, encrypted text cannot be emptyFunc (a*tobytes) Encrypt (src []byte) (DST []byte) {src =a. padding (src) DST = make ([]byte,Len(SRC)) var index int =0     for Len(SRC) >0{a. CIP. Encrypt (dst[index:index+a. CIP. BlockSize ()], src) index + =a. CIP. BlockSize () src = src[a. CIP. BlockSize ():]}returnDst//Decrypt text with AESFunc (a*tobytes) Decrypt (src []byte) (DST []byte) {if Len(SRC)%a. CIP. BlockSize ()! =0{returnSRC} DST = make ([]byte,Len(SRC)) var index int =0     for Len(SRC) >0{a. CIP. Decrypt (dst[index:index+a. CIP. BlockSize ()], src) index + =a. CIP. BlockSize () src = src[a. CIP. BlockSize ():]}return a. unpadding (DST)}//When encrypting text with AES, the text must be fixed long, i.e. it must be an integer multiple of 16,24,32,Func (a*tobytes) padding (src []byte) (DST []byte) {PDG: =a. CIP. BlockSize ()-Len(SRC)%a. CIP. BlockSize () P: =a. pdgtext[:p DG] p[pdg-1] =byte(PDG)returnAppend (src, p ...)}//Use AES to decrypt text, decrypt deleted padding textFunc (a*tobytes) unpadding (src []byte) (DST []byte) {length:=Len(SRC)if length<=0{returnSRC}returnSrc[:(length-Int (src[length-1]))]}
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.