none
how can encrypt in PHP and decrypt in Visual Basic 2005 RRS feed

  • 問題

  • in php i use

     

    ckey is UserPwd

    cipher is Mcrypt_safer64

    seed is ********

     

    $cryptedString = mcrypt_encrypt($this->cipher,  $this->ckey , $valString, constant("MCRYPT_MODE_CFB"), $this->seed);
      

    $this->cryptoString     = bin2hex($cryptedString);

     

    example: $valString is mis888

    the output will be d7dffc274200

     

    how can i decrypt d7dffc274200 to mis888 in vb 2005?

    please help me..

    2007年9月4日 上午 07:59

解答

  • All comments already pushed in previous article.

     

     

    Code Snippet
       Function encryptStringToBytes_AES(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
            ' Check arguments.
            If plainText Is Nothing OrElse plainText.Length <= 0 Then
                Throw New ArgumentNullException("plainText")
            End If
            If Key Is Nothing OrElse Key.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            If IV Is Nothing OrElse IV.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            ' Declare the streams used
            ' to encrypt to an in memory
            ' array of bytes.
            Dim msEncrypt As MemoryStream = Nothing
            Dim csEncrypt As CryptoStream = Nothing
            Dim swEncrypt As StreamWriter = Nothing

            ' Declare the RijndaelManaged object
            ' used to encrypt the data.
            Dim aesAlg As RijndaelManaged = Nothing

            ' Declare the bytes used to hold the
            ' encrypted data.
            Dim encrypted As Byte() = Nothing

            Try
                ' Create a RijndaelManaged object
                ' with the specified key and IV.
                aesAlg = New RijndaelManaged()
                aesAlg.Key = Key
                aesAlg.IV = IV

                ' Create a decrytor to perform the stream transform.
                Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

                ' Create the streams used for encryption.
                msEncrypt = New MemoryStream()
                csEncrypt = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                swEncrypt = New StreamWriter(csEncrypt)

                'Write all data to the stream.
                swEncrypt.Write(plainText)

            Finally
                ' Clean things up.
                ' Close the streams.
                If Not (swEncrypt Is Nothing) Then
                    swEncrypt.Close()
                End If
                If Not (csEncrypt Is Nothing) Then
                    csEncrypt.Close()
                End If
                If Not (msEncrypt Is Nothing) Then
                    msEncrypt.Close()
                End If
                ' Clear the RijndaelManaged object.
                If Not (aesAlg Is Nothing) Then
                    aesAlg.Clear()
                End If
            End Try
            ' Return the encrypted bytes from the memory stream.
            Return msEncrypt.ToArray()

        End Function

     

     

    Source: .NET Framework SDK, RijndaelManaged class sample.


    2007年9月4日 上午 08:56
    版主

所有回覆

  • You must to use the same encryption algorithm in PHP and VB 2005 program, and you must provide the key and IV for symmetric algorithm to decrypt information, or provide required information (like private key) for asymmetric algorithm.

     

    Code Snippet
        Function decryptStringFromBytes_AES(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
            ' Check arguments.
            If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
                Throw New ArgumentNullException("cipherText")
            End If
            If Key Is Nothing OrElse Key.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            If IV Is Nothing OrElse IV.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            ' TDeclare the streams used
            ' to decrypt to an in memory
            ' array of bytes.
            Dim msDecrypt As MemoryStream = Nothing
            Dim csDecrypt As CryptoStream = Nothing
            Dim srDecrypt As StreamReader = Nothing

            ' Declare the RijndaelManaged object
            ' used to decrypt the data.
            Dim aesAlg As RijndaelManaged = Nothing

            ' Declare the string used to hold
            ' the decrypted text.
            Dim plaintext As String = Nothing

            Try
                ' Create a RijndaelManaged object
                ' with the specified key and IV.
                aesAlg = New RijndaelManaged()
                aesAlg.Key = Key
                aesAlg.IV = IV

                ' Create a decrytor to perform the stream transform.
                Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

                ' Create the streams used for decryption.
                msDecrypt = New MemoryStream(cipherText)
                csDecrypt = New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
                srDecrypt = New StreamReader(csDecrypt)

                ' Read the decrypted bytes from the decrypting stream
                ' and place them in a string.
                plaintext = srDecrypt.ReadToEnd()
            Finally
                ' Clean things up.
                ' Close the streams.
                If Not (srDecrypt Is Nothing) Then
                    srDecrypt.Close()
                End If
                If Not (csDecrypt Is Nothing) Then
                    csDecrypt.Close()
                End If
                If Not (msDecrypt Is Nothing) Then
                    msDecrypt.Close()
                End If
                ' Clear the RijndaelManaged object.
                If Not (aesAlg Is Nothing) Then
                    aesAlg.Clear()
                End If
            End Try
            Return plaintext

        End Function

     

     

    Source: .NET Framework SDK, RijndaelManaged class code sample.

    2007年9月4日 上午 08:19
    版主
  • And i want to ask how can i use vb to encrypt the result will same for php

    example: vb input mis888 the result is encrypt , same as php encrypted

    2007年9月4日 上午 08:54
  • All comments already pushed in previous article.

     

     

    Code Snippet
       Function encryptStringToBytes_AES(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
            ' Check arguments.
            If plainText Is Nothing OrElse plainText.Length <= 0 Then
                Throw New ArgumentNullException("plainText")
            End If
            If Key Is Nothing OrElse Key.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            If IV Is Nothing OrElse IV.Length <= 0 Then
                Throw New ArgumentNullException("Key")
            End If
            ' Declare the streams used
            ' to encrypt to an in memory
            ' array of bytes.
            Dim msEncrypt As MemoryStream = Nothing
            Dim csEncrypt As CryptoStream = Nothing
            Dim swEncrypt As StreamWriter = Nothing

            ' Declare the RijndaelManaged object
            ' used to encrypt the data.
            Dim aesAlg As RijndaelManaged = Nothing

            ' Declare the bytes used to hold the
            ' encrypted data.
            Dim encrypted As Byte() = Nothing

            Try
                ' Create a RijndaelManaged object
                ' with the specified key and IV.
                aesAlg = New RijndaelManaged()
                aesAlg.Key = Key
                aesAlg.IV = IV

                ' Create a decrytor to perform the stream transform.
                Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

                ' Create the streams used for encryption.
                msEncrypt = New MemoryStream()
                csEncrypt = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                swEncrypt = New StreamWriter(csEncrypt)

                'Write all data to the stream.
                swEncrypt.Write(plainText)

            Finally
                ' Clean things up.
                ' Close the streams.
                If Not (swEncrypt Is Nothing) Then
                    swEncrypt.Close()
                End If
                If Not (csEncrypt Is Nothing) Then
                    csEncrypt.Close()
                End If
                If Not (msEncrypt Is Nothing) Then
                    msEncrypt.Close()
                End If
                ' Clear the RijndaelManaged object.
                If Not (aesAlg Is Nothing) Then
                    aesAlg.Clear()
                End If
            End Try
            ' Return the encrypted bytes from the memory stream.
            Return msEncrypt.ToArray()

        End Function

     

     

    Source: .NET Framework SDK, RijndaelManaged class sample.


    2007年9月4日 上午 08:56
    版主
  • when i use the encryptStringToBytes_AES() function

    i pass the key is UserPwd...the key same of PHP but pop the error message

    "定的金鑰不是此演算法的有效大小。"
    2007年9月4日 上午 09:29