使用PowerShell解密Python加密的字符串 [英] Using PowerShell to decrypt a Python encrypted String

查看:120
本文介绍了使用PowerShell解密Python加密的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python程序来获取大字符串(256个字节或更多),并使用AES-CBC对其进行加密。这将在Linux系统上发生,然后将加密的数据传输到Windows计算机,在此解密。我能够在python中加密数据,但无法在PowerShell中解密数据。我相信我的问题出在PowerShell代码上,但不确定。在PowerShell中,我得到了一个很大的ASCII字符字符串作为输出:

I am using a python program to take a large string (256 bytes or more) and encrypt it using AES-CBC. This would happen on a Linux system then the encrypted data would be transferred to a windows machine where it would be decrypted. I am able to encrypt the data in python, but cannot decrypt the data in PowerShell. I believe my issue is with the PowerShell code, but am not completely sure. In PowerShell I am getting a large string of ASCII characters as my output:

IV is equal to 81 114 150 34 27 90 82 1 78 188 221 119 110 240 56 183
AES key is TXlwYXNzcGhyYXNlS2V5MQ==
Unencrypted string: TextMustBe16BytesUsually
Encrypted string: ZjE5NGRkMjY0MGU3NzJhNjRlZWI1MjlhYzlmNzk4N2NhNjE4ZjlmZDE5MmE3MWJjZDczMTBlZjBmNDQ3ZTUzMw==
Unencrypted string: g�V��⓪����DĖ    u���.Ӣ���B�#�!�v����ƭɐ

我将在下面发布二者的源代码,对您的帮助将不胜感激。

I will post the source for both below, any help is greatly appreciated.

Python:

from Crypto.Cipher import AES
import hashlib
import sys
import base64
import binascii
import Padding

val='TextMustBe16BytesUsually'
password='ew+39INFhCg+rcNZsY/bd64hWoopaOA5m8r9mgfF/x0='
ival= 12345678


plaintext=val

def encrypt2(plaintext,key, mode,iv):
    encobj = AES.new(key,mode,iv)
    return(encobj.encrypt(plaintext))

def decrypt2(ciphertext,key, mode,iv):
    encobj = AES.new(key,mode,iv)
    return(encobj.decrypt(ciphertext))


key = hashlib.sha256(password).digest()

iv= hex(ival)[2:8].zfill(16)



print "IV: "+ base64.b64encode(iv)

plaintext=val
plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode=0)

ciphertext = encrypt2(plaintext,key,AES.MODE_CBC,iv)
print ciphertext
print "Cipher (CBC): "+ base64.b64encode(binascii.hexlify(bytearray(ciphertext)))

plaintext = decrypt2(ciphertext,key,AES.MODE_CBC,iv)
plaintext = Padding.removePadding(plaintext,mode=0)
print "Decrypt: "+plaintext

PowerShell:

Powershell:

function Create-AesManagedObject($key, $IV) {
    $aesManaged = New-Object "System.Security.Cryptography.AesManaged"
    $aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
    $aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
    $aesManaged.BlockSize = 128
    $aesManaged.KeySize = 256
    if ($IV) {
        if ($IV.getType().Name -eq "String") {
            $aesManaged.IV = [System.Convert]::FromBase64String($IV)
        }
        else {
            $aesManaged.IV = $IV
        }
    }
    if ($key) {
        if ($key.getType().Name -eq "String") {
            $aesManaged.Key = [System.Convert]::FromBase64String($key)
        }
        else {
            $aesManaged.Key = $key
        }
    }
    $aesManaged
}

function Create-AesKey() {
    $aesManaged = Create-AesManagedObject
    $aesManaged.GenerateKey()
    [System.Convert]::ToBase64String($aesManaged.Key)
}


function Decrypt-String($key, $encryptedStringWithIV) {
    $bytes = [System.Convert]::FromBase64String($encryptedStringWithIV)
    $aesManaged = Create-AesManagedObject $key $IV
    $decryptor = $aesManaged.CreateDecryptor();
    $unencryptedData = $decryptor.TransformFinalBlock($bytes, 16, $bytes.Length - 16);
    $aesManaged.Dispose()
    [System.Text.Encoding]::UTF8.GetString($unencryptedData).Trim([char]0)
}


$key = "ew+39INFhCg+rcNZsY/bd64hWoopaOA5m8r9mgfF/x0="
"KEY:"
$key
"IV:"
$IV
$unencryptedString = "TextMustBe16BytesUsually"
"ENCRYPTED STRING"
$encryptedString = "ZjE5NGRkMjY0MGU3NzJhNjRlZWI1MjlhYzlmNzk4N2NhNjE4ZjlmZDE5MmE3MWJjZDczMTBlZjBmNDQ3ZTUzMw=="
$encryptedString
$backToPlainText = Decrypt-String $key $encryptedString
"Plain Text"
$backToPlainText


推荐答案

我修改了您的加密。您的加密丢失了$ IV参考。

I modified your Encrypt. Your encrypt was missing the $IV reference.

解密操作将附加IV数组并将其传递给对象。

The decrypt appends the IV array and also passes it to the object.

function Encrypt-String($key, $unencryptedString) {
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($unencryptedString)
    $aesManaged = Create-AesManagedObject $key $IV
    $encryptor = $aesManaged.CreateEncryptor()
    $encryptedData = $encryptor.TransformFinalBlock($bytes, 0, $bytes.Length);
    [byte[]] $fullData = $aesManaged.IV + $encryptedData
    $aesManaged.Dispose()
    [System.Convert]::ToBase64String($fullData)
}

function Decrypt-String($key, $encryptedStringWithIV) {

    $bytes = [System.Convert]::FromBase64String($encryptedStringWithIV)
    $IV = $bytes[0..15]
    $aesManaged = Create-AesManagedObject $key $IV
    $decryptor = $aesManaged.CreateDecryptor();
    $unencryptedData = $decryptor.TransformFinalBlock($bytes, 16, $bytes.Length - 16);
    $aesManaged.Dispose()
    [System.Text.Encoding]::UTF8.GetString($unencryptedData).Trim([char]0)
}


$unencryptedString = "TextMustBe16BytesUsually"
$encryptedString = Encrypt-String $key $unencryptedString
$backToPlainText = Decrypt-String $key $encryptedString
$backToPlainText

这篇关于使用PowerShell解密Python加密的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆