使用go实现Aes加解密

package encrypt

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

const (
	MYKEY = "oingtx47safd756s" //十六字节密匙
	IV    = "mhytanglki872345" //CBC模式的初始化向量:与key等长:十六字节
)

// 注:输入输出都直接是字符串

//使用aes进行对称加密
func Aes_Encrypt(ss string) string {
	src := []byte(ss)
	//1. 创建并返回一个使用DES算法的cipher.Block接口
	block, err := aes.NewCipher([]byte(MYKEY))
	if err != nil {
		panic(err)
	}
	//2. 对最后一个明文分组进行数据填充
	src = paddingBytes(src, block.BlockSize())
	//3. 创建一个密码分组为链接模式的,底层使用DES加密的BlockMode接口
	cbcDecrypter := cipher.NewCBCEncrypter(block, []byte(IV))
	//4. 加密连续的数据并返回
	dst := make([]byte, len(src))
	cbcDecrypter.CryptBlocks(dst, src)

	return hex.EncodeToString(dst)
}

//使用aes进行解密
func Aes_Decrypt(ss string) string {
	src, _ := hex.DecodeString(ss)
	//1. 创建并返回一个使用DES算法的cipher.Block接口
	block, err := aes.NewCipher([]byte(MYKEY))
	if err != nil {
		panic(err)
	}
	//2. 创建一个密码分组为链接模式的,底层使用DES解密的BlockMode接口
	cbcDecrypter := cipher.NewCBCDecrypter(block, []byte(IV))
	//3. 数据块解密
	dst := make([]byte, len(src))
	cbcDecrypter.CryptBlocks(dst, src)
	//4. 去掉最后一组填充数据
	newBytes := unPaddingBytes(dst)
	return string(newBytes)
}

//填充明文最后一个分组工具方法
//src - 原始数据
//blockSize - 每个分组的数据长度
func paddingBytes(src []byte, blockSize int) []byte {
	//1.求出最后一个分组要填充多个字节
	padding := blockSize - len(src)%blockSize
	//2.创建新的切片,切片的字节数为填充的字节数,并初始化化,每个字节的值为填充的字节数
	padBytes := bytes.Repeat([]byte{byte(padding)}, padding)
	//3.将创建出的新切片和原始数据进行连接
	newBytes := append(src, padBytes...)

	//4.返回新的字符串
	return newBytes
}

//删除密文末尾分组填充的工具方法
func unPaddingBytes(src []byte) []byte {
	//1.求出要处理的切片的长度
	l := len(src)
	//2.取出最后一个字符,得到其整型值
	n := int(src[l-1])

	//3.将切片末尾的number个字节删除
	return src[:l-n]
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请注意,由于安全性原因,我不能提供完整的代码示例,但我可以指导您如何使用cgo来调用OpenSSL库以实现AES加密解密。以下是一个简单的示例代码,仅供参考: ```go package main /* #include <openssl/aes.h> #include <string.h> // 将Go的[]byte类型转换为C中的unsigned char数组类型 void byteToCharArray(unsigned char* dest, const unsigned char* src, int len) { memcpy(dest, src, len); } // 使用OpenSSL库进行AES加密 void encryptAES(const unsigned char* key, const unsigned char* plaintext, unsigned char* ciphertext) { AES_KEY aesKey; AES_set_encrypt_key(key, 128, &aesKey); AES_encrypt(plaintext, ciphertext, &aesKey); } // 使用OpenSSL库进行AES解密 void decryptAES(const unsigned char* key, const unsigned char* ciphertext, unsigned char* plaintext) { AES_KEY aesKey; AES_set_decrypt_key(key, 128, &aesKey); AES_decrypt(ciphertext, plaintext, &aesKey); } */ import "C" import ( "fmt" "unsafe" ) func main() { key := []byte("0123456789abcdef") plaintext := []byte("Hello, World!") // 将Go的[]byte类型转换为C中的unsigned char数组类型 cKey := (*C.uchar)(unsafe.Pointer(&key[0])) cPlaintext := (*C.uchar)(unsafe.Pointer(&plaintext[0])) ciphertext := make([]byte, len(plaintext)) cCiphertext := (*C.uchar)(unsafe.Pointer(&ciphertext[0])) // 调用C函数进行AES加密 C.encryptAES(cKey, cPlaintext, cCiphertext) fmt.Printf("Ciphertext: %x\n", ciphertext) decryptedPlaintext := make([]byte, len(plaintext)) cDecryptedPlaintext := (*C.uchar)(unsafe.Pointer(&decryptedPlaintext[0])) // 调用C函数进行AES解密 C.decryptAES(cKey, cCiphertext, cDecryptedPlaintext) fmt.Printf("Decrypted plaintext: %s\n", decryptedPlaintext) } ``` 请确保在您的系统上安装了OpenSSL库,以及Go语言的cgo工具。此示例代码演示了如何将Go的[]byte类型转换为C中的unsigned char数组类型,并调用C函数进行AES加密和解密。请注意,此示例没有进行错误处理和安全性检查,实际使用时应该添加适当的错误处理和安全性措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值