自学内容网 自学内容网

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

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!