以下是Golang中在Windows上使用AES128解密的示例代码:

package main

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

func main() {
    key := []byte("1234567890123456") // 16字节密钥
    ciphertext, err := base64.StdEncoding.DecodeString("base64编码的密文")
    if err != nil {
        panic(err)
    }

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

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

    mode := cipher.NewCBCDecrypter(block, iv)
    mode.CryptBlocks(ciphertext, ciphertext)

    plaintext := string(ciphertext)
    fmt.Println(plaintext)
}

其中,需要注意的是:

  • 密钥必须是16字节长度;
  • 密文必须以16字节的IV向量开头;
  • CBC模式解密时,需要先将密文分离出IV向量。
  • 解密过程中,需要使用mode.CryptBlocks()方法对密文进行解密
golang Windows aes128解密

原文地址: https://www.cveoy.top/t/topic/emEz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录