如何在 Windows 和 Linux 上加密 Python 中的代码对象

发布于 2025-01-09 05:36:41 字数 916 浏览 1 评论 0原文

如何将代码对象加密为 base64、字节、sha256 等,但必须同时支持 Windows 和 Linux/Termux。我已经尝试过 marshal.dumps,但它仅适用于制作它的操作系统。就像:我在 Windows 上使用 marshal.dumps 将该代码对象转换为字节。但它只能在 Windows 上运行,当我将该代码移动到 termux/linux 时,它显示Segmentation Failure。如果我在 linux/termux 上使用相同的方法转换为字节,然后将转换后的代码移至 Windows,它会显示一个对话框,并显示 python 没有响应。主要代码位于 github 上。这是示例代码:

my_code="print('This is encrypted code')"
enc_code=compile(my_code, 'string', 'exec')
# i want to encrypt this enc_code that supports both windows and linux

编辑:

应该有一个解密方法。我还想解密该代码对象并使用 exec 函数执行它。 例如:

code=compile('print("Sometext")', 'nothing', 'exec')
code.encrypted_value='iurefbiuyrencuigfbc8yoiwusenowrov'

解密该加密值然后执行它:

exec('iurefbiuyrencuigfbc8yoiwusenowrov'.decrypted_code_object)

How to Encrypt a code object to base64, bytes, sha256 etc any of them but that must support both of windows and Linux/Termux. I have tried marshal.dumps but it only work on that os where it was made. Like: I converted that code object to bytes with marshal.dumps on windows. But it only runs on windows, when I move that code to termux/linux, it shows Segmentation Fault. And if I convert to bytes with the same method on linux/termux, and then move that converted code to windows, it shows a dialog box and says python is not responding. The main code is on github. Here is a sample code:

my_code="print('This is encrypted code')"
enc_code=compile(my_code, 'string', 'exec')
# i want to encrypt this enc_code that supports both windows and linux

Edit:

There should be a decrypt method. I also want to decrypt that code object and execute that with exec function.
Eg:

code=compile('print("Sometext")', 'nothing', 'exec')
code.encrypted_value='iurefbiuyrencuigfbc8yoiwusenowrov'

And decrypt that encrypted value then execute it:

exec('iurefbiuyrencuigfbc8yoiwusenowrov'.decrypted_code_object)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2025-01-16 05:36:41

你可以使用密码学库,还没有检查它,但我认为它与linux和windows兼容。

from cryptography.fernet import Fernet
my_code="print('This is encrypted code')"

key=Fernet.generate_key()
print (key) #SAVE THIS KEY this key will be used to decrypt

## Encryption
f=Fernet(key)
encrypted_text=f.encrypt(my_code.encode()) #encrypting your code
print (encrypted_text)


### Decryption
f=Fernet(key) #passing the key you saved
decrypted_text=f.decrypt(encrypted_text) #decrypting the code
print(decrypted_text)





You can use cryptography library, havent checked it but I think it is compatible with both linux and windows.

from cryptography.fernet import Fernet
my_code="print('This is encrypted code')"

key=Fernet.generate_key()
print (key) #SAVE THIS KEY this key will be used to decrypt

## Encryption
f=Fernet(key)
encrypted_text=f.encrypt(my_code.encode()) #encrypting your code
print (encrypted_text)


### Decryption
f=Fernet(key) #passing the key you saved
decrypted_text=f.decrypt(encrypted_text) #decrypting the code
print(decrypted_text)





~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文