首次提交
This commit is contained in:
parent
4f7e4ccb9a
commit
a10b5bfbf9
116
main.go
Normal file
116
main.go
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"goED/util"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readFileToStr(path string) string {
|
||||||
|
content, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("read file failed, err:", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func genFile(str, path string) {
|
||||||
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("open file failed, err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
file.Write([]byte(str)) //写入字节切片数据
|
||||||
|
//file.WriteString("hello go") //直接写入字符串数据
|
||||||
|
}
|
||||||
|
|
||||||
|
func test() {
|
||||||
|
//content, err := ioutil.ReadFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
||||||
|
strContent := readFileToStr("C:\\METSIM\\example\\gascleaning_1.5_30.sfw")
|
||||||
|
//fmt.Println(strContent)
|
||||||
|
Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
|
||||||
|
//fmt.Printf("Encrypt: %v\n", Encrypt)
|
||||||
|
Decrypt, _ := util.AesEcpt.AesBase64Decrypt(Encrypt)
|
||||||
|
//fmt.Printf("Decrypt: %v\n", Decrypt)
|
||||||
|
fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
|
||||||
|
}
|
||||||
|
func testGenFile(path, enPath, dePath string) {
|
||||||
|
strContent := readFileToStr(path)
|
||||||
|
Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
|
||||||
|
genFile(Encrypt, enPath)
|
||||||
|
//fmt.Printf("Encrypt: %v\n", Encrypt)
|
||||||
|
Decrypt, _ := util.AesEcpt.AesBase64Decrypt(Encrypt)
|
||||||
|
genFile(Decrypt, dePath)
|
||||||
|
//fmt.Printf("Decrypt: %v\n", Decrypt)
|
||||||
|
fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
|
||||||
|
}
|
||||||
|
|
||||||
|
func enFile(path string) {
|
||||||
|
enFileToOutPath(path, path)
|
||||||
|
}
|
||||||
|
func enFileToOutPath(path, outPath string) {
|
||||||
|
strContent := readFileToStr(path)
|
||||||
|
Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
|
||||||
|
genFile(Encrypt, outPath)
|
||||||
|
}
|
||||||
|
func deFile(path string) {
|
||||||
|
deFileToOutPath(path, path)
|
||||||
|
}
|
||||||
|
func deFileToOutPath(path string, outPath string) {
|
||||||
|
strContent := readFileToStr(path)
|
||||||
|
Decrypt, _ := util.AesEcpt.AesBase64Decrypt(strContent)
|
||||||
|
genFile(Decrypt, outPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
var mode = flag.String("mode", "de", "加密或者解密 en de")
|
||||||
|
var path = flag.String("path", "", "文件路径")
|
||||||
|
var outPath = flag.String("outPath", "", "指定输出文件路径")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
//fmt.Println(*mode)
|
||||||
|
//fmt.Println(*path)
|
||||||
|
var out string = *path
|
||||||
|
var modeName string
|
||||||
|
if len(*outPath) != 0 {
|
||||||
|
out = *outPath
|
||||||
|
}
|
||||||
|
if len(*path) == 0 {
|
||||||
|
fmt.Printf("path 不能为空")
|
||||||
|
} else if *mode != "en" && *mode != "de" {
|
||||||
|
fmt.Printf("mode 只能是 en 或者 de")
|
||||||
|
} else if *mode == "en" {
|
||||||
|
modeName = "加密"
|
||||||
|
enFileToOutPath(*path, out)
|
||||||
|
} else if *mode == "de" {
|
||||||
|
modeName = "解密"
|
||||||
|
deFileToOutPath(*path, out)
|
||||||
|
}
|
||||||
|
fmt.Println(modeName + "成功,被" + modeName + "文件是在: " + *path + " " + modeName + "后文件生产在:" + out)
|
||||||
|
//test()
|
||||||
|
// testGenFile("C:\\METSIM\\example\\gascleaning_1.5_30.sfw",
|
||||||
|
// "C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw.EN",
|
||||||
|
// "C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw")
|
||||||
|
// enFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
||||||
|
// deFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
||||||
|
|
||||||
|
// cmd := exec.Command("cmd", "/C", "C:\\METSIM\\METSIM.exe",
|
||||||
|
// "MOD=C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw", "SIL=1")
|
||||||
|
// var stdout, stderr bytes.Buffer
|
||||||
|
// cmd.Stdout = &stdout
|
||||||
|
// cmd.Stderr = &stderr
|
||||||
|
// err := cmd.Run()
|
||||||
|
// outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
|
||||||
|
// fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatalf("cmd.Run() failed with %s\n", err)
|
||||||
|
// }
|
||||||
|
// cmd := exec.Command("cmd", "/C", "C:\\METSIM\\METSIM.exe",
|
||||||
|
// "MOD=C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw", "SIL=1")
|
||||||
|
// cmd.Start()
|
||||||
|
|
||||||
|
}
|
66
util/EncodeAndDecode.go
Normal file
66
util/EncodeAndDecode.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AesEncrypter struct {
|
||||||
|
key []byte
|
||||||
|
iv []byte
|
||||||
|
block cipher.Block
|
||||||
|
}
|
||||||
|
|
||||||
|
var AesEcpt AesEncrypter
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
AesEcpt.key = []byte("bGcGfWb3Kg2s4gcG")
|
||||||
|
AesEcpt.iv = []byte("aebksHkG4jAEk2Ag")
|
||||||
|
var err error
|
||||||
|
AesEcpt.block, err = aes.NewCipher(AesEcpt.key)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加密
|
||||||
|
func (a *AesEncrypter) AesBase64Encrypt(in string) (string, error) {
|
||||||
|
origData := []byte(in)
|
||||||
|
origData = PKCS5Padding(origData, a.block.BlockSize())
|
||||||
|
crypted := make([]byte, len(origData))
|
||||||
|
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
|
||||||
|
bm := cipher.NewCBCEncrypter(a.block, a.iv)
|
||||||
|
bm.CryptBlocks(crypted, origData)
|
||||||
|
var b = base64.StdEncoding.EncodeToString(crypted)
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解密
|
||||||
|
func (a *AesEncrypter) AesBase64Decrypt(b string) (string, error) {
|
||||||
|
crypted, err := base64.StdEncoding.DecodeString(b)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("err: %v\n", err)
|
||||||
|
}
|
||||||
|
origData := make([]byte, len(crypted))
|
||||||
|
bm := cipher.NewCBCDecrypter(a.block, a.iv)
|
||||||
|
bm.CryptBlocks(origData, crypted)
|
||||||
|
origData = PKCS5UnPadding(origData)
|
||||||
|
var out = string(origData)
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
||||||
|
padding := blockSize - len(ciphertext)%blockSize
|
||||||
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||||
|
return append(ciphertext, padtext...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PKCS5UnPadding(origData []byte) []byte {
|
||||||
|
length := len(origData)
|
||||||
|
// 去掉最后一个字节 unpadding 次
|
||||||
|
unpadding := int(origData[length-1])
|
||||||
|
return origData[:(length - unpadding)]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user