1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Encrypt in ASP.NET decrypt in PHP

Discussion in 'C#' started by Malaiko, Jun 27, 2006.

  1. #1
    Hello to you all,

    I need to encrypt some data in ASP.NET and decrypt it in PHP.

    i Am almost doing it, but the end of the encrypted string is different from one another.

    Anyone has a clue?

    String to encode: Let us meet at 9 oclock at the secret place
    Key: its_my_secret
    Return PHP: jTD0Q9Utg09RBkhbAxPOLYw+cqQbG4HKFPJmzmuu/eeq7kyQhuj63gUhbLv6g1rx
    Return ASP.NET: jTD0Q9Utg09RBkhbAxPOLYw+cqQbG4HKFPJmzmuu/eeq7kyQhuj63ihSwMAkV0Ee

    Almost the same...


    Here is the code i am using:
    <?php
    //echo base64_encode("Let us meet at 9 oclock at the secret place");

    $td = mcrypt_module_open('tripledes', '', 'cbc', '');


    // $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
    $iv="ha123PXY";
    $ks = mcrypt_enc_get_key_size($td);


    $key = substr(md5('its_my_secret'), 0, $ks);


    mcrypt_generic_init($td, $key, $iv);


    $encrypted = mcrypt_generic($td, 'Let us meet at 9 oclock at the secret place');
    echo $encrypted . "<br>";
    echo base64_encode($encrypted) . "<br>";
    SEMrush
    mcrypt_generic_deinit($td);


    mcrypt_generic_init($td, $key, $iv);


    $decrypted = mdecrypt_generic($td, $encrypted);


    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);


    echo trim($decrypted) . "\n";
    ?>



    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.io" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Security.Cryptography" %>



    <%@ Assembly Name="APi" %>

    <script runat="server">
    Private Key As Byte()
    Private IV As Byte()
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    runTest()
    end sub


    Private Function runTest()

    Dim testResult As New StringBuilder
    Dim enc As New System.Text.ASCIIEncoding

    Dim plainKeyBytes As Byte() = enc.GetBytes("its_my_secret")
    Dim md5KeyBytes As Byte() = md5Hash(plainKeyBytes)

    'get hexadecimal string representation of MD5 hash and take
    '24 bytes (128 bits) to use as key
    Key = enc.GetBytes(System.BitConverter.ToString(md5KeyBytes).Replace("-", "").ToLower.Substring(0, 24))

    IV = enc.GetBytes("ha123PXY")

    Dim cryptText As Byte()
    Dim plainText As Byte() = enc.GetBytes("Let us meet at 9 o'clock at the secret place.")

    cryptText = Encrypt(plainText)

    RESPONSE.WRITE("<b>key:</b> " & enc.GetString(Key) & "<br>")
    RESPONSE.WRITE("<b>IV:</b> " & enc.GetString(IV) & "<br>")
    RESPONSE.WRITE("<b>encrypted ascii:</b> " & enc.GetString(cryptText) & "<br>")
    RESPONSE.WRITE("<b>len:</b> " & cryptText.Length & "<br>")
    RESPONSE.WRITE("<b>base64:</b> " & System.Convert.ToBase64String(cryptText) & "<br>")
    RESPONSE.WRITE("<b>decrypted:</b> " & enc.GetString(DeCrypt(cryptText)))

    'lblTest.Text = testResult.ToString

    End Function

    Private Function md5Hash(ByVal data() As Byte) As Byte()
    Dim provider As New MD5CryptoServiceProvider
    Return provider.ComputeHash(data)
    End Function

    Private Function Encrypt(ByVal plainText() As Byte) As Byte()
    Dim cryptostream As CryptoStream
    Dim ms As New System.IO.MemoryStream '-- memory stream to write encrypted data to
    Dim tdes As New TripleDESCryptoServiceProvider

    Try
    '-- create TripleDES Encryptor from this instance
    Dim tdesEncrypt As ICryptoTransform = tdes.CreateEncryptor(Key, IV)

    '-- create Crypto Stream that transforms memory stream using des encryption
    cryptostream = New CryptoStream(ms, tdesEncrypt, CryptoStreamMode.Write)

    '-- write out and flush TripleDES encrypted file to memory stream
    With cryptostream
    .Write(plainText, 0, plainText.Length)
    .FlushFinalBlock()
    End With

    '-- return encrypted data
    Return ms.ToArray
    Catch e As Exception
    Throw e
    Finally '-- close streams
    If Not (cryptostream Is Nothing) Then cryptostream.Close()
    If Not (ms Is Nothing) Then ms.Close()
    End Try

    End Function

    Private Function DeCrypt(ByVal encryptedData() As Byte) As Byte()

    Dim ms As System.IO.MemoryStream '-- memory stream to write decrypted data to
    Dim cryptostreamDecr As CryptoStream
    Dim tdes As New TripleDESCryptoServiceProvider

    Try
    '-- create TripleDES instance and Decryptor
    Dim tdesDecrypt As ICryptoTransform = tdes.CreateDecryptor(Key, IV)

    '-- create crypto stream set to read and do a TripleDES decryption transform on incoming bytes
    ms = New MemoryStream(encryptedData)
    cryptostreamDecr = New CryptoStream(ms, tdesDecrypt, CryptoStreamMode.Read)

    '-- get the decrypted bytes
    Dim destArr() As Byte = New BinaryReader(cryptostreamDecr).ReadBytes(ms.Length * 2)
    Return destArr '-- return decrypted data
    Catch e As Exception
    Throw e
    Finally '-- close streams
    If Not (ms Is Nothing) Then ms.Close()

    '-- Don't use the following. It gives error "Stream does not support writing
    'If Not (cryptostreamDecr Is Nothing) Then cryptostreamDecr.Close()
    End Try

    End Function
    </script>
     
    Malaiko, Jun 27, 2006 IP
    SEMrush
  2. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you have a period at the end of your asp.net string and no period at the end of your php string. Also you have an apostrophe in your asp.net string that doesnt appear in your php string.
     
    vectorgraphx, Jun 27, 2006 IP
  3. Malaiko

    Malaiko Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Stupid me.

    I copy pasted the wrong php file:

    Corrected Php (just the sentence is different)
    Corrected encrypted strings:
    PHP: jTD0Q9Utg09RBkhbAxPOLYw+cqQbG4HKFPJmzmuu/eeq7kyQhuj63iS/uKmuuYfs
    ASP.NET: jTD0Q9Utg09RBkhbAxPOLYw+cqQbG4HKFPJmzmuu/eeq7kyQhuj63ihSwMAkV0Ee


    Any ideas?
    <?php
    //echo base64_encode("Let us meet at 9 o\'clock at the secret place.");
    /* Open the cipher */
    $td = mcrypt_module_open('tripledes', '', 'cbc', '');

    /* Create the IV and determine the keysize length, used MCRYPT_RAND
    * on Windows instead */
    // $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
    $iv="ha123PXY";
    $ks = mcrypt_enc_get_key_size($td);

    /* Create key */
    $key = substr(md5('its_my_secret'), 0, $ks);

    /* Intialize encryption */
    mcrypt_generic_init($td, $key, $iv);

    /* Encrypt data */
    $encrypted = mcrypt_generic($td, 'Let us meet at 9 o\'clock at the secret place.');
    echo $encrypted . "<br>";
    echo base64_encode($encrypted) . "<br>";
    /* Terminate encryption handler */
    mcrypt_generic_deinit($td);

    /* Initialize encryption module for decryption */
    mcrypt_generic_init($td, $key, $iv);

    /* Decrypt encrypted string */
    $decrypted = mdecrypt_generic($td, $encrypted);

    /* Terminate decryption handle and close module */
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    /* Show string */
    echo trim($decrypted) . "\n";
    ?>
     
    Malaiko, Jun 27, 2006 IP