日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

分组密码的调用

發布時間:2025/3/21 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分组密码的调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

分組密碼的調用

CBC

package mainimport ("bytes""crypto/cipher""crypto/aes""encoding/base64""fmt" )func PKCS7Padding(ciphertext []byte, blockSize int) []byte {padding := blockSize - len(ciphertext) % blockSizepadtext := bytes.Repeat([]byte{byte(padding)}, padding)return append(ciphertext, padtext...) }func PKCS7UnPadding(origData []byte) []byte {length := len(origData)unpadding := int(origData[length-1])return origData[:(length - unpadding)] }func AesEncrypt(origData, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, err}blockSize := block.BlockSize()origData = PKCS7Padding(origData, blockSize)blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])crypted := make([]byte, len(origData))blockMode.CryptBlocks(crypted, origData)return crypted, nil }func AesDecrypt(crypted, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, err}blockSize := block.BlockSize()blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])origData := make([]byte, len(crypted))blockMode.CryptBlocks(origData, crypted)origData = PKCS7UnPadding(origData)return origData, nil }func main() {key := []byte("0123456789abcdef")result, err := AesEncrypt([]byte("hello world"), key)if err != nil {panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData, err := AesDecrypt(result, key)if err != nil {panic(err)}fmt.Println(string(origData)) }

CFB 模式

package mainimport ("io""crypto/cipher""crypto/aes""crypto/rand""encoding/base64""fmt" )func AesEncrypt(plaintext, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, 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)return ciphertext, nil }func AesDecrypt(ciphertext, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, err}// The IV needs to be unique, but not secure. Therefore it's common to// include it at the beginning of the ciphertext.if len(ciphertext) < aes.BlockSize {panic("ciphertext too short")}iv := ciphertext[:aes.BlockSize]ciphertext = ciphertext[aes.BlockSize:]stream := cipher.NewCFBDecrypter(block, iv)// XORKeyStream can work in-place if the two arguments are the same.stream.XORKeyStream(ciphertext, ciphertext)return ciphertext, nil }func main() {key := []byte("0123456789abcdef")result, err := AesEncrypt([]byte("hello world"), key)if err != nil {panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData, err := AesDecrypt(result, key)if err != nil {panic(err)}fmt.Println(string(origData)) }

OFB 模式

package mainimport ("io""crypto/cipher""crypto/aes""crypto/rand""encoding/base64""fmt" )func AesEncrypt(plaintext, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, 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)return ciphertext, nil }func AesDecrypt(ciphertext, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, err}// The IV needs to be unique, but not secure. Therefore it's common to// include it at the beginning of the ciphertext.iv := ciphertext[:aes.BlockSize]if len(ciphertext) < aes.BlockSize {panic("ciphertext too short")}plaintext2 := make([]byte, len(ciphertext))stream := cipher.NewOFB(block, iv)stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])return plaintext2, nil }func main() {key := []byte("6368616e676520746869732070617374")result, err := AesEncrypt([]byte("hello world"), key)if err != nil {panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData, err := AesDecrypt(result, key)if err != nil {panic(err)}fmt.Println(string(origData)) }

CTR模式

package mainimport ("io""crypto/cipher""crypto/aes""crypto/rand""encoding/base64""fmt" )func AesEncrypt(plaintext, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, 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)return ciphertext, nil }func AesDecrypt(ciphertext, key []byte) ([]byte, error) {block, err := aes.NewCipher(key)if err != nil {return nil, err}// The IV needs to be unique, but not secure. Therefore it's common to// include it at the beginning of the ciphertext.iv := ciphertext[:aes.BlockSize]if len(ciphertext) < aes.BlockSize {panic("ciphertext too short")}plaintext2 := make([]byte, len(ciphertext))stream := cipher.NewCTR(block, iv)stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])return plaintext2, nil }func main() {key := []byte("6368616e676520746869732070617374")result, err := AesEncrypt([]byte("hello world"), key)if err != nil {panic(err)}fmt.Println(base64.StdEncoding.EncodeToString(result))origData, err := AesDecrypt(result, key)if err != nil {panic(err)}fmt.Println(string(origData)) }

總結

以上是生活随笔為你收集整理的分组密码的调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。