PowerShell非对称加密

PowerShell非对称加密,powershell,cryptography,public-key-encryption,x509certificate2,Powershell,Cryptography,Public Key Encryption,X509certificate2,我试图使用OpenSSL生成的公钥加密base64字符串($key)。但是(根据我的发现),我只能导入一个证书(在PowerShell中),然后使用X509Certificate2对象的公钥提取对目标进行加密 然而,在得到结果之后,当我尝试使用python脚本解密结果时,我没有得到原始的明文。但是,当我在python脚本中使用相同的密钥进行加密和解密时,我会得到原始的明文 因此,我猜测要么我错误地进行了PowerShell公钥加密(如下所示),要么我被绊倒了 PowerShell: 函数encr

我试图使用OpenSSL生成的公钥加密base64字符串($key)。但是(根据我的发现),我只能导入一个证书(在PowerShell中),然后使用X509Certificate2对象的公钥提取对目标进行加密

然而,在得到结果之后,当我尝试使用python脚本解密结果时,我没有得到原始的明文。但是,当我在python脚本中使用相同的密钥进行加密和解密时,我会得到原始的明文

因此,我猜测要么我错误地进行了PowerShell公钥加密(如下所示),要么我被绊倒了

PowerShell:

函数encryptKey(){
Param(
[参数(必需=$true,位置=0,帮助消息='key')]
[ValidateNotNullorEmpty()]
[字符串]$key
)    
[字节[]]$certBytes=
$cert=新对象-TypeName System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($certBytes)
$byteval=[System.Text.Encoding]::UTF8.GetBytes($key)
$encKey=$cert.PublicKey.Key.Encrypt($byteval,$true)
$encKey=[System.Convert]::ToBase64String($encKey)
返回$encKey
}
Python解密:

#!/usr/bin/python

from Crypto.PublicKey import RSA
from base64 import b64decode
from base64 import b64encode
from Crypto.Cipher import PKCS1_OAEP


privKey = "<Private key in String>"


encKey = "<encrypted String TO DECRYPT>"


privKey = b64decode(privKey)
r = RSA.importKey(privKey,passphrase=None)
cipher = PKCS1_OAEP.new(r)

encKey = b64decode(encKey)
decKey = cipher.decrypt(encKey)
print decKey
with open('sucks.txt','w') as f:
    f.write(decKey)
#/usr/bin/python
从Crypto.PublicKey导入RSA
从base64导入B64解码
从base64导入b64encode
privKey=“”
encKey=“”
privKey=b64解码(privKey)
r=RSA.importKey(privKey,passphrase=None)
encKey=B64解码(encKey)
decKey=r.decrypt(加密密钥)
印刷德基
将open('sucks.txt','w')作为f:
f、 写(德基)
Python加密:

从Crypto.PublicKey导入RSA
从base64导入B64解码
从base64导入b64encode
键64=b“”
keyDER=b64解码(key64)
keyPub=RSA.importKey(keyDER)
key=“TPnrxxxxxxjT8JLXWMJrPQ==”#key是要加密的目标
enc=keyPub.encrypt(密钥,32)
enc=''.join((enc))
打印B64编码(enc)

多亏了@PetSerAl,他说PowerShell中有OAEP填充,但Python代码中没有(如上所述)。下面是使用PKCS1_OAEP模块编辑的python解密代码

Python解密:

#!/usr/bin/python

from Crypto.PublicKey import RSA
from base64 import b64decode
from base64 import b64encode
from Crypto.Cipher import PKCS1_OAEP


privKey = "<Private key in String>"


encKey = "<encrypted String TO DECRYPT>"


privKey = b64decode(privKey)
r = RSA.importKey(privKey,passphrase=None)
cipher = PKCS1_OAEP.new(r)

encKey = b64decode(encKey)
decKey = cipher.decrypt(encKey)
print decKey
with open('sucks.txt','w') as f:
    f.write(decKey)
#/usr/bin/python
从Crypto.PublicKey导入RSA
从base64导入B64解码
从base64导入b64encode
从Crypto.Cipher导入PKCS1\u OAEP
privKey=“”
encKey=“”
privKey=b64解码(privKey)
r=RSA.importKey(privKey,passphrase=None)
密码=PKCS1_OAEP.new(r)
encKey=B64解码(encKey)
decKey=密码解密(encKey)
印刷德基
将open('sucks.txt','w')作为f:
f、 写(德基)

多亏了@PetSerAl,他说PowerShell中有OAEP填充,但Python代码中没有(如上所述)。下面是使用PKCS1_OAEP模块编辑的python解密代码

Python解密:

#!/usr/bin/python

from Crypto.PublicKey import RSA
from base64 import b64decode
from base64 import b64encode
from Crypto.Cipher import PKCS1_OAEP


privKey = "<Private key in String>"


encKey = "<encrypted String TO DECRYPT>"


privKey = b64decode(privKey)
r = RSA.importKey(privKey,passphrase=None)
cipher = PKCS1_OAEP.new(r)

encKey = b64decode(encKey)
decKey = cipher.decrypt(encKey)
print decKey
with open('sucks.txt','w') as f:
    f.write(decKey)
#/usr/bin/python
从Crypto.PublicKey导入RSA
从base64导入B64解码
从base64导入b64encode
从Crypto.Cipher导入PKCS1\u OAEP
privKey=“”
encKey=“”
privKey=b64解码(privKey)
r=RSA.importKey(privKey,passphrase=None)
密码=PKCS1_OAEP.new(r)
encKey=B64解码(encKey)
decKey=密码解密(encKey)
印刷德基
将open('sucks.txt','w')作为f:
f、 写(德基)

Show python script,这样我们就可以发现它们的区别了。@PetSerAl OP已经添加了它们的代码。PowerShell和python Encrypt之间的一个区别是:python代码不使用填充,而PowerShell代码使用OAEP填充。@PetSerAl感谢您提供的信息!密文确实有OAEP编码,我所要做的就是使用PKCS1_OAEP模块在python中对其进行解密。同时显示python脚本,以便我们能够发现差异。@PetSerAl OP添加了他们的代码。PowerShell和python Encrypt的一个区别是:python代码不使用填充,PowerShell代码使用OAEP填充。@谢谢您提供的信息!密文确实有OAEP编码,我所要做的就是使用PKCS1_OAEP模块在python中对其进行解密。