• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 70
  • Last Modified:

Simple3Des VB.net into PHP HELP

Public Sub EncryptFile(ByVal tmpAttFile As String, ByVal tmpPass As String, ByVal tmpText As String)
            Dim wrapper As New Simple3Des(tmpPass)
            Dim cipherText As String = wrapper.EncryptData(tmpText)
            My.Computer.FileSystem.WriteAllText(Path_SendFile & tmpAttFile, tmpText, False)
        Catch ex As Exception
            MsgBox("Proses Create File dan Encrypsi Gagal. Harap Close Dahulu Modul Transaksi", MsgBoxStyle.Information, "Error")
        End Try
    End Sub

Public NotInheritable Class Simple3Des
        Private TripleDes As New TripleDESCryptoServiceProvider

        Private Function TruncateHash(
        ByVal key As String,
        ByVal length As Integer) As Byte()

            Dim sha1 As New SHA1CryptoServiceProvider

            ' Hash the key.
            Dim keyBytes() As Byte =
            Dim hash() As Byte = sha1.ComputeHash(keyBytes)

            ' Truncate or pad the hash.
            ReDim Preserve hash(length - 1)
            Return hash
        End Function

        Sub New(ByVal key As String)
            ' Initialize the crypto provider.
            TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
            TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
        End Sub

Public Function EncryptData(
    ByVal plaintext As String) As String

            ' Convert the plaintext string to a byte array.
            Dim plaintextBytes() As Byte =

            ' Create the stream.
            Dim ms As New System.IO.MemoryStream
            ' Create the encoder to write to the stream.
            Dim encStream As New CryptoStream(ms,

            ' Use the crypto stream to write the byte array to the stream.
            encStream.Write(plaintextBytes, 0, plaintextBytes.Length)

            ' Convert the encrypted stream to a printable string.
            Return Convert.ToBase64String(ms.ToArray)
        End Function

Open in new window

public static function encrypt($message, $key)
  $mac_algorithm = 'sha1';
  $enc_algorithm = MCRYPT_RIJNDAEL_256;
  $enc_mode = MCRYPT_MODE_CBC;
  $mac = hash_hmac($mac_algorithm, $message, $key, true);
  $mac = substr($mac, 0, ceil(static::getMacAlgoBlockSize($mac_algorithm)/8));
  $message= $mac . $message;

  $iv_size = mcrypt_get_iv_size($enc_algorithm, $enc_mode);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

  $ciphertext = mcrypt_encrypt($enc_algorithm, $key,
   $message, $enc_mode, $iv);

  return base64_encode($iv . $ciphertext);

Open in new window

Someone can help me to convert VB.net into PHP ?
San Lie
San Lie
  • 2
1 Solution
David Johnson, CD, MVPOwnerCommented:
it is never good to try and transpose code from one language to another
here is an example of a working php solution https://github.com/iam-raihan/3DES-ECB-Cryptography-in-PHP
David Johnson, CD, MVPOwnerCommented:
solution offered with no response from author
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now