使用openssl_decrypt将VB.NET AES编码转换为PHP

目前我正致力于在VB.NET中编写外部服务的集成。
这项服务发给我一个字符串 一些使用以下函数加密的信息(这是外部服务开发人员向我发送代码的剪切和过去)</ p>

 公共功能解码(ByVal S As String,ByVal chiave As  String,ByVal iv As String)As String 
Dim rjm As RijndaelManaged = New RijndaelManaged
rjm.KeySize = 128
rjm.BlockSize = 128
rjm.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(chiave)

rjm.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv)
尝试
Dim input()As Byte = System.Convert.FromBase64String(S)
Dim output()As Byte = rjm.CreateDecryptor .TransformFinalBlock(input,0,input.Length)
返回System.Text.Encoding.UTF8.GetString(输出)
Catch ex As System.Exception
返回S
结束尝试
结束函数
</ code > </ pre>

如果我使用下面的mcry解码字符串 pt函数一切正常工作:</ p>

  $ mcrypt_cipher = MCRYPT_RIJNDAEL_128; 
$ mcrypt_mode = MCRYPT_MODE_CBC;
$ iv ='1234567891011121';
$ key ='1234567891011121' ;

$ message = base64_decode($ message);

$ message = rtrim(mcrypt_decrypt($ mcrypt_cipher,$ key,$ message,$ mcrypt_mode,$ iv),“\ 0”);;
</ code> </ pre>

但是我无法使用openssl_decrypt获得相同的结果。 该函数总是以false结果失败,在代码下面:</ p>

  $ result = openssl_decrypt(
base64_decode($ message),
'AES-128-CBC',

$ key,
OPENSSL_RAW_DATA,
$ iv
);
</ code> </ pre>

我很确定这个问题与我遗漏的一些填充有关 ..但我不明白问题的确切位置。</ p>

UPDATE1 </ strong> -

我更正了代码,$ iv和$ key都是我忘了的字符串 '两者都是16Bytes = 128Bits </ p>
</ div>

展开原文

原文

Currently I'm working on integration of external service wrote in VB.NET. This service send me a string with some information encrypted with below function (this is a cut and past from the external service developer that send me the code)

Public Function Decode(ByVal S As String, ByVal chiave As String, ByVal iv As String) As String
    Dim rjm As RijndaelManaged = New RijndaelManaged
    rjm.KeySize = 128
    rjm.BlockSize = 128
    rjm.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(chiave)
    rjm.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv)
    Try
        Dim input() As Byte = System.Convert.FromBase64String(S)
        Dim output() As Byte = rjm.CreateDecryptor.TransformFinalBlock(input, 0, input.Length)
        Return System.Text.Encoding.UTF8.GetString(output)
    Catch ex As System.Exception  
        Return S
    End Try
End Function

if I decode the string with below mcrypt function everything work as expected:

 $mcrypt_cipher = MCRYPT_RIJNDAEL_128;
 $mcrypt_mode = MCRYPT_MODE_CBC;
 $iv= '1234567891011121';
 $key = '1234567891011121';

 $message= base64_decode($message);

 $message = rtrim(mcrypt_decrypt($mcrypt_cipher, $key, $message, $mcrypt_mode, $iv), "\0");;

But i'm unable to obtain same result with openssl_decrypt. the function always fail with false as result, below the code:

 $result = openssl_decrypt(
                base64_decode($message),
                'AES-128-CBC',
                $key,
                OPENSSL_RAW_DATA,
                $iv
            );

I'm pretty sure that the problem is related to some padding that i missing.. but I don't understand where the problem is exactly.

UPDATE1 - I correct the code, both $iv and $key are strings i forgot the ' both are of 16Bytes = 128Bits

dsq1982
dsq1982 谢谢,我做了一个小错字,如我的更新消息中所述都是128位字符串
2 年多之前 回复
douan3414
douan3414 你的$key和$iv不应该是字符串而不是数字吗?AES-128需要16字节密钥和IV。因此,它可能是0填充。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问