golang GoLang使用AES加密将字符串加密到base64,反之亦然。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang GoLang使用AES加密将字符串加密到base64,反之亦然。相关的知识,希望对你有一定的参考价值。

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
)

func main() {
	originalText := "encrypt this golang"
	fmt.Println(originalText)

	key := []byte("example key 1234")

	// encrypt value to base64
	cryptoText := encrypt(key, originalText)
	fmt.Println(cryptoText)

	// encrypt base64 crypto to original value
	text := decrypt(key, cryptoText)
	fmt.Printf(text)
}

// encrypt string to base64 crypto using AES
func encrypt(key []byte, text string) string {
	// key := []byte(keyText)
	plaintext := []byte(text)

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	// The IV needs to be unique, but not secure. Therefore it's common to
	// include it at the beginning of the ciphertext.
	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)

	// convert to base64
	return base64.URLEncoding.EncodeToString(ciphertext)
}

// decrypt from base64 to decrypted string
func decrypt(key []byte, cryptoText string) string {
	ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(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 fmt.Sprintf("%s", ciphertext)
}

golang:AES加密解密

golang:AES加密解密代码示例:

package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"fmt"
)

func padding(src []byte, blocksize int) []byte 
	padnum := blocksize - len(src)%blocksize
	pad := bytes.Repeat([]bytebyte(padnum), padnum)
	return append(src, pad...)


func unPadding(src []byte) []byte 
	n := len(src)
	unPadNum := int(src[n-1])
	if unPadNum > n 
		return nil
	
	return src[:n-unPadNum]


func EncryptAES(src []byte, key []byte) []byte 
	block, err := aes.NewCipher(key)
	if err != nil 
		return nil
	
	src = padding(src, block.BlockSize())
	blockMode := cipher.NewCBCEncrypter(block, key)
	blockMode.CryptBlocks(src, src)
	return src


func DecryptAES(src []byte, key []byte) []byte 
	block, err := aes.NewCipher(key)
	if err != nil 
		return nil
	
	blockMode := cipher.NewCBCDecrypter(block, key)
	blockMode.CryptBlocks(src, src)
	src = unPadding(src)
	return src


func main() 
	raw := []byte("guoshuaijie")
	key := []byte("huyanyan87654321")
	//key2:=[]byte("guoshuai87654321")
	encryptByte := EncryptAES(raw, key)
	if encryptByte != nil 
		decryptByte := DecryptAES(encryptByte, key)
		if decryptByte != nil 
			fmt.Println(string(decryptByte))
		
	

输出结果:

guoshuaijie

以上是关于golang GoLang使用AES加密将字符串加密到base64,反之亦然。的主要内容,如果未能解决你的问题,请参考以下文章

golang GoLang使用AES加密将字符串加密到base64,反之亦然。

golang 在golang中使用AES256 GCM加密文本

golang:AES加密解密

Golang里的AES加密解密

非对称加密的RSA算法如何通过golang来实现?

golang 加密解密