一種利用編譯型語言保護Python原始碼方法
在使用Python 開發一段時間發現Python有很多優點,但是Python是解釋型語言,Python專案的部署時原始碼是裸露的,針對這個問題現在也有幾種解決方案,在使用以上幾種解決方案保護Python原始碼時不太方便和安全,所以提出一種利用編譯型語言保護Python原始碼方法,
思考程序:
- 計劃使用純Python代碼利密碼學保護原始碼,隨著代碼的撰寫發現無論如何利用對稱加密、非對稱加密和Hash演算法都需要一個入口代碼,別人可以利用入口代碼一步步的進行破解,(論證無法解決問題)
- 因為之前寫過C/C++,考慮編譯型語言為什么不需要保護原始碼,編譯型語言的可執行檔案是二進制檔案,很難進行反編譯,隨著這個思路能不能利用編譯型語言的保密性對Python原始碼進行保護,
- 由于Python代碼在運行時會被加載到記憶體,當程式運行成功是磁盤上的Python原始碼可以不存在,
- 利用Go將Python原始碼加密,使用時執行Go的二進制執行檔案進行解密,Python代碼啟動成功后洗掉解密后的Python原始碼,只保留加密后的Python原始碼,
原始碼在Github上:(https://github.com/wugsh/simple_tools/blob/main/Python_code_protection.go),
package main
/*
* Using Go binary file and encryption method to prevent Python code leakage.
* Encrypting Python files --> Decrypting Python files --> Runing Python files --> Delete Decrypted Python files
* Only Go binary and Encrypted Python files are left at run time and stop time.
*/
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"crypto/cipher"
"crypto/des"
"crypto/sha256"
"encoding/base64"
"syscall"
"time"
)
//File encryption
func EncryptFile(fileName string, writfileName string, key []byte) (int, error) {
file, err:=os.Open(fileName)
writfile, err1:=os.Create(writfileName)
if err!=nil {
fmt.Println("File not found")
os.Exit(0)
}
if err1!=nil {
fmt.Println("File not found")
os.Exit(0)
}
defer file.Close()
defer writfile.Close()
//Read file contents
plain,_:=ioutil.ReadAll(file)
//Create block
block,_:=des.NewTripleDESCipher(key)
EncryptMode:=cipher.NewCBCEncrypter(block,key[:8])
//Plaintext complement
plain=PKCS5append(plain)
EncryptMode.CryptBlocks(plain,plain)
err2:= ioutil.WriteFile(writfileName, []byte(base64.StdEncoding.EncodeToString(plain)), 0777)
if err2!=nil{
fmt.Println("Failed to save encrypted file")
return -1, nil
}else{
fmt.Println("The file is encrypted. Remember to encrypt the key")
return 0, nil
}
}
//Decrypt files
func DecryptFile(fileName string, writfileName string, key []byte) (int, error) {
file,err:=os.Open(fileName)
writfile, err1:=os.Create(writfileName)
if err!=nil {
fmt.Println("File not found")
os.Exit(0)
}
if err1!=nil {
os.Exit(0)
}
defer file.Close()
defer writfile.Close()
//Read file contents
plain,_:=ioutil.ReadAll(file)
//Create block
block,_:=des.NewTripleDESCipher(key)
DecryptMode:=cipher.NewCBCDecrypter(block,key[:8])
plain,_=base64.StdEncoding.DecodeString(string(plain))
DecryptMode.CryptBlocks(plain,plain)
plain=PKCS5remove(plain)
err2 := ioutil.WriteFile(writfileName,plain,0777)
if err2!=nil{
fmt.Println("Failed to save decrypted file")
return -1, err2
}else{
fmt.Println("File decrypted")
return 0, nil
}
}
func PKCS5append(plaintext []byte) []byte {
num := 8 - len(plaintext)%8
for i:=0;i<num;i++{
plaintext=append(plaintext,byte(num))
}
return plaintext
}
func PKCS5remove(plaintext []byte) []byte {
length := len(plaintext)
num := int(plaintext[length-1])
return plaintext[:(length - num)]
}
func main() {
fmt.Println(syscall.Getpid())
var PassKey string = "***********************"
//Generate key
PassKeyByte :=sha256.Sum224([]byte(PassKey))
key :=PassKeyByte[:24]
num1, _:= EncryptFile("PrimaryFile.py", "EncryptFile.py", key)
num2, _ := DecryptFile("EncryptFile.py", "DecryptFile.py", key)
//Using a goroutine for task processing
go RunPython()
//Wait for the task to finish processing
time.Sleep(time.Second * 5)
fmt.Println(num1, num2)
}
//Execute Python program
func RunPython() {
cmd := exec.Command("python3", "DecryptFile.py")
lines,_ := cmd.Output()
err2 := cmd.Start()
if err2!=nil{
fmt.Println(err2)
os.Exit(1)
} else {
fmt.Println(string(lines))
}
err3 := cmd.Wait()
if err3 !=nil {
fmt.Println(err3)
} else {
fmt.Println("succeeded")
}
err := os.Remove("DecryptFile.py")
if err != nil {
fmt.Println("Deletion failed")
} else {
fmt.Println("Deletion succeeded")
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/230353.html
標籤:區塊鏈
