|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Am trying to create an encrypted token that can be shared across hosts but am not having any success..
Both hosts share Initialisation Vector (IV) and secret key (Key) but encrypted secret (Token) is different for both hosts: Key: cf9f3db8cb4a1095e1ae29a9 IV: ha123PXY Secret:Let us meet at 9 o'clock at the secret place. Algo: TripleDES ======================================== Host 1: (Mcrypt Library / PHP / Linux / Apache) <?PHP $input = "Let us meet at 9 o'clock at the secret place."; $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); $iv="ha123PXY"; mcrypt_generic_init ($td, $key, $iv); $c_t = mcrypt_generic ($td, $input); mcrypt_module_close ($td); $token=base64_encode($c_t); ?> ======================================== Host 2: (.NET) Imports System.Security.Cryptography Imports System.text Imports System.io Public Class _default Inherits System.Web.UI.Page Private Key As Byte() Private IV As Byte() #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Protected WithEvents lblTest As System.Web.UI.WebControls.Label 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 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(md5KeyBy tes).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) testResult.Append("<b>key:</b> " & enc.GetString(Key) & "<br>") testResult.Append("<b>IV:</b> " & enc.GetString(IV) & "<br>") testResult.Append("<b>encrypted ascii:</b> " & enc.GetString(cryptText) & "<br>") testResult.Append("<b>len:</b> " & cryptText.Length & "<br>") testResult.Append("<b>base64:</b> " & System.Convert.ToBase64String(cryptText) & "<br>") testResult.Append("<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 End Class ========================================= Host 1 Output: Key: cf9f3db8cb4a1095e1ae29a9 IV: ha123PXY Secret: Let us meet at 9 o'clock at the secret place. Encrypted ascii:ÌcLZénMå s#eW Bïd šŸñ–ÝRAƒÆÑ$¤®X/t’Wv8ê‹ÉJÁÙ len: 48 Base 64: zGNMG1rpbk3lIHMjFmVXDELvZAman/GW3VJBg8bRJKSuWC8YdJJXdjjqi8lKB8HZ Decrypted: Let us meet at 9 o'clock at the secret place. ========================================= Host 2 Output: key: cf9f3db8cb4a1095e1ae29a9 IV: ha123PXY encrypted ascii: 0tCU-OQH[N- >r$JrfNk.}g*nLhz^(R@@$WA len: 48 base64: jTD0Q9Utg09RBkhbAxPOLYw+cqQbG4HKFPJmzmuu/eeq7kyQhuj63ihSwMAkV0Ee decrypted: Let us meet at 9 o'clock at the secret place. ========================================== I couldn't find anything in searches where people have tried this before or any suggested approaches.. Any ideas would be greatly appreciated... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Sharing Encrypted (TripleDES) Token between PHP & .NET |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|