golang实现加密解密文档

golang实现加密解密文档

package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
)

func main() {
	encodePtr := flag.String("e", "", "File to be encoded")
	decodePtr := flag.String("d", "", "File to be decoded")
	outputPtr := flag.String("o", "", "Output file")
	keyPtr := flag.String("k", "", "Decryption key")
	flag.Parse()

	if *encodePtr != "" {
		key := encryptFile(*encodePtr, *outputPtr)
		fmt.Println("Encryption key:", key)
	} else if *decodePtr != "" && *keyPtr != "" {
		decryptFile(*decodePtr, *keyPtr, *outputPtr)
	} else {
		flag.PrintDefaults()
	}
}

func encryptFile(inputFile, outputFile string) string {
	plaintext, err := ioutil.ReadFile(inputFile)
	if err != nil {
		fmt.Println("Error reading input file:", err)
		return ""
	}

	key := make([]byte, 16)
	_, err = rand.Read(key)
	if err != nil {
		fmt.Println("Error generating random key:", err)
		return ""
	}

	block, err := aes.NewCipher(key)
	if err != nil {
		fmt.Println("Error creating AES cipher:", err)
		return ""
	}

	// Padding the plaintext to a multiple of the block size
	padding := aes.BlockSize - (len(plaintext) % aes.BlockSize)
	if padding > 0 {
		plaintext = append(plaintext, bytes.Repeat([]byte{byte(padding)}, padding)...)
	}

	ciphertext := make([]byte, aes.BlockSize+len(plaintext))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		fmt.Println("Error generating IV:", err)
		return ""
	}

	mode := cipher.NewCBCEncrypter(block, iv)
	mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)

	encoded := base64.StdEncoding.EncodeToString(ciphertext)

	if outputFile != "" {
		err := ioutil.WriteFile(outputFile, []byte(encoded), 0644)
		if err != nil {
			fmt.Println("Error writing to output file:", err)
			return ""
		}
	}

	return base64.StdEncoding.EncodeToString(key)
}

func decryptFile(inputFile, keyString, outputFile string) {
	ciphertext, err := ioutil.ReadFile(inputFile)
	if err != nil {
		fmt.Println("Error reading input file:", err)
		return
	}

	key, err := base64.StdEncoding.DecodeString(keyString)
	if err != nil {
		fmt.Println("Error decoding key:", err)
		return
	}

	block, err := aes.NewCipher(key)
	if err != nil {
		fmt.Println("Error creating AES cipher:", err)
		return
	}

	decoded, err := base64.StdEncoding.DecodeString(string(ciphertext))
	if err != nil {
		fmt.Println("Error decoding Base64:", err)
		return
	}

	if len(decoded) < aes.BlockSize {
		fmt.Println("Ciphertext too short")
		return
	}

	iv := decoded[:aes.BlockSize]
	decoded = decoded[aes.BlockSize:]

	if len(decoded)%aes.BlockSize != 0 {
		fmt.Println("Ciphertext is not a multiple of the block size")
		return
	}

	mode := cipher.NewCBCDecrypter(block, iv)
	mode.CryptBlocks(decoded, decoded)

	// Remove padding
	padding := decoded[len(decoded)-1]
	decoded = decoded[:len(decoded)-int(padding)]

	err = ioutil.WriteFile(outputFile, decoded, 0644)
	if err != nil {
		fmt.Println("Error writing to output file:", err)
		return
	}

	fmt.Println("Decryption successful. Decrypted content written to", outputFile)
}

实现效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/40ef0f7fbafd45568910644eeb61d1d2.png
打包代码

go build -ldflags=" -s -w" -buildvcs=false follow-me.go 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我重来不说话

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值