golang的AES加密和解密的三种模式实现(CBC/ECB/CFB)「建议收藏」

golang的AES加密和解密的三种模式实现(CBC/ECB/CFB)packagemainimport( "bytes" "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/base64" "encoding/hex"

大家好,又见面了,我是你们的朋友全栈君。

golang的AES加密和解密的三种模式实现(CBC/ECB/CFB)

package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"encoding/hex"
	"io"
	"log"
)

func main() {
	origData := []byte("Hello World") // 待加密的数据
	key := []byte("ABCDEFGHIJKLMNOP") // 加密的密钥
	log.Println("原文:", string(origData))

	log.Println("------------------ CBC模式 --------------------")
	encrypted := AesEncryptCBC(origData, key)
	log.Println("密文(hex):", hex.EncodeToString(encrypted))
	log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
	decrypted := AesDecryptCBC(encrypted, key)
	log.Println("解密结果:", string(decrypted))

	log.Println("------------------ ECB模式 --------------------")
	encrypted = AesEncryptECB(origData, key)
	log.Println("密文(hex):", hex.EncodeToString(encrypted))
	log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
	decrypted = AesDecryptECB(encrypted, key)
	log.Println("解密结果:", string(decrypted))

	log.Println("------------------ CFB模式 --------------------")
	encrypted = AesEncryptCFB(origData, key)
	log.Println("密文(hex):", hex.EncodeToString(encrypted))
	log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
	decrypted = AesDecryptCFB(encrypted, key)
	log.Println("解密结果:", string(decrypted))
}

// =================== CBC ======================
func AesEncryptCBC(origData []byte, key []byte) (encrypted []byte) {
	// 分组秘钥
	// NewCipher该函数限制了输入k的长度必须为16, 24或者32
	block, _ := aes.NewCipher(key)
	blockSize := block.BlockSize()                              // 获取秘钥块的长度
	origData = pkcs5Padding(origData, blockSize)                // 补全码
	blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) // 加密模式
	encrypted = make([]byte, len(origData))                     // 创建数组
	blockMode.CryptBlocks(encrypted, origData)                  // 加密
	return encrypted
}
func AesDecryptCBC(encrypted []byte, key []byte) (decrypted []byte) {
	block, _ := aes.NewCipher(key)                              // 分组秘钥
	blockSize := block.BlockSize()                              // 获取秘钥块的长度
	blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) // 加密模式
	decrypted = make([]byte, len(encrypted))                    // 创建数组
	blockMode.CryptBlocks(decrypted, encrypted)                 // 解密
	decrypted = pkcs5UnPadding(decrypted)                       // 去除补全码
	return decrypted
}
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padtext...)
}
func pkcs5UnPadding(origData []byte) []byte {
	length := len(origData)
	unpadding := int(origData[length-1])
	return origData[:(length - unpadding)]
}

// =================== ECB ======================
func AesEncryptECB(origData []byte, key []byte) (encrypted []byte) {
	cipher, _ := aes.NewCipher(generateKey(key))
	length := (len(origData) + aes.BlockSize) / aes.BlockSize
	plain := make([]byte, length*aes.BlockSize)
	copy(plain, origData)
	pad := byte(len(plain) - len(origData))
	for i := len(origData); i < len(plain); i++ {
		plain[i] = pad
	}
	encrypted = make([]byte, len(plain))
	// 分组分块加密
	for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
		cipher.Encrypt(encrypted[bs:be], plain[bs:be])
	}

	return encrypted
}
func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) {
	cipher, _ := aes.NewCipher(generateKey(key))
	decrypted = make([]byte, len(encrypted))
	//
	for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
		cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
	}

	trim := 0
	if len(decrypted) > 0 {
		trim = len(decrypted) - int(decrypted[len(decrypted)-1])
	}

	return decrypted[:trim]
}
func generateKey(key []byte) (genKey []byte) {
	genKey = make([]byte, 16)
	copy(genKey, key)
	for i := 16; i < len(key); {
		for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
			genKey[j] ^= key[i]
		}
	}
	return genKey
}

// =================== CFB ======================
func AesEncryptCFB(origData []byte, key []byte) (encrypted []byte) {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}
	encrypted = make([]byte, aes.BlockSize+len(origData))
	iv := encrypted[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}
	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(encrypted[aes.BlockSize:], origData)
	return encrypted
}
func AesDecryptCFB(encrypted []byte, key []byte) (decrypted []byte) {
	block, _ := aes.NewCipher(key)
	if len(encrypted) < aes.BlockSize {
		panic("ciphertext too short")
	}
	iv := encrypted[:aes.BlockSize]
	encrypted = encrypted[aes.BlockSize:]

	stream := cipher.NewCFBDecrypter(block, iv)
	stream.XORKeyStream(encrypted, encrypted)
	return encrypted
}

输出结果:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/128363.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python取整符号_python 取整「建议收藏」

    广告关闭腾讯云11.11云上盛惠,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元!(1)向下取整向下取整很简单,直接使用int()函数即可,如下代码(python2.7.5idle)a=3.75int(a)3(2)四舍五入第二种就是对数字进行四舍五入,具体的看下面的代码:a=3.25;b=3.75round(a);round(b)3.0…

    2022年4月18日
    125
  • 关于MIPI协议(一)——物理层D-PHY总结

    关于MIPI协议(一)——物理层D-PHY总结关于移动端mipi协议的一些总结

    2022年5月30日
    157
  • Android之查看外部依赖jar的源码_android private libralies does not allow modifications to source

    在Eclipse中开发android的应用程序时,有时想查看函数的内部实现,但是当在函数上点击ctrl和鼠标左键的时候,往往出现如下提示: Class File EditorSource not foundThe JAR of this class file belongs to container ‘Android’  which does not allow modifications to

    2022年3月11日
    43
  • ubuntu镜像下载教程_Ubuntu镜像

    ubuntu镜像下载教程_Ubuntu镜像在众多的linux操作系统中,Ubuntu(乌班图)是目前主流的linux操作系统。而绝大部分新手网友要接触linux或使用linux操作系统,当然是首选Ubuntu(乌班图)linux操作系统。因为Ubuntu系统绝大部分是图形化操作,很少会使用到命令,同时在linux操作系统中支持Ubuntu系统的软件、游戏也最多。Ubuntu官方网站:http://www.ubuntu.com/downl

    2025年6月26日
    0
  • 吞噬星空是鸿蒙三部曲吗,星辰变吞噬星空盘龙三部有什么关系

    吞噬星空是鸿蒙三部曲吗,星辰变吞噬星空盘龙三部有什么关系三部小说之间的关系就说这三部小说都属于鸿蒙三部曲,其中的各个宇宙之间都有一定的联系。《星辰变》是在红袖网上连载的奇幻修真小说,作者是我吃西红柿。小说以一个庞大的修真世界为背景,讲述了主角秦羽不甘于没落、努力修炼跨越先天不足的劣势,一步步踏上强者之路,最终成为鸿蒙掌控者的故事。《吞噬星空》,是一部东方科幻类型的网络小说,小说签约授权首发连载于红袖网。小说已经完本,作者是我吃西红柿。小说主要讲述了地球…

    2022年6月16日
    119
  • Lunix_UNIX/LINUX

    Lunix_UNIX/LINUXlinux关机和重启命令(转)Lunix/Unix1.shutdownshutdown命令用于安全关闭Linux系统。shutdown命令的部分参数如下:[-t]指定在多长时间之后关闭系统[-r]重启系统[-k]并不真正关机,只是给每个登录用户发送警告信号[-h]关闭系统(halt)shutdown命令的工作实质是给ini…

    2022年10月4日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号