douxiongzhen2126 2018-12-18 18:58
浏览 115

将PowerShell中的加密/解密功能转换为PHP(openssl_)

I am trying to adapt the following functions in PowerShell to PHP:

function EncryptDES
{
Param(
    [String] $plainText,
    [byte[]] $Key,
    [byte[]] $Iv
)

    $tdsAlg = New-Object System.Security.Cryptography.DESCryptoServiceProvider
    $tdsAlg.Key = $Key
    $tdsAlg.IV = $Iv
    $encrypt = $tdsAlg.CreateEncryptor($tdsAlg.Key, $tdsAlg.IV)
    $msEncrypt = New-Object System.IO.MemoryStream
    $csEncrypt = New-Object System.Security.Cryptography.CryptoStream $msEncrypt, $encrypt, "Write"
    $swEncrypt = New-Object System.IO.StreamWriter $csEncrypt
    $swEncrypt.Write($plainText)
    $swEncrypt.Close()
    $csEncrypt.Close()
    $msEncrypt.Close()
    $encrypt.Clear()
    $encrypted = $msEncrypt.ToArray()
    $result = [Convert]::ToBase64String($encrypted)
    return $result;        
}

function DecryptDES
{
Param(
    [String] $encrypted,
    [byte[]] $Key,
    [byte[]] $Iv
)
    [byte[]]$NewStr = [System.Convert]::FromBase64String($encrypted)
    $tdsAlg = New-Object System.Security.Cryptography.DESCryptoServiceProvider
    $tdsAlg.Key = $Key
    $tdsAlg.IV = $Iv
    $encrypt = $tdsAlg.CreateDecryptor($tdsAlg.Key, $tdsAlg.IV)
    $msEncrypt = New-Object System.IO.MemoryStream @(,$NewStr)
    $csEncrypt = New-Object System.Security.Cryptography.CryptoStream $msEncrypt, $encrypt, "Read"
    $swEncrypt = New-Object System.IO.StreamReader $csEncrypt
    [String]$result = $swEncrypt.ReadToEnd()
    $swEncrypt.Close()
    $csEncrypt.Close()
    $msEncrypt.Close()
    $encrypt.Clear()

    return $result;     
}

I am trying to convert this code to an equivalent in PHP using the openssl_decrypt and openssl_encrpyt function, I tried with the following code but I do not get anything:

function encrypt_decrypt($action, $string) {
    $output = false;
    $encrypt_method = "AES-256-CBC";
    $secret_key = 'This is my secret key';
    $secret_iv = 'This is my secret iv';
    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);
    if ( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    } else if( $action == 'decrypt' ) {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }
    return $output;
}

If someone could guide me, I would appreciate your help, thank you

  • 写回答

1条回答

  • doulu7174 2018-12-18 20:03
    关注

    This was the solution that I developed and it works perfect:

    <?php
    $txt = 'ciphertext';
    $key = '12345678';
    $iv = '12345678';
    $method = 'des-cbc';
    // cipher_text
    $code = openssl_encrypt($txt, $method, $key, true, $iv);
    
    // ciper_text with base64_encode();
    echo base64_encode($code);
    
    // decrypt method
    $result = openssl_decrypt($code, $method, $key, 1, $iv);
    echo $result;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)