转换功能不起作用

时间:2014-12-03 21:19:13

标签: php vb.net migration

我正在尝试将vb.net函数转换为PHP。我进行了转换,但显然是不正确的。

Public Shared Function EncryptString(ByVal InputString As String, ByVal SecretKey As String, Optional ByVal CyphMode As CipherMode = CipherMode.ECB) As String
    Try
        Dim Des As New TripleDESCryptoServiceProvider
        'Put the string into a byte array
        Dim InputbyteArray() As Byte = Encoding.UTF8.GetBytes(InputString)
        'Create the crypto objects, with the key, as passed in
        Dim hashMD5 As New MD5CryptoServiceProvider
        Des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(SecretKey))
        Des.Mode = CyphMode
        Dim ms As MemoryStream = New MemoryStream
        Dim cs As CryptoStream = New CryptoStream(ms, Des.CreateEncryptor(), _
        CryptoStreamMode.Write)
        'Write the byte array into the crypto stream
        '(It will end up in the memory stream)
        cs.Write(InputbyteArray, 0, InputbyteArray.Length)
        cs.FlushFinalBlock()
        'Get the data back from the memory stream, and into a string
        Dim ret As StringBuilder = New StringBuilder
        Dim b() As Byte = ms.ToArray
        ms.Close()
        Dim I As Integer
        For I = 0 To UBound(b)
            'Format as hex
            ret.AppendFormat("{0:X2}", b(I))
        Next

        Return ret.ToString()
    Catch ex As System.Security.Cryptography.CryptographicException
        'ExceptionManager.Publish(ex)
        CatchError(ex.Message)
        Return ""
    End Try

End Function   

我试过这个

    $cipher = mcrypt_module_open(MCRYPT_3DES,'',MCRYPT_MODE_ECB,'');

    $key = $secretkey ; //mb_convert_encoding($secretkey, "ASCII");
    $ks = mcrypt_enc_get_key_size($cipher);
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND); 
    $md5key = substr(md5($key),0,$ks);
    mcrypt_generic_init($cipher, $md5key, $iv);
    $encrypted =  strtoupper(bin2hex(mcrypt_generic($cipher,utf8_encode($password))));
    mcrypt_generic_deinit($cipher);

    return $encrypted;

我有不同的结果,我该如何解决?

0 个答案:

没有答案