DES_ECB加密解密 Golang

package test

import (
	"bytes"
	"crypto/des"
	"encoding/hex"
	"fmt"
	_ "jiami/routers"
	"testing"
)

func TestJiaMi(t *testing.T) {

	key := "1234rfvc"
	input := "lisi123"
	ecb := EncryptDES_ECB(input, key)
	fmt.Println(ecb)
	desEcb := DecryptDES_ECB(ecb, key)
	fmt.Println(desEcb)
}

//ECB加密
func EncryptDES_ECB(src, key string) string {
	data := []byte(src)
	keyByte := []byte(key)
	block, err := des.NewCipher(keyByte)
	if err != nil {
		panic(err)
	}
	bs := block.BlockSize()
	//对明文数据进行补码
	data = PKCS5Padding(data, bs)
	if len(data)%bs != 0 {
		panic("Need a multiple of the blocksize")
	}
	out := make([]byte, len(data))
	dst := out
	for len(data) > 0 {
		//对明文按照blocksize进行分块加密
		//必要时可以使用go关键字进行并行加密
		block.Encrypt(dst, data[:bs])
		data = data[bs:]
		dst = dst[bs:]
	}
	return fmt.Sprintf("%X", out)
}

//ECB解密
func DecryptDES_ECB(src, key string) string {
	data, err := hex.DecodeString(src)
	if err != nil {
		panic(err)
	}
	keyByte := []byte(key)
	block, err := des.NewCipher(keyByte)
	if err != nil {
		panic(err)
	}
	bs := block.BlockSize()
	if len(data)%bs != 0 {
		panic("crypto/cipher: input not full blocks")
	}
	out := make([]byte, len(data))
	dst := out
	for len(data) > 0 {
		block.Decrypt(dst, data[:bs])
		data = data[bs:]
		dst = dst[bs:]
	}
	out = PKCS5UnPadding(out)
	return string(out)
}

//明文补码算法
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)]
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用golang传输post数据进行加密解密可以通过以下步骤实现: 1. 创建一个公钥和私钥来进行加密和解密数据。 2. 在发送数据之前,先将其加密。可以使用AES、DES或RSA算法来加密数据。 3. 将加密的数据转换成Base64编码格式,然后发送到接收方。 4. 接收方接收到数据后,把Base64格式的数据解码。 5. 使用相同的密钥和算法来解密数据。 以下是一个使用AES算法进行加密解密数据的示例代码: ``` package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := "1234567890123456" plaintext := []byte("Hello World") // 加密 block, err := aes.NewCipher([]byte(key)) if err != nil { panic(err) } ciphertext := make([]byte, aes.BlockSize+len(plaintext)) iv := ciphertext[:aes.BlockSize] if _, err := rand.Read(iv); err != nil { panic(err) } mode := cipher.NewCBCEncrypter(block, iv) mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext) fmt.Printf("加密后的数据: %s\n", base64.StdEncoding.EncodeToString(ciphertext)) // 解密 ciphertext, err = base64.StdEncoding.DecodeString("LksRIe+I7LVEw/sCh3qyQQ==") if err != nil { panic(err) } iv = ciphertext[:aes.BlockSize] ciphertext = ciphertext[aes.BlockSize:] mode = cipher.NewCBCDecrypter(block, iv) mode.CryptBlocks(ciphertext, ciphertext) fmt.Printf("解密后的数据: %s\n", ciphertext) } ``` 注意,该示例代码中使用的AES算法是对称加密算法,因此加密和解密使用的是相同的密钥。在实际应用中,需要使用公钥和私钥来实现非对称加密,以确保传输的数据更加安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值