PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 10th, 2003, 08:34 AM
red303 red303 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 2 red303 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sharing Encrypted (TripleDES) Token between Hosts (PHP & .NET)

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...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Sharing Encrypted (TripleDES) Token between Hosts (PHP & .NET)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT