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)
}

实现效果:
golang实现加密解密文档_第1张图片
打包代码

go build -ldflags=" -s -w" -buildvcs=false follow-me.go 

你可能感兴趣的:(golang,开发语言,后端)