Powershell Encypt和Python解密

时间:2019-04-01 23:00:11

标签: python powershell encryption aes

我有一个即将退休的数据库,但存储着“秘密”,我需要能够加密数据并保存到Windows计算机,然后在Linux计算机上解密。

我已经在Powershell中使用了加密功能,但是,我不知道我是否将加密密钥和初始化向量正确地输入到python中,因为它没有被正确解密。

以下是用于读取纯文本密码并对其进行加密的powershell代码:

$key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($key)

$AES = New-Object System.Security.Cryptography.AesManaged
$iv = $AES.IV
$key = $AES.Key

$iv | Out-File IV.key
$key | Out-File AES.key 

$raw = Get-Content -Path .\passwords.txt -Raw

foreach ($line in -split $raw) {
    $secure_pass = ConvertTo-SecureString -String $line -AsPlainText -Force
    $encrypted_pass = ConvertFrom-SecureString -SecureString $secure_pass -key $key 
    $encrypted_pass | Add-Content encrypted_passes.txt
}

这是python中的解密代码:

rom Crypto import Random
from Crypto.Cipher import AES


key = open("AES.key","r")
iv = open("IV2.key","r")
password_file = open("encrypted_passes.txt","r")
iv_text = iv.read()
key_text = key.read()
iv_base = base64.b64encode(iv_text)
print "IV: " + iv_base[:16]
base_key = base64.b64encode(key_text)
aes = AES.new(base_key[:32], AES.MODE_CBC, iv_base[:16])
print len(iv_base)

for line in password_file:
        print line
        print "blah: " + aes.decrypt(line[:32])

我希望python能够读取加密的密码文件并能够对其正确解密。

0 个答案:

没有答案