本文介绍: 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 

原文地址:https://blog.csdn.net/qq_19623861/article/details/135482357

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_54086.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注