1. 趣币网首页
  2. 专栏

从零到壹学习密码学第十一讲:分组密码的调用

从零到壹学习密码学为一个系列,一共20讲,包括初识密码学、Hash 函数、对称加密算法、数字签名、椭圆曲线加解密、公钥基础设施( PKI )、零知识证明、随机数等。今天我们将为大家介绍第十一讲:分组密码的调用。

从零到壹学习密码学为一个系列,一共20讲,包括初识密码学、Hash 函数、对称加密算法、数字签名、椭圆曲线加解密、公钥基础设施( PKI )、零知识证明、随机数等,为大家详尽的介绍密码学的学习过程。今天我们将为大家介绍第十一讲:分组密码的调用。话不多说,马上开启我们的密码学学习之旅。

资料获取,添加莉莉微信kongyixueyuan。

孔壹学院
CBC
packagemainimport("bytes""crypto/cipher""crypto/aes""encoding/base64""fmt")funcPKCS7Padding(ciphertext[]byte,blockSizeint)[]byte{padding:=blockSize-len(ciphertext)%blockSizepadtext:=bytes.Repeat([]byte{byte(padding)},padding)returnappend(ciphertext,padtext...)}funcPKCS7UnPadding(origData[]byte)[]byte{length:=len(origData)unpadding:=int(origData[length-1])returnorigData[:(length-unpadding)]}funcAesEncrypt(origData,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err}blockSize:=block.BlockSize()origData=PKCS7Padding(origData,blockSize)blockMode:=cipher.NewCBCEncrypter(block,key[:blockSize])crypted:=make([]byte,len(origData))blockMode.CryptBlocks(crypted,origData)returncrypted,nil}funcAesDecrypt(crypted,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err}blockSize:=block.BlockSize()blockMode:=cipher.NewCBCDecrypter(block,key[:blockSize])origData:=make([]byte,len(crypted))blockMode.CryptBlocks(origData,crypted)origData=PKCS7UnPadding(origData)returnorigData,nil}funcmain(){key:=[]byte("0123456789abcdef")result,err:=AesEncrypt([]byte("helloworld"),key)iferr!=nil{panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData,err:=AesDecrypt(result,key)iferr!=nil{panic(err)}fmt.Println(string(origData))}
packagemainimport(	"io""crypto/cipher"	"crypto/aes"	"crypto/rand""encoding/base64""fmt")funcAesEncrypt(plaintext,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err	}		ciphertext:=make([]byte,aes.BlockSize+len(plaintext))	iv:=ciphertext[:aes.BlockSize]	if_,err:=io.ReadFull(rand.Reader,iv);err!=nil{		panic(err)	}	stream:=cipher.NewCFBEncrypter(block,iv)	stream.XORKeyStream(ciphertext[aes.BlockSize:],plaintext)returnciphertext,nil}funcAesDecrypt(ciphertext,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err}//TheIVneedstobeunique,butnotsecure.Thereforeit'scommonto	//includeitatthebeginningoftheciphertext.	iflen(ciphertext)<aes.BlockSize{		panic("ciphertexttooshort")	}	iv:=ciphertext[:aes.BlockSize]	ciphertext=ciphertext[aes.BlockSize:]	stream:=cipher.NewCFBDecrypter(block,iv)	//XORKeyStreamcanworkin-placeifthetwoargumentsarethesame.	stream.XORKeyStream(ciphertext,ciphertext)returnciphertext,nil}funcmain(){key:=[]byte("0123456789abcdef")result,err:=AesEncrypt([]byte("helloworld"),key)iferr!=nil{panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData,err:=AesDecrypt(result,key)iferr!=nil{panic(err)}fmt.Println(string(origData))}
OFB 模式
packagemainimport(	"io""crypto/cipher"	"crypto/aes"	"crypto/rand""encoding/base64""fmt")funcAesEncrypt(plaintext,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err	}		ciphertext:=make([]byte,aes.BlockSize+len(plaintext))	iv:=ciphertext[:aes.BlockSize]	if_,err:=io.ReadFull(rand.Reader,iv);err!=nil{		panic(err)	}	stream:=cipher.NewOFB(block,iv)	stream.XORKeyStream(ciphertext[aes.BlockSize:],plaintext)returnciphertext,nil}funcAesDecrypt(ciphertext,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err}//TheIVneedstobeunique,butnotsecure.Thereforeit'scommonto	//includeitatthebeginningoftheciphertext.	iv:=ciphertext[:aes.BlockSize]		iflen(ciphertext)<aes.BlockSize{		panic("ciphertexttooshort")	}	plaintext2:=make([]byte,len(ciphertext))	stream:=cipher.NewOFB(block,iv)	stream.XORKeyStream(plaintext2,ciphertext[aes.BlockSize:])returnplaintext2,nil}funcmain(){key:=[]byte("6368616e676520746869732070617374")result,err:=AesEncrypt([]byte("helloworld"),key)iferr!=nil{panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData,err:=AesDecrypt(result,key)iferr!=nil{panic(err)}fmt.Println(string(origData))}
CTR模式
packagemainimport(	"io""crypto/cipher"	"crypto/aes"	"crypto/rand""encoding/base64""fmt")funcAesEncrypt(plaintext,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err	}		ciphertext:=make([]byte,aes.BlockSize+len(plaintext))	iv:=ciphertext[:aes.BlockSize]	if_,err:=io.ReadFull(rand.Reader,iv);err!=nil{		panic(err)	}	stream:=cipher.NewCTR(block,iv)	stream.XORKeyStream(ciphertext[aes.BlockSize:],plaintext)returnciphertext,nil}funcAesDecrypt(ciphertext,key[]byte)([]byte,error){block,err:=aes.NewCipher(key)iferr!=nil{returnnil,err}//TheIVneedstobeunique,butnotsecure.Thereforeit'scommonto	//includeitatthebeginningoftheciphertext.	iv:=ciphertext[:aes.BlockSize]		iflen(ciphertext)<aes.BlockSize{		panic("ciphertexttooshort")	}	plaintext2:=make([]byte,len(ciphertext))	stream:=cipher.NewCTR(block,iv)	stream.XORKeyStream(plaintext2,ciphertext[aes.BlockSize:])returnplaintext2,nil}funcmain(){key:=[]byte("6368616e676520746869732070617374")result,err:=AesEncrypt([]byte("helloworld"),key)iferr!=nil{panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData,err:=AesDecrypt(result,key)iferr!=nil{panic(err)}fmt.Println(string(origData))}

©本文仅代表作者本人观点,与趣币网无关。趣币网对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。若以此作为投资依据,请自行承担全部责任。转载请注明出处:趣币网

发表评论

登录后才能评论

联系我们

13798586780

在线咨询:点击这里给我发消息

邮件:1074760229@qq.com

QQ群:551893940

工作时间:工作日9:00-18:00,节假日休息

QR code