从powershell加密并从python解密

从powershell加密并从python解密,python,python-3.x,powershell,encryption,aes,Python,Python 3.x,Powershell,Encryption,Aes,我正在为windows编写自己的collectd软件包,因为它不是现成的,我需要加密powershell中的所有度量数据,并在通过网络传输时使用python在Unix设备上解密。这是我的安全团队批准我的解决方案的要求。我到处寻找解决方案,但没有找到 只是把我写的东西贴出来以防万一有人在这里遇到同样的情况 我的设想: 我的AES密钥将是ASCII密码 我的IV将从系统自动生成 我的数据是json和一大块文本(我在测试中使用了简单文本) powershell: $plainText = 'T

我正在为windows编写自己的collectd软件包,因为它不是现成的,我需要加密powershell中的所有度量数据,并在通过网络传输时使用python在Unix设备上解密。这是我的安全团队批准我的解决方案的要求。我到处寻找解决方案,但没有找到

只是把我写的东西贴出来以防万一有人在这里遇到同样的情况

我的设想:

  • 我的AES密钥将是ASCII密码
  • 我的IV将从系统自动生成
  • 我的数据是json和一大块文本(我在测试中使用了简单文本)
powershell:


$plainText  = 'Testing my encrypting'
$encoding   = New-Object System.Text.ASCIIEncoding
$key        = $encoding.GetBytes("P0Shell Encrypt 2 Pyth0n D3crypt")
$InitializationVector = [System.Byte[]]::new(16)

$AESCipher  = New-Object System.Security.Cryptography.AesCryptoServiceProvider
$AESCipher.Key  = $key
$AESCipher.IV   = $InitializationVector

$Text2Bytes     = [System.Text.Encoding]::UTF8.GetBytes($plainText)
$Encryptor      = $AESCipher.CreateEncryptor()
$EncryptedBytes = $Encryptor.TransformFinalBlock($Text2Bytes, 0, $Text2Bytes.Length)

[byte[]] $FullData    = $AESCipher.IV + $EncryptedBytes
$CipherText     = [System.Convert]::ToBase64String($FullData)
$CipherText

$AESCipher.Dispose()

在powershell上运行该程序:

PS C:\Users\pkkatta\Documents>\encrypt2.ps1 aaaaaaaaaaaaaaaaaaaaaaaaaf8fl8opjqzv4x8lnej6czddwiydghmvcncqx32tv9kz

Python代码:


#!/usr/bin/env python3

from Crypto.Cipher import AES
import base64

key = b"P0Shell Encrypt 2 Pyth0n D3crypt"
enc_String = 'AAAAAAAAAAAAAAAAAAAAAF8fl8OpJQZv4x8lNeJ6CZdDwIyDGHMVcnCqx32tV9kZ'

Enc_bytes = base64.b64decode(enc_String)
iv = Enc_bytes[:16]
aes = AES.new(key, AES.MODE_CBC, iv)
print (aes.decrypt(Enc_bytes[16:]).decode("utf-8"))

在执行python代码时:

bash-4.2#/decrypt.py

测试我的加密


希望有人觉得这很有用

你可以从中草拟一个问题,就像你在问题中提到的场景一样,然后将你的方法作为答案发布并接受它。这将是有益的。