【Go】使用Go语言实现AES CBC No Padding加密和解密


冷雨悄悄停吧
天真的心因为你
那管多风雨天仍和你一起
告诉你我其实多么的想你
其实我我真的爱着你
                     🎵 蒋明周《真的爱着你》


引言

高级加密标准(AES)是一种广泛使用的加密算法。它可以工作在多种模式下,最常用的一种是CBC(Cipher Block Chaining)模式。本文将介绍如何使用Go语言实现AES CBC No Padding加密和解密。

什么是AES CBC No Padding?

AES(Advanced Encryption Standard)是一种对称加密算法,意味着加密和解密使用相同的密钥。CBC(Cipher Block Chaining)是一种工作模式,它将每个明文块与前一个密文块进行异或(XOR)操作后再进行加密。No Padding表示数据块必须是加密算法所要求的固定大小,不足时不会自动填充。

准备工作

在开始编写代码之前,请确保已安装Go语言环境。可以从 Go语言官方网站 下载并安装。

代码实现

下面是完整的代码实现,包括加密和解密函数。

package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"encoding/hex"
	"fmt"
	"log"
)

// AES加密函数
func aesEncrypt(plaintext, key, iv []byte) ([]byte, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	if len(plaintext)%aes.BlockSize != 0 {
		return nil, fmt.Errorf("plaintext is not a multiple of the block size")
	}

	ciphertext := make([]byte, len(plaintext))
	mode := cipher.NewCBCEncrypter(block, iv)
	mode.CryptBlocks(ciphertext, plaintext)

	return ciphertext, nil
}

// AES解密函数
func aesDecrypt(ciphertext, key, iv []byte) ([]byte, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	if len(ciphertext)%aes.BlockSize != 0 {
		return nil, fmt.Errorf("ciphertext is not a multiple of the block size")
	}

	plaintext := make([]byte, len(ciphertext))
	mode := cipher.NewCBCDecrypter(block, iv)
	mode.CryptBlocks(plaintext, ciphertext)

	return plaintext, nil
}

func main() {
	key := []byte("0123456789abcdef") // 16字节的密钥
	iv := []byte("abcdef9876543210")  // 16字节的初始向量
	plaintext := []byte("Hello, AES CBC No Padding!") // 明文

	// 补充明文,使其长度为块大小的倍数
	if len(plaintext)%aes.BlockSize != 0 {
		padding := aes.BlockSize - len(plaintext)%aes.BlockSize
		plaintext = append(plaintext, bytes.Repeat([]byte{byte(padding)}, padding)...)
	}

	fmt.Printf("原始明文: %s\n", plaintext)

	// 加密
	ciphertext, err := aesEncrypt(plaintext, key, iv)
	if err != nil {
		log.Fatalf("加密失败: %v", err)
	}
	fmt.Printf("密文: %s\n", hex.EncodeToString(ciphertext))

	// 解密
	decryptedText, err := aesDecrypt(ciphertext, key, iv)
	if err != nil {
		log.Fatalf("解密失败: %v", err)
	}
	fmt.Printf("解密后的明文: %s\n", decryptedText)
}

代码说明

导入必要的包:我们使用crypto/aes和crypto/cipher包来处理AES加密和解密。

AES加密函数:

创建一个新的AES密码块。
检查明文长度是否为块大小的倍数,如果不是,则返回错误。
使用CBC加密模式进行加密。

AES解密函数:

创建一个新的AES密码块。
检查密文长度是否为块大小的倍数,如果不是,则返回错误。
使用CBC解密模式进行解密。

主函数:

定义密钥和初始向量(IV)。
补充明文,使其长度为块大小的倍数。
执行加密和解密操作,并输出结果。

运行代码

将上述代码保存为main.go,然后在终端中运行:

go run main.go

结论

本文介绍了如何使用Go语言实现AES CBC No Padding加密和解密。我们讨论了AES CBC模式的基本概念,并提供了完整的代码示例。希望这篇文章能帮助你更好地理解AES CBC加密和解密的实现。如果你有任何问题或建议,请在评论中告诉我们。

相关推荐

  1. Go使用Go语言实现AES CBC No Padding加密解密

    2024-06-14 18:18:02       32 阅读
  2. go使用aes加密算法

    2024-06-14 18:18:02       50 阅读
  3. go语言数组使用

    2024-06-14 18:18:02       43 阅读
  4. Go语言-关于 go get go install

    2024-06-14 18:18:02       43 阅读
  5. Go语言GC

    2024-06-14 18:18:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 18:18:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 18:18:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 18:18:02       82 阅读
  4. Python语言-面向对象

    2024-06-14 18:18:02       91 阅读

热门阅读

  1. Redis缓存(笔记三:Redis6新数据类型)

    2024-06-14 18:18:02       27 阅读
  2. junit mockito Dao层

    2024-06-14 18:18:02       32 阅读
  3. 如何对Spring管理bean进行增强

    2024-06-14 18:18:02       36 阅读
  4. cloud compare编译

    2024-06-14 18:18:02       25 阅读
  5. Django Form 组件

    2024-06-14 18:18:02       33 阅读
  6. Scala 入门指南:从零开始的大数据开发

    2024-06-14 18:18:02       30 阅读
  7. Scala入门教程

    2024-06-14 18:18:02       34 阅读
  8. 深度学习 - RNN训练过程推演

    2024-06-14 18:18:02       22 阅读
  9. 构建实时搜索与推荐系统:Elasticsearch与业务结合

    2024-06-14 18:18:02       35 阅读
  10. pytest request.session 保持登录状态

    2024-06-14 18:18:02       27 阅读