GO语音 DES + CBC 加密使用方法 (区块链)

这个是写代码的思路和过程

des特点:
               1. 8字节秘钥,8字节分组
cbc特点:
                1. 需要填充

package main
import (
"fmt"
)
//加密函数 : key:秘钥, plainText :明文
func desCbcEncrypt(key, plainText []byte) ([]byte, error) {
fmt.Println("开始加密,明文:", string(plainText))
return nil, nil
}
//解密函数
func desCbcDecrypt(key, cipherText []byte) ([]byte, error) {
fmt.Println("开始解密,密文:", string(cipherText))
return nil, nil
}
func main() {
key := []byte("1234567887654321") //16字节秘钥
plainText := []byte("你好,昌平!")
cipherData, err := desCbcEncrypt(key, plainText)
if err != nil {
fmt.Println("加密失败:", err)
return
}
fmt.Printf("加密后的数据为, hex :%x\n", cipherData)
fmt.Printf("加密后的数据为, string :%s\n", cipherData)
plainText, err = desCbcDecrypt(key, cipherData)
if err != nil {
fmt.Println("解密失败:", err)return
}
fmt.Printf("解密后的数据为:%s\n", plainText)
}

阶段1:加密满足条件长度的数据(8字节)

package main
import (
"bytes"
"crypto/cipher"
"crypto/des"
"fmt"
)
//des特点:
//1. 8字节秘钥,8字节分组
//
//cbc特点:
//1. 需要填充
//加密函数 : key:秘钥, plainText :明文
func desCbcEncrypt(key, plainText []byte) ([]byte, error) {
fmt.Println("开始加密,明文:", string(plainText))
//第一步:创建密码接口
block, err := des.NewCipher(key)
if err != nil {
fmt.Println("des.NewCipher err:", err)
return nil, err
}
fmt.Println("Block Size:", block.BlockSize()) //8
//创建iv
iv := bytes.Repeat([]byte("1"), block.BlockSize())
//TODO填充数据
//第二步:创建cbc加密分组blockMode := cipher.NewCBCEncrypter(block, iv)
dst := make([]byte, len(plainText))
//第三步:加密
blockMode.CryptBlocks(dst /*密文*/, plainText /*明文*/)
return dst, nil
}
//解密函数
func desCbcDecrypt(key, cipherText []byte) ([]byte, error) {
fmt.Println("开始解密,密文:", string(cipherText))
//第一步:创建密码接口
block, err := des.NewCipher(key)
if err != nil {
fmt.Println("des.NewCipher err:", err)
return nil, err
}
fmt.Println("Block Size:", block.BlockSize()) //8
//创建iv
iv := bytes.Repeat([]byte("1"), block.BlockSize())
//第二步:创建cbc解密分组
blockMode := cipher.NewCBCDecrypter(block, iv)
//<<<<======== 此处不同
dst := make([]byte, len(cipherText))
//第三步:解密
blockMode.CryptBlocks(dst /*明文*/, cipherText /*密文*/)
//去除填充
//TODO
return dst, nil
}
func main() {
key := []byte("12345678")
//8字节秘钥
plainText := []byte("你好,昌平!") //ok
//plainText := []byte("你好,昌平!!") //不ok
cipherData, err := desCbcEncrypt(key, plainText)
if err != nil {
fmt.Println("加密失败:", err)
return
}
fmt.Printf("加密后的数据为, hex :%x\n", cipherData)
fmt.Printf("加密后的数据为, string :%s\n", cipherData)
plainText, err = desCbcDecrypt(key, cipherData)
if err != nil {
fmt.Println("解密失败:", err)
return
}
fmt.Printf("解密后的数据为:%s\n", plainText)
}

阶段2:输入不满足条件的数据(10字节),需要填充
填充函数:

//填充函数
func paddingNumber(src []byte, blockSize int) ([]byte, error) {
fmt.Println("paddingNumber called!")
if src == nil {
return nil, errors.New("填充数据为空!")
}
//src : 25byte,每个分组长度是:8byte 需要填充:7byte
//a. 剩余字节数
leftNumber := len(src) % blockSize //1
//b. 求出需要填充的个数
needNumber := blockSize - leftNumber
//[]byte{7,7,7,7,7,7,7}
newSlice := bytes.Repeat([]byte{byte(needNumber)}, needNumber)
fmt.Println("newSlice:", newSlice)
//将填好的数据追加到src后面
src = append(src, newSlice...)
return src, nil
}

加密之前调用:

效果:

去除填充:

//去除填充函数
func unPaddingNumber(src []byte) []byte {
fmt.Println("unPaddingNumber called!")
//1. 获取最后一个字符
lastByte := src[len(src)-1]
num := int(lastByte)
//2. 截取原文
return src[0 : len(src)-num]
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值