ZBLOG

golang 如何给任意长度的字符串进行加解密操作,并提供相关demo

Go语言中可以使用标准库中的crypto包进行加解密操作。具体实现方法如下:

  1. 导入crypto包和相关子包:
import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/hex"
    "io"
)
  1. 定义用于加解密的key和iv(初始化向量):
var key = []byte("0123456789ABCDEF") // 16位或32位的key
var iv = []byte("ABCDEF0123456789") // 16位iv
  1. 定义加密函数encrypt:
func encrypt(plaintext string) (string, error) {
    plaintextBytes := []byte(plaintext)

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

    ciphertext := make([]byte, aes.BlockSize+len(plaintextBytes))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
        return "", err
    }

    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintextBytes)

    return hex.EncodeToString(ciphertext), nil
}
  1. 定义解密函数decrypt:
func decrypt(ciphertext string) (string, error) {
    ciphertextBytes, err := hex.DecodeString(ciphertext)
    if err != nil {
        return "", err
    }

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

    if len(ciphertextBytes) < aes.BlockSize {
        return "", errors.New("ciphertext too short")
    }

    iv := ciphertextBytes[:aes.BlockSize]
    ciphertextBytes = ciphertextBytes[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)
    stream.XORKeyStream(ciphertextBytes, ciphertextBytes)

    return string(ciphertextBytes), nil
}
  1. 测试加解密函数:
func main() {
    plaintext := "Hello World!"
    
    ciphertext, err := encrypt(plaintext)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("ciphertext:", ciphertext)

    decryptedText, err := decrypt(ciphertext)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("decrypted text:", decryptedText)
}

完整的示例代码如下:

package main

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

var key = []byte("0123456789ABCDEF") // 16位或32位的key
var iv = []byte("ABCDEF0123456789") // 16位iv

func encrypt(plaintext string) (string, error) {
    plaintextBytes := []byte(plaintext)

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

    ciphertext := make([]byte, aes.BlockSize+len(plaintextBytes))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
        return "", err
    }

    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintextBytes)

    return hex.EncodeToString(ciphertext), nil
}

func decrypt(ciphertext string) (string, error) {
    ciphertextBytes, err := hex.DecodeString(ciphertext)
    if err != nil {
        return "", err
    }

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

    if len(ciphertextBytes) < aes.BlockSize {
        return "", errors.New("ciphertext too short")
    }

    iv := ciphertextBytes[:aes.BlockSize]
    ciphertextBytes = ciphertextBytes[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)
    stream.XORKeyStream(ciphertextBytes, ciphertextBytes)

    return string(ciphertextBytes), nil
}

func main() {
    plaintext := "Hello World!"
    
    ciphertext, err := encrypt(plaintext)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("ciphertext:", ciphertext)

    decryptedText, err := decrypt(ciphertext)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("decrypted text:", decryptedText)
}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=1247

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

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

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?